-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamOperation_class.py
More file actions
442 lines (368 loc) · 17.2 KB
/
Copy pathCamOperation_class.py
File metadata and controls
442 lines (368 loc) · 17.2 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# -- coding: utf-8 --
import threading
import time
import sys
import inspect
import ctypes
import random
import os
import platform
from ctypes import *
currentsystem = platform.system()
if currentsystem == 'Windows':
sys.path.append(os.path.join(os.getenv('MVCAM_COMMON_RUNENV'), "Samples", "Python", "MvImport"))
else:
sys.path.append(os.path.join("..", "..", "MvImport"))
from CameraParams_header import *
from MvCameraControl_class import *
# 强制关闭线程
def Async_raise(tid, exctype):
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
# 停止线程
def Stop_thread(thread):
Async_raise(thread.ident, SystemExit)
# 转为16进制字符串
def To_hex_str(num):
chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'}
hexStr = ""
if num < 0:
num = num + 2 ** 32
while num >= 16:
digit = num % 16
hexStr = chaDic.get(digit, str(digit)) + hexStr
num //= 16
hexStr = chaDic.get(num, str(num)) + hexStr
return hexStr
# 是否是Mono图像
def Is_mono_data(enGvspPixelType):
if PixelType_Gvsp_Mono8 == enGvspPixelType or PixelType_Gvsp_Mono10 == enGvspPixelType \
or PixelType_Gvsp_Mono10_Packed == enGvspPixelType or PixelType_Gvsp_Mono12 == enGvspPixelType \
or PixelType_Gvsp_Mono12_Packed == enGvspPixelType:
return True
else:
return False
# 是否是彩色图像
def Is_color_data(enGvspPixelType):
if PixelType_Gvsp_BayerGR8 == enGvspPixelType or PixelType_Gvsp_BayerRG8 == enGvspPixelType \
or PixelType_Gvsp_BayerGB8 == enGvspPixelType or PixelType_Gvsp_BayerBG8 == enGvspPixelType \
or PixelType_Gvsp_BayerGR10 == enGvspPixelType or PixelType_Gvsp_BayerRG10 == enGvspPixelType \
or PixelType_Gvsp_BayerGB10 == enGvspPixelType or PixelType_Gvsp_BayerBG10 == enGvspPixelType \
or PixelType_Gvsp_BayerGR12 == enGvspPixelType or PixelType_Gvsp_BayerRG12 == enGvspPixelType \
or PixelType_Gvsp_BayerGB12 == enGvspPixelType or PixelType_Gvsp_BayerBG12 == enGvspPixelType \
or PixelType_Gvsp_BayerGR10_Packed == enGvspPixelType or PixelType_Gvsp_BayerRG10_Packed == enGvspPixelType \
or PixelType_Gvsp_BayerGB10_Packed == enGvspPixelType or PixelType_Gvsp_BayerBG10_Packed == enGvspPixelType \
or PixelType_Gvsp_BayerGR12_Packed == enGvspPixelType or PixelType_Gvsp_BayerRG12_Packed == enGvspPixelType \
or PixelType_Gvsp_BayerGB12_Packed == enGvspPixelType or PixelType_Gvsp_BayerBG12_Packed == enGvspPixelType \
or PixelType_Gvsp_BayerRBGG8 == enGvspPixelType \
or PixelType_Gvsp_BayerGR16 == enGvspPixelType or PixelType_Gvsp_BayerRG16 == enGvspPixelType or PixelType_Gvsp_BayerGB16 == enGvspPixelType or PixelType_Gvsp_BayerBG16 == enGvspPixelType \
or PixelType_Gvsp_YUV422_Packed == enGvspPixelType or PixelType_Gvsp_YUV422_YUYV_Packed == enGvspPixelType:
return True
else:
return False
# 相机操作类
class CameraOperation:
def __init__(self, obj_cam, st_device_list, n_connect_num=0, b_open_device=False, b_start_grabbing=False,
h_thread_handle=None,
b_thread_closed=False, st_frame_info=None, b_exit=False, b_save_bmp=False, b_save_jpg=False,
buf_save_image=None,
n_save_image_size=0, n_win_gui_id=0, frame_rate=0, exposure_time=0, gain=0):
self.obj_cam = obj_cam
self.st_device_list = st_device_list
self.n_connect_num = n_connect_num
self.b_open_device = b_open_device
self.b_start_grabbing = b_start_grabbing
self.b_thread_closed = b_thread_closed
self.st_frame_info = MV_FRAME_OUT_INFO_EX()
self.b_exit = b_exit
self.b_save_bmp = b_save_bmp
self.b_save_jpg = b_save_jpg
self.buf_save_image = buf_save_image
self.buf_save_image_len = 0
self.n_save_image_size = n_save_image_size
self.h_thread_handle = h_thread_handle
self.b_thread_closed
self.frame_rate = frame_rate
self.exposure_time = exposure_time
self.gain = gain
self.buf_lock = threading.Lock() # 取图和存图的buffer锁
# 打开相机
def Open_device(self):
if not self.b_open_device:
if self.n_connect_num < 0:
return MV_E_CALLORDER
# ch:选择设备并创建句柄 | en:Select device and create handle
nConnectionNum = int(self.n_connect_num)
stDeviceList = cast(self.st_device_list.pDeviceInfo[int(nConnectionNum)],
POINTER(MV_CC_DEVICE_INFO)).contents
self.obj_cam = MvCamera()
ret = self.obj_cam.MV_CC_CreateHandle(stDeviceList)
if ret != 0:
self.obj_cam.MV_CC_DestroyHandle()
return ret
ret = self.obj_cam.MV_CC_OpenDevice()
if ret != 0:
return ret
print("open device successfully!")
self.b_open_device = True
self.b_thread_closed = False
# ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
if stDeviceList.nTLayerType == MV_GIGE_DEVICE or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
nPacketSize = self.obj_cam.MV_CC_GetOptimalPacketSize()
if int(nPacketSize) > 0:
ret = self.obj_cam.MV_CC_SetIntValue("GevSCPSPacketSize", nPacketSize)
if ret != 0:
print("warning: set packet size fail! ret[0x%x]" % ret)
else:
print("warning: set packet size fail! ret[0x%x]" % nPacketSize)
stBool = c_bool(False)
ret = self.obj_cam.MV_CC_GetBoolValue("AcquisitionFrameRateEnable", stBool)
if ret != 0:
print("get acquisition frame rate enable fail! ret[0x%x]" % ret)
# ch:设置触发模式为off | en:Set trigger mode as off
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerMode", MV_TRIGGER_MODE_OFF)
if ret != 0:
print("set trigger mode fail! ret[0x%x]" % ret)
return MV_OK
# 开始取图
def Start_grabbing(self, winHandle):
if not self.b_start_grabbing and self.b_open_device:
self.b_exit = False
ret = self.obj_cam.MV_CC_StartGrabbing()
if ret != 0:
return ret
self.b_start_grabbing = True
print("start grabbing successfully!")
try:
thread_id = random.randint(1, 10000)
self.h_thread_handle = threading.Thread(target=CameraOperation.Work_thread, args=(self, winHandle))
self.h_thread_handle.start()
self.b_thread_closed = True
finally:
pass
return MV_OK
return MV_E_CALLORDER
# 停止取图
def Stop_grabbing(self):
if self.b_start_grabbing and self.b_open_device:
# 退出线程
if self.b_thread_closed:
Stop_thread(self.h_thread_handle)
self.b_thread_closed = False
ret = self.obj_cam.MV_CC_StopGrabbing()
if ret != 0:
return ret
print("stop grabbing successfully!")
self.b_start_grabbing = False
self.b_exit = True
return MV_OK
else:
return MV_E_CALLORDER
# 关闭相机
def Close_device(self):
if self.b_open_device:
# 退出线程
if self.b_thread_closed:
Stop_thread(self.h_thread_handle)
self.b_thread_closed = False
ret = self.obj_cam.MV_CC_CloseDevice()
if ret != 0:
return ret
# ch:销毁句柄 | Destroy handle
self.obj_cam.MV_CC_DestroyHandle()
self.b_open_device = False
self.b_start_grabbing = False
self.b_exit = True
print("close device successfully!")
return MV_OK
# 设置触发模式
def Set_trigger_mode(self, is_trigger_mode):
if not self.b_open_device:
return MV_E_CALLORDER
if not is_trigger_mode:
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerMode", 0)
if ret != 0:
return ret
else:
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerMode", 1)
if ret != 0:
return ret
ret = self.obj_cam.MV_CC_SetEnumValue("TriggerSource", 7)
if ret != 0:
return ret
return MV_OK
# 软触发一次
def Trigger_once(self):
if self.b_open_device:
return self.obj_cam.MV_CC_SetCommandValue("TriggerSoftware")
# 获取参数
def Get_parameter(self):
if self.b_open_device:
stFloatParam_FrameRate = MVCC_FLOATVALUE()
memset(byref(stFloatParam_FrameRate), 0, sizeof(MVCC_FLOATVALUE))
stFloatParam_exposureTime = MVCC_FLOATVALUE()
memset(byref(stFloatParam_exposureTime), 0, sizeof(MVCC_FLOATVALUE))
stFloatParam_gain = MVCC_FLOATVALUE()
memset(byref(stFloatParam_gain), 0, sizeof(MVCC_FLOATVALUE))
ret = self.obj_cam.MV_CC_GetFloatValue("AcquisitionFrameRate", stFloatParam_FrameRate)
if ret != 0:
return ret
self.frame_rate = stFloatParam_FrameRate.fCurValue
ret = self.obj_cam.MV_CC_GetFloatValue("ExposureTime", stFloatParam_exposureTime)
if ret != 0:
return ret
self.exposure_time = stFloatParam_exposureTime.fCurValue
ret = self.obj_cam.MV_CC_GetFloatValue("Gain", stFloatParam_gain)
if ret != 0:
return ret
self.gain = stFloatParam_gain.fCurValue
return MV_OK
# 设置参数
def Set_parameter(self, frameRate, exposureTime, gain):
if '' == frameRate or '' == exposureTime or '' == gain:
print('show info', 'please type in the text box !')
return MV_E_PARAMETER
if self.b_open_device:
ret = self.obj_cam.MV_CC_SetEnumValue("ExposureAuto", 0)
time.sleep(0.2)
ret = self.obj_cam.MV_CC_SetFloatValue("ExposureTime", float(exposureTime))
if ret != 0:
print('show error', 'set exposure time fail! ret = ' + To_hex_str(ret))
return ret
ret = self.obj_cam.MV_CC_SetFloatValue("Gain", float(gain))
if ret != 0:
print('show error', 'set gain fail! ret = ' + To_hex_str(ret))
return ret
ret = self.obj_cam.MV_CC_SetFloatValue("AcquisitionFrameRate", float(frameRate))
if ret != 0:
print('show error', 'set acquistion frame rate fail! ret = ' + To_hex_str(ret))
return ret
print('show info', 'set parameter success!')
return MV_OK
# 取图线程函数
def Work_thread(self, winHandle):
stOutFrame = MV_FRAME_OUT()
memset(byref(stOutFrame), 0, sizeof(stOutFrame))
while True:
ret = self.obj_cam.MV_CC_GetImageBuffer(stOutFrame, 1000)
if 0 == ret:
# 拷贝图像和图像信息
# 获取缓存锁
self.buf_lock.acquire()
if self.buf_save_image_len < stOutFrame.stFrameInfo.nFrameLen:
if self.buf_save_image is not None:
del self.buf_save_image
self.buf_save_image = None
self.buf_save_image = (c_ubyte * stOutFrame.stFrameInfo.nFrameLen)()
self.buf_save_image_len = stOutFrame.stFrameInfo.nFrameLen
memmove(byref(self.st_frame_info), byref(stOutFrame.stFrameInfo), sizeof(MV_FRAME_OUT_INFO_EX))
memmove(byref(self.buf_save_image), stOutFrame.pBufAddr, self.st_frame_info.nFrameLen)
self.buf_lock.release()
print("get one frame: Width[%d], Height[%d], nFrameNum[%d]"
% (self.st_frame_info.nWidth, self.st_frame_info.nHeight, self.st_frame_info.nFrameNum))
# 释放缓存
self.obj_cam.MV_CC_FreeImageBuffer(stOutFrame)
else:
print("no data, ret = " + To_hex_str(ret))
continue
# 使用Display接口显示图像
stDisplayParam = MV_DISPLAY_FRAME_INFO()
memset(byref(stDisplayParam), 0, sizeof(stDisplayParam))
stDisplayParam.hWnd = int(winHandle)
stDisplayParam.nWidth = self.st_frame_info.nWidth
stDisplayParam.nHeight = self.st_frame_info.nHeight
stDisplayParam.enPixelType = self.st_frame_info.enPixelType
stDisplayParam.pData = self.buf_save_image
stDisplayParam.nDataLen = self.st_frame_info.nFrameLen
self.obj_cam.MV_CC_DisplayOneFrame(stDisplayParam)
# 是否退出
if self.b_exit:
if self.buf_save_image is not None:
del self.buf_save_image
break
# 存jpg图像
def Save_jpg(self):
if self.buf_save_image is None:
return
# 获取缓存锁
self.buf_lock.acquire()
file_path = str(self.st_frame_info.nFrameNum) + ".jpg"
c_file_path = file_path.encode('ascii')
stSaveParam = MV_SAVE_IMAGE_TO_FILE_PARAM_EX()
stSaveParam.enPixelType = self.st_frame_info.enPixelType # ch:相机对应的像素格式 | en:Camera pixel type
stSaveParam.nWidth = self.st_frame_info.nWidth # ch:相机对应的宽 | en:Width
stSaveParam.nHeight = self.st_frame_info.nHeight # ch:相机对应的高 | en:Height
stSaveParam.nDataLen = self.st_frame_info.nFrameLen
stSaveParam.pData = cast(self.buf_save_image, POINTER(c_ubyte))
stSaveParam.enImageType = MV_Image_Jpeg # ch:需要保存的图像类型 | en:Image format to save
stSaveParam.nQuality = 80
stSaveParam.pcImagePath = ctypes.create_string_buffer(c_file_path)
stSaveParam.iMethodValue = 1
ret = self.obj_cam.MV_CC_SaveImageToFileEx(stSaveParam)
self.buf_lock.release()
return ret
def Save_Bmp(self, file_path):
if self.buf_save_image is None:
return MV_E_CALLORDER
self.buf_lock.acquire()
c_file_path = file_path.encode('ascii')
stSaveParam = MV_SAVE_IMAGE_TO_FILE_PARAM_EX()
stSaveParam.enPixelType = self.st_frame_info.enPixelType
stSaveParam.nWidth = self.st_frame_info.nWidth
stSaveParam.nHeight = self.st_frame_info.nHeight
stSaveParam.nDataLen = self.st_frame_info.nFrameLen
stSaveParam.pData = cast(self.buf_save_image, POINTER(c_ubyte))
stSaveParam.enImageType = MV_Image_Bmp
stSaveParam.pcImagePath = ctypes.create_string_buffer(c_file_path)
stSaveParam.iMethodValue = 1
ret = self.obj_cam.MV_CC_SaveImageToFileEx(stSaveParam)
self.buf_lock.release()
return ret
def get_opencv_image(self):
if self.buf_save_image is None:
return None
self.buf_lock.acquire()
width = self.st_frame_info.nWidth
height = self.st_frame_info.nHeight
pixel_type = self.st_frame_info.enPixelType
data = np.frombuffer(self.buf_save_image, dtype=np.uint8)
try:
if pixel_type == PixelType_Gvsp_Mono8:
img = data.reshape((height, width))
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
else:
# Assume Bayer → convert to BGR
img = data.reshape((height, width))
img = cv2.cvtColor(img, cv2.COLOR_BAYER_BG2BGR)
except:
self.buf_lock.release()
return None
self.buf_lock.release()
return img
# 存BMP图像
# def Save_Bmp(self):
# if 0 == self.buf_save_image:
# return
# # 获取缓存锁
# self.buf_lock.acquire()
# file_path = str(self.st_frame_info.nFrameNum) + ".bmp"
# c_file_path = file_path.encode('ascii')
# stSaveParam = MV_SAVE_IMAGE_TO_FILE_PARAM_EX()
# stSaveParam.enPixelType = self.st_frame_info.enPixelType # ch:相机对应的像素格式 | en:Camera pixel type
# stSaveParam.nWidth = self.st_frame_info.nWidth # ch:相机对应的宽 | en:Width
# stSaveParam.nHeight = self.st_frame_info.nHeight # ch:相机对应的高 | en:Height
# stSaveParam.nDataLen = self.st_frame_info.nFrameLen
# stSaveParam.pData = cast(self.buf_save_image, POINTER(c_ubyte))
# stSaveParam.enImageType = MV_Image_Bmp # ch:需要保存的图像类型 | en:Image format to save
# stSaveParam.pcImagePath = ctypes.create_string_buffer(c_file_path)
# stSaveParam.iMethodValue = 1
# ret = self.obj_cam.MV_CC_SaveImageToFileEx(stSaveParam)
# self.buf_lock.release()
# return ret