-
Notifications
You must be signed in to change notification settings - Fork 11
/
GenericVideoDevice.py
339 lines (266 loc) · 8.79 KB
/
GenericVideoDevice.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#
# Project: MXCuBE
# https://github.com/mxcube.
#
# This file is part of MXCuBE software.
#
# MXCuBE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MXCuBE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MXCuBE. If not, see <http://www.gnu.org/licenses/>.
"""
[Name]
GenericVideo
[Description]
This module declares class GenericVideo.
This class is not meant to be instanced directly but as
the base class for classes providing access to Video in MXCuBE
"""
import abc
import os
import time
import logging
import gevent
import numpy as np
from QtImport import *
try:
import cv2
except:
pass
from HardwareRepository.BaseHardwareObjects import Device
class GenericVideoDevice(Device):
default_cam_encoding = "yuv422p"
default_poll_interval = 50
default_cam_type = "basler"
def __init__(self, name):
Device.__init__(self,name)
self.cam_mirror = None
self.cam_encoding = None
self.cam_gain = None
self.cam_exposure = None
self.poll_interval = None
self.cam_type = None
self.cam_scale_factor = None
self.image_dimensions = [None,None]
self.image_polling = None
self.image_format = None # not used
self.default_cam_encoding = None
self.default_poll_interval = None
def init(self):
# Read values from XML
try:
self.cam_mirror = eval(self.getProperty("mirror"))
except:
self.cam_mirror = [False, False]
try:
self.cam_encoding = self.getProperty("encoding").lower()
except:
pass
try:
self.poll_interval = self.getProperty("interval")
except:
self.poll_interval = 1
try:
self.cam_gain = float(self.getProperty("gain"))
except:
pass
try:
self.cam_exposure = float(self.getProperty("exposure"))
except:
pass
try:
self.cam_type = self.getProperty("type").lower()
except:
pass
# Apply defaults if necessary
if self.cam_encoding is None:
self.cam_encoding = self.default_cam_encoding
if self.poll_interval is None:
self.poll_interval = self.default_poll_interval
if self.cam_exposure is None:
self.cam_exposure = self.poll_interval/1000.0
if self.cam_type is None:
self.cam_exposure = self.default_cam_type
# Apply values
self.set_cam_encoding(self.cam_encoding)
self.set_exposure_time(self.cam_exposure)
if self.cam_gain is not None:
self.set_gain(self.cam_gain)
self.image_dimensions = self.get_image_dimensions()
# Start polling greenlet
if self.image_polling is None:
self.set_video_live(True)
self.change_owner()
self.image_polling = gevent.spawn(self.do_image_polling,
self.poll_interval/1000.0)
self.setIsReady(True)
""" Generic methods """
def get_new_image(self):
"""
Descript. :
"""
raw_buffer, width, height = self.get_image()
if raw_buffer:
if self.cam_type == "basler":
raw_buffer = self.decoder(raw_buffer)
qimage = QImage(raw_buffer, width, height,
width * 3,
QImage.Format_RGB888)
else:
qimage = QImage(raw_buffer, width, height,
QImage.Format_RGB888)
if self.cam_mirror is not None:
qimage = qimage.mirrored(self.cam_mirror[0], self.cam_mirror[1])
qpixmap = QPixmap(qimage)
self.emit("imageReceived", qpixmap)
return qimage
def get_cam_type(self):
return self.cam_type
def y8_2_rgb(self, raw_buffer):
image = np.fromstring(raw_buffer, dtype=np.uint8)
image.resize(self.image_dimensions[1], self.image_dimensions[0], 1)
return cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
def yuv_2_rgb(self, raw_buffer):
image = np.fromstring(raw_buffer, dtype=np.uint8)
image.resize(self.image_dimensions[1], self.image_dimensions[0], 2)
return cv2.cvtColor(image, cv2.COLOR_YUV2RGB_UYVY)
def save_snapshot(self, filename, image_type='PNG'):
qimage = self.get_new_image()
qimage.save(filename, image_type)
def get_snapshot(self, bw=None, return_as_array=True):
qimage = self.get_new_image()
if return_as_array:
qimage = qimage.convertToFormat(4)
ptr = qimage.bits()
ptr.setsize(qimage.byteCount())
image_array = np.array(ptr).reshape(qimage.height(),
qimage.width(), 4)
if bw:
return np.dot(image_array[...,:3],[0.299, 0.587, 0.144])
else:
return image_array
else:
if bw:
return qimage.convertToFormat(QImage.Format_Mono)
else:
return qimage
def get_scaling_factor(self):
"""
Descript. :
Returns : Scaling factor in float. None if does not exists
"""
return self.cam_scale_factor
def imageType(self):
"""
Descript. : returns image type (not used)
"""
return self.image_format
def start_camera(self):
return
def setLive(self, mode):
"""
Descript. :
"""
return
if mode:
self.set_video_live(True)
self.change_owner()
else:
self.set_video_live(False)
def change_owner(self):
"""
Descript. :
"""
if os.getuid() == 0:
try:
os.setgid(int(os.getenv("SUDO_GID")))
os.setuid(int(os.getenv("SUDO_UID")))
except:
logging.getLogger().warning('%s: failed to change the process'
'ownership.', self.name())
def getWidth(self):
"""
Descript. :
"""
return int(self.image_dimensions[0])
def getHeight(self):
"""
Descript. :
"""
return int(self.image_dimensions[1])
def do_image_polling(self, sleep_time):
"""
Descript. :
"""
while self.get_video_live() == True:
self.get_new_image()
time.sleep(sleep_time)
def connectNotify(self, signal):
"""
Descript. :
"""
return
"""if signal == "imageReceived" and self.image_polling is None:
self.image_polling = gevent.spawn(self.do_image_polling,
self.poll_interval/1000.0)"""
def refresh_video(self):
"""
Descript. :
"""
pass
def set_cam_encoding(self, cam_encoding):
if cam_encoding == "yuv422p":
self.decoder = self.yuv_2_rgb
elif cam_encoding == "y8":
self.decoder = self.y8_2_rgb
""" Methods to be implemented by the implementing class """
@abc.abstractmethod
def get_image_dimensions(self):
pass
@abc.abstractmethod
def get_image(self):
""" The implementing class should return here the latest_image in
raw_format, followed by the width and height of the image"""
pass
@abc.abstractmethod
def get_gain(self):
pass
@abc.abstractmethod
def set_gain(self, gain_value):
pass
@abc.abstractmethod
def get_exposure_time(self):
pass
@abc.abstractmethod
def set_exposure_time(self, exposure_time_value):
pass
@abc.abstractmethod
def get_video_live(self):
pass
@abc.abstractmethod
def set_video_live(self, flag):
pass
# Other (no implementation for now. Can be overloaded, otherwise dummy)
def get_gamma(self):
return
def set_gamma(self, gamma_value):
return
def get_contrast(self):
return
def set_contrast(self, contrast_value):
return
def get_brightness(self):
return
def set_brightness(self, brightness_value):
return
def test_hwo(hwo):
print "Image dimensions: ", hwo.get_image_dimensions()
print "Live Mode: ", hwo.get_video_live()