forked from bernardokyotoku/pydcu
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqt_example.py
121 lines (104 loc) · 3.74 KB
/
qt_example.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
116
117
118
119
120
121
#!/usr/bin/python
import cv2
import sys, time
from PyQt4 import QtCore, QtGui
from qt_example_ui import Ui_Display
import ueye
import IS
class Display(QtGui.QMainWindow):
""" This is the main Thread
Starts the other threads (eye tracking, video capture/recording)
"""
enabled = True
fc = 0
last_t = time.time()
def __init__(self, fps, parent=None):
self.fps = 29
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Display()
self.ui.setupUi(self)
# add the guys we need for displaying frames
self.ui.scene = QtGui.QGraphicsScene(self.ui.layout)
self.ui.view = QtGui.QGraphicsView(self.ui.scene)
self.ui.layout.addWidget(self.ui.view)
self.ui.image = QtGui.QGraphicsPixmapItem()
self.ui.scene.addItem(self.ui.image)
self.ui.view.centerOn(self.ui.image)
self.vcr = VideoRecorder(fps)
self.vcr.plug_in(self)
self.vcr.start()
self.connect( self.vcr, QtCore.SIGNAL("update(QString)"), self.update )
def update(self):
""" update display with frame from eye tracker """
frame = self.vcr.frame
height, width = frame.shape
im = QtGui.QImage(frame.flatten(), width, height, QtGui.QImage.Format_Indexed8)
pix = QtGui.QPixmap.fromImage(im)
self.ui.image.setPixmap(pix)
def closeEvent(self, event):
self.vcr.close()
class VideoRecorder(QtCore.QThread):
""" capture video and record to file """
device = 'TL'
fps = 60
fc = 0 # this one gets reset every couple of seconds
cfc = 0
last_t = time.time()
def __init__(self, fps):
QtCore.QThread.__init__(self)
self.keep_running = True
self.fps = float(fps)
self.configure_camera()
self.fc = 0 # frame counter
self.recording = False
if self.camera.isOpened() == False:
print "Something wrong, can't access camera"
exit
def plug_in(self, dp):
""" make VCR aware of ET and display """
self.dp = dp
def configure_camera(self):
if self.device == 'TL':
print "using Thorlabs camera"
self.camera = ueye.camera(1)
self.camera.AllocImageMem(width=1280,height=1024,bitpixel=8)
self.camera.SetImageMem()
self.camera.SetAOI(x=0, y=0, width=1280, height=1024)
print self.camera.GetAOI()
self.camera.SetFrameRate(self.fps)
print "framerate: %s " % self.camera.GetFrameRate()
self.camera.SetColorMode()
self.camera.CaptureVideo()
else:
self.camera = cv2.VideoCapture(0)
self.camera.set(cv2.cv.CV_CAP_PROP_FPS, self.fps)
def get_frame(self):
self.frame = self.camera.read()
try:
height, width = self.frame.shape
except:
self.frame = cv2.cvtColor( self.frame, cv2.COLOR_RGB2GRAY )
def run(self):
""" main worker
get frame, share it with ET, Display, and write to file if desired """
while True:
self.get_frame()
self.emit(QtCore.SIGNAL('update(QString)'), "update display" )
time.sleep(1.0/self.fps)
def close(self):
if self.device == 'TL':
self.camera.StopLiveVideo()
self.camera.FreeImageMem()
self.camera.ExitCamera()
else:
self.camera.release()
self.out.release()
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Please provide desired frame rate for the camera"
print "Example: \"python " + sys.argv[0] + " 60\""
sys.exit()
app = QtGui.QApplication(sys.argv)
dp = Display(sys.argv[1])
dp.show()
sys.exit(app.exec_())