forked from davidgrier/pyfab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabdvr.py
115 lines (96 loc) · 3.26 KB
/
fabdvr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
import cv2
from QVideoItem import QVideoItem
import os
class fabdvr(object):
def __init__(self,
source=None,
filename='~/data/fabdvr.avi',
codec='HFYU', **kwds):
"""Record digital video stream with lossless compression
:param camera: object reference to QCameraItem
:param filename: video file name. Extension determines container.
; Not all containers work with all codecs.
:param codec: FOURCC descriptor for video codec
:returns:
:rtype:
;
;Note on codecs:
; On macs, FFV1 appears to work with avi containers
; On Ubuntu 16.04, HFYU works with avi container.
; FFV1 fails silently
; LAGS does not work (not found)
"""
super(fabdvr, self).__init__(**kwds)
self._writer = None
self.source = source
self.filename = filename
self._framenumber = 0
self._nframes = 0
if cv2.__version__.startswith('2'):
self._fourcc = cv2.cv.CV_FOURCC(*codec)
else:
self._fourcc = cv2.VideoWriter_fourcc(*codec)
def record(self, nframes=100):
if (nframes > 0):
self._nframes = nframes
self.start()
def start(self):
if not self.hassource():
return
self.framenumber = 0
self._writer = cv2.VideoWriter(self.filename,
self._fourcc,
self.source.device.fps,
self.size(),
not self.source.gray)
self.source.sigNewFrame.connect(self.write)
def stop(self):
if self.isrecording():
self.source.sigNewFrame.disconnect()
self._writer.release()
self.nframes = 0
self._writer = None
def write(self, frame):
if self.source.transposed:
frame = cv2.transpose(frame)
if self.source.flipped:
frame = cv2.flip(frame, 0)
self._writer.write(frame)
self.framenumber += 1
if (self.framenumber == self._nframes):
self.stop()
@property
def filename(self):
return self._filename
@filename.setter
def filename(self, filename):
if not self.isrecording():
self._filename = os.path.expanduser(filename)
def hassource(self):
return isinstance(self.source, QVideoItem)
def isrecording(self):
return (self._writer is not None)
def size(self):
if self.hassource():
sz = self.source.device.size
w = int(sz.width())
h = int(sz.height())
return (w, h)
else:
return None
def framenumber(self):
return self._framenumber
if __name__ == '__main__':
from PyQt4 import QtGui
from QCameraDevice import QCameraDevice
from QVideoItem import QVideoWidget
import sys
app = QtGui.QApplication(sys.argv)
device = QCameraDevice(size=(640, 480), gray=True)
source = QVideoItem(device)
widget = QVideoWidget(source, background='w')
widget.show()
dvr = fabdvr(source=source)
dvr.record(24)
sys.exit(app.exec_())