-
Notifications
You must be signed in to change notification settings - Fork 5
/
qt_sample.py
68 lines (55 loc) · 2.1 KB
/
qt_sample.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
from PyQt4 import QtGui, QtCore
import gobject, sys
import ueye
import numpy
class Viewer(QtGui.QMainWindow):
def __init__(self, *args):
super(Viewer, self).__init__(*args)
self.setup_graphics_view()
self.setup_camera()
def setup_graphics_view(self):
self.graphics = QtGui.QGraphicsView()
self.setCentralWidget(self.graphics)
self.scene = QtGui.QGraphicsScene()
self.graphics.setScene(self.scene)
# self.pixmap = QtGui.QPixmap()
# self.scene.addPixmap(self.pixmap)
def setup_camera(self):
self.camera = ueye.camera(1)
self.camera.AllocImageMem()
self.camera.SetImageMem()
self.camera.SetImageSize()
self.camera.SetColorMode()
self.camera.CaptureVideo()
self.timer = QtCore.QTimer()
self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.update_image)
self.timer.start(1000)
def update_image(self):
if hasattr(self,"camera_pixmap"):
self.scene.removeItem(self.camera_pixmap)
self.camera.CopyImageMem()
image = gray2qimage(self.camera.data)
self.camera_pixmap = self.scene.addPixmap(QtGui.QPixmap.fromImage(image))
self.timer.start(100)
def closeEvent(self, event):
self.camera.StopLiveVideo()
self.camera.FreeImageMem()
self.camera.ExitCamera()
def gray2qimage(gray):
"""Convert the 2D numpy array `gray` into a 8-bit QImage with a gray
colormap. The first dimension represents the vertical image axis.
http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg17961.html"""
if len(gray.shape) != 2:
raise ValueError("gray2QImage can only convert 2D arrays")
gray = numpy.require(gray, numpy.uint8, 'C')
h, w = gray.shape
result = QtGui.QImage(gray.data, w, h, QtGui.QImage.Format_Indexed8)
result.ndarray = gray
for i in range(256):
result.setColor(i, QtGui.QColor(i, i, i).rgb())
return result
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_window = Viewer()
main_window.show()
app.exec_()