-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathqt5-imageprocessing.py
229 lines (169 loc) · 6 KB
/
qt5-imageprocessing.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QWidget,QMainWindow, QLabel, QSizePolicy, QApplication, QAction, QHBoxLayout,QProgressBar
from PyQt5.QtCore import Qt,QEvent,QObject
from PyQt5.QtCore import *
import sys,traceback,os
import ctypes as C
import numpy as np
import cv2
# Import PyhtonNet
import clr
sys.path.append(os.getenv('IC35PATH') + "/redist/dotnet/x64")
# Load IC Imaging Control .NET
clr.AddReference('TIS.Imaging.ICImagingControl35')
# Import the IC Imaging Control namespace.
import TIS.Imaging
class SinkData:
brightnes = 0
FrameBuffer = None
class DisplayBuffer:
'''
This class is needed to copy the image into a pixmap for
displaying in the video window.
'''
locked = False
pixmap = None
def Copy( self, FrameBuffer):
if( int(FrameBuffer.FrameType.BitsPerPixel/8 ) == 4):
imgcontent = C.cast(FrameBuffer.GetIntPtr().ToInt64(), C.POINTER(C.c_ubyte * FrameBuffer.FrameType.BufferSize))
qimage = QImage(imgcontent.contents, FrameBuffer.FrameType.Width,FrameBuffer.FrameType.Height, QImage.Format_RGB32).mirrored()
self.pixmap = QPixmap(qimage)
class WorkerSignals(QObject):
display = pyqtSignal(object)
result = pyqtSignal(object)
class DisplayFilter(TIS.Imaging.FrameFilterImpl):
'''
This frame filter copies an incoming frame into our
DisplayBuffer object and signals the QApplication
with the new buffer.
'''
__namespace__ = "DisplayFilterClass"
signals = WorkerSignals()
dispBuffer = DisplayBuffer()
def GetSupportedInputTypes(self, frameTypes):
frameTypes.Add( TIS.Imaging.FrameType(TIS.Imaging.MediaSubtypes.RGB32))
def GetTransformOutputTypes(self,inType, outTypes):
outTypes.Add(inType)
return True
def Transform(self, src, dest):
dest.CopyFrom(src)
if self.dispBuffer.locked is False:
self.dispBuffer.locked = True
self.dispBuffer.Copy(dest)
self.signals.display.emit(self.dispBuffer)
return False
# create a listener for the sink
class Listener(TIS.Imaging.IFrameQueueSinkListener):
__namespace__ = 'ListenerNameSpace'
saveimage=False
signals = WorkerSignals()
def SinkConnected(self, sink, frameType ):
print('Sink connected')
sink.AllocAndQueueBuffers(2)
def SinkDisconnected(self, sink,buffers):
print('Sink disconnected')
def FramesQueued(self, sink):
frame = sink.PopOutputQueueBuffer()
if self.saveimage is True:
self.saveimage = False
TIS.Imaging.FrameExtensions.SaveAsJpeg(frame,"test.jpg",75)
data = SinkData()
data.FrameBuffer = frame
self.signals.result.emit(data)
def SelectDevice():
ic.LiveStop()
ic.ShowDeviceSettingsDialog()
if ic.DeviceValid is True:
ic.LiveStart()
ic.SaveDeviceStateToFile("device.xml")
def ShowProperties():
if ic.DeviceValid is True:
ic.ShowPropertyDialog()
ic.SaveDeviceStateToFile("device.xml")
def SnapImage():
listener.saveimage = True
print("Snap")
def Close():
if ic.DeviceValid is True:
ic.LiveStop()
app.quit()
def imageCallback(x,y,buffer):
print("hallo")
return 0
def OnResult(result):
imgcontent = C.cast(result.FrameBuffer.GetIntPtr().ToInt64(), C.POINTER(C.c_ubyte * result.FrameBuffer.FrameType.BufferSize))
img = np.ndarray(buffer = imgcontent.contents,
dtype = np.uint8,
shape = (result.FrameBuffer.FrameType.Height,
result.FrameBuffer.FrameType.Width,
int(result.FrameBuffer.FrameType.BitsPerPixel/8 )) )
# Calculate average image brightness
gray = cv2.cvtColor(img ,cv2.COLOR_BGR2GRAY)
mean = cv2.mean(gray)
brightnessbar.setValue(int(mean[0]))
sink.QueueBuffer(result.FrameBuffer)
def OnDisplay(dispBuffer):
videowindow.setPixmap(dispBuffer.pixmap)
dispBuffer.locked = False
#videowindow.update()
#print("x")
app = QApplication(sys.argv)
w = QMainWindow()
w.resize(640, 480)
w.move(300, 300)
w.setWindowTitle('Simple Camera')
# Create the menu
mainMenu = w.menuBar()
fileMenu = mainMenu.addMenu('&File')
exitAct = QAction("&Exit",app)
exitAct.setStatusTip("Exit program")
exitAct.triggered.connect(Close)
fileMenu.addAction(exitAct)
deviceMenu = mainMenu.addMenu('&Device')
devselAct = QAction("&Select",app)
devselAct.triggered.connect(SelectDevice)
deviceMenu.addAction(devselAct)
devpropAct = QAction("&Properties",app)
devpropAct.triggered.connect(ShowProperties)
deviceMenu.addAction(devpropAct)
snapAct = QAction("Snap &Image",app)
snapAct.triggered.connect(SnapImage)
deviceMenu.addAction(snapAct)
layout = QHBoxLayout()
mainwindow = QWidget()
videowindow = QLabel()
layout.addWidget(videowindow)
brightnessbar = QProgressBar()
brightnessbar.setRange(0,256)
brightnessbar.setOrientation( Qt.Vertical)
brightnessbar.setValue(25)
layout.addWidget(brightnessbar)
mainwindow.setLayout(layout)
w.setCentralWidget(mainwindow)
# Create the IC Imaging Control object.
ic = TIS.Imaging.ICImagingControl()
# Instantiate the display filter object
# for live display
displayFilter = DisplayFilter()
# Connect the display signal handler to our filter.
displayFilter.signals.display.connect(OnDisplay)
ic.DisplayFrameFilters.Add( ic.FrameFilterCreate(displayFilter))
# Create listener and sink and connect to IC
# for image processing.
listener = Listener(TIS.Imaging.IFrameQueueSinkListener)
sink = TIS.Imaging.FrameQueueSink(listener,TIS.Imaging.MediaSubtypes.RGB32)
ic.Sink = sink
# Connect the signal handler to the listener.
listener.signals.result.connect(OnResult)
# use a TIS.Imaging.MediaStreamSink for video capture alternatively.
ic.LiveDisplay = True
# Try to open the last used video capture device.
try:
ic.LoadDeviceStateFromFile("device.xml",True)
if ic.DeviceValid is True:
ic.LiveStart()
except Exception as ex:
print(ex)
pass
w.show()
app.exec()