-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicDemo.py
More file actions
376 lines (316 loc) · 14.6 KB
/
Copy pathBasicDemo.py
File metadata and controls
376 lines (316 loc) · 14.6 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
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import *
from CamOperation_class_continuous import CameraOperation
from MvCameraControl_class import *
from MvErrorDefine_const import *
from CameraParams_header import *
from PyUICBasicDemo import Ui_MainWindow
import ctypes
# 获取选取设备信息的索引,通过[]之间的字符去解析
def TxtWrapBy(start_str, end, all):
start = all.find(start_str)
if start >= 0:
start += len(start_str)
end = all.find(end, start)
if end >= 0:
return all[start:end].strip()
# 将返回的错误码转换为十六进制显示
def ToHexStr(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
if __name__ == "__main__":
# ch:初始化SDK | en: initialize SDK
MvCamera.MV_CC_Initialize()
global deviceList
deviceList = MV_CC_DEVICE_INFO_LIST()
global cam
cam = MvCamera()
global nSelCamIndex
nSelCamIndex = 0
global obj_cam_operation
obj_cam_operation = 0
global isOpen
isOpen = False
global isGrabbing
isGrabbing = False
global isCalibMode # 是否是标定模式(获取原始图像)
isCalibMode = True
# 绑定下拉列表至设备信息索引
def xFunc(event):
global nSelCamIndex
nSelCamIndex = TxtWrapBy("[", "]", ui.ComboDevices.get())
# Decoding Characters
def decoding_char(ctypes_char_array):
"""
安全地从 ctypes 字符数组中解码出字符串。
适用于 Python 2.x 和 3.x,以及 32/64 位环境。
"""
byte_str = memoryview(ctypes_char_array).tobytes()
# 在第一个空字符处截断
null_index = byte_str.find(b'\x00')
if null_index != -1:
byte_str = byte_str[:null_index]
# 多编码尝试解码
for encoding in ['gbk', 'utf-8', 'latin-1']:
try:
return byte_str.decode(encoding)
except UnicodeDecodeError:
continue
# 如果所有编码都失败,使用替换策略
return byte_str.decode('latin-1', errors='replace')
# ch:枚举相机 | en:enum devices
def enum_devices():
global deviceList
global obj_cam_operation
deviceList = MV_CC_DEVICE_INFO_LIST()
n_layer_type = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE
| MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE)
ret = MvCamera.MV_CC_EnumDevices(n_layer_type, deviceList)
if ret != 0:
strError = "Enum devices fail! ret = :" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
return ret
if deviceList.nDeviceNum == 0:
QMessageBox.warning(mainWindow, "Info", "Find no device", QMessageBox.Ok)
return ret
print("Find %d devices!" % deviceList.nDeviceNum)
devList = []
for i in range(0, deviceList.nDeviceNum):
mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
print("\ngige device: [%d]" % i)
user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chUserDefinedName)
model_name = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
print("device user define name: " + user_defined_name)
print("device model name: " + model_name)
nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
print("current ip: %d.%d.%d.%d " % (nip1, nip2, nip3, nip4))
devList.append(
"[" + str(i) + "]GigE: " + user_defined_name + " " + model_name + "(" + str(nip1) + "." + str(
nip2) + "." + str(nip3) + "." + str(nip4) + ")")
elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
print("\nu3v device: [%d]" % i)
user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chUserDefinedName)
model_name = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
print("device user define name: " + user_defined_name)
print("device model name: " + model_name)
strSerialNumber = ""
for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber:
if per == 0:
break
strSerialNumber = strSerialNumber + chr(per)
print("user serial number: " + strSerialNumber)
devList.append("[" + str(i) + "]USB: " + user_defined_name + " " + model_name
+ "(" + str(strSerialNumber) + ")")
elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
print("\nCML device: [%d]" % i)
user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chUserDefinedName)
model_name = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
print("device user define name: " + user_defined_name)
print("device model name: " + model_name)
strSerialNumber = ""
for per in mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber:
if per == 0:
break
strSerialNumber = strSerialNumber + chr(per)
print("user serial number: " + strSerialNumber)
devList.append("[" + str(i) + "]CML: " + user_defined_name + " " + model_name
+ "(" + str(strSerialNumber) + ")")
elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
print("\nCXP device: [%d]" % i)
user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chUserDefinedName)
model_name = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
print("device user define name: " + user_defined_name)
print("device model name: " + model_name)
strSerialNumber = ""
for per in mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber:
if per == 0:
break
strSerialNumber = strSerialNumber + chr(per)
print("user serial number: " + strSerialNumber)
devList.append("[" + str(i) + "]CXP: " + user_defined_name + " " + model_name
+ "(" + str(strSerialNumber) + ")")
elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
print("\nXoF device: [%d]" % i)
user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chUserDefinedName)
model_name = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
print("device user define name: " + user_defined_name)
print("device model name: " + model_name)
strSerialNumber = ""
for per in mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber:
if per == 0:
break
strSerialNumber = strSerialNumber + chr(per)
print("user serial number: " + strSerialNumber)
devList.append("[" + str(i) + "]XoF: " + user_defined_name + " " + model_name
+ "(" + str(strSerialNumber) + ")")
ui.ComboDevices.clear()
ui.ComboDevices.addItems(devList)
ui.ComboDevices.setCurrentIndex(0)
# ch:打开相机 | en:open device
def open_device():
global deviceList
global nSelCamIndex
global obj_cam_operation
global isOpen
if isOpen:
QMessageBox.warning(mainWindow, "Error", 'Camera is Running!', QMessageBox.Ok)
return MV_E_CALLORDER
nSelCamIndex = ui.ComboDevices.currentIndex()
if nSelCamIndex < 0:
QMessageBox.warning(mainWindow, "Error", 'Please select a camera!', QMessageBox.Ok)
return MV_E_CALLORDER
obj_cam_operation = CameraOperation(cam, deviceList, nSelCamIndex)
ret = obj_cam_operation.Open_device()
if 0 != ret:
strError = "Open device failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
isOpen = False
else:
set_continue_mode()
get_param()
isOpen = True
enable_controls()
# ch:开始取流 | en:Start grab image
def start_grabbing():
global obj_cam_operation
global isGrabbing
ret = obj_cam_operation.Start_grabbing(ui.widgetDisplay.winId())
if ret != 0:
strError = "Start grabbing failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
else:
isGrabbing = True
enable_controls()
# ch:停止取流 | en:Stop grab image
def stop_grabbing():
global obj_cam_operation
global isGrabbing
ret = obj_cam_operation.Stop_grabbing()
if ret != 0:
strError = "Stop grabbing failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
else:
isGrabbing = False
enable_controls()
# ch:关闭设备 | Close device
def close_device():
global isOpen
global isGrabbing
global obj_cam_operation
if isOpen:
obj_cam_operation.Close_device()
isOpen = False
isGrabbing = False
enable_controls()
# ch:设置触发模式 | en:set trigger mode
def set_continue_mode():
ret = obj_cam_operation.Set_trigger_mode(False)
if ret != 0:
strError = "Set continue mode failed ret:" + ToHexStr(ret) + " mode is " + str(is_trigger_mode)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
else:
ui.radioContinueMode.setChecked(True)
ui.radioTriggerMode.setChecked(False)
ui.bnSoftwareTrigger.setEnabled(False)
# ch:设置软触发模式 | en:set software trigger mode
def set_software_trigger_mode():
ret = obj_cam_operation.Set_trigger_mode(True)
if ret != 0:
strError = "Set trigger mode failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
else:
ui.radioContinueMode.setChecked(False)
ui.radioTriggerMode.setChecked(True)
ui.bnSoftwareTrigger.setEnabled(isGrabbing)
# ch:设置触发命令 | en:set trigger software
def trigger_once():
ret = obj_cam_operation.Trigger_once()
if ret != 0:
strError = "TriggerSoftware failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
# ch:存图 | en:save image
def save_bmp():
ret = obj_cam_operation.Save_Bmp()
if ret != MV_OK:
strError = "Save BMP failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
else:
print("Save image success")
def is_float(str):
try:
float(str)
return True
except ValueError:
return False
# ch: 获取参数 | en:get param
def get_param():
ret = obj_cam_operation.Get_parameter()
if ret != MV_OK:
strError = "Get param failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
else:
ui.edtExposureTime.setText("{0:.2f}".format(obj_cam_operation.exposure_time))
ui.edtGain.setText("{0:.2f}".format(obj_cam_operation.gain))
ui.edtFrameRate.setText("{0:.2f}".format(obj_cam_operation.frame_rate))
# ch: 设置参数 | en:set param
def set_param():
frame_rate = ui.edtFrameRate.text()
exposure = ui.edtExposureTime.text()
gain = ui.edtGain.text()
if is_float(frame_rate)!=True or is_float(exposure)!=True or is_float(gain)!=True:
strError = "Set param failed ret:" + ToHexStr(MV_E_PARAMETER)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
return MV_E_PARAMETER
ret = obj_cam_operation.Set_parameter(frame_rate, exposure, gain)
if ret != MV_OK:
strError = "Set param failed ret:" + ToHexStr(ret)
QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok)
return MV_OK
# ch: 设置控件状态 | en:set enable status
def enable_controls():
global isGrabbing
global isOpen
# 先设置group的状态,再单独设置各控件状态
ui.groupGrab.setEnabled(isOpen)
ui.groupParam.setEnabled(isOpen)
ui.bnOpen.setEnabled(not isOpen)
ui.bnClose.setEnabled(isOpen)
ui.bnStart.setEnabled(isOpen and (not isGrabbing))
ui.bnStop.setEnabled(isOpen and isGrabbing)
ui.bnSoftwareTrigger.setEnabled(isGrabbing and ui.radioTriggerMode.isChecked())
ui.bnSaveImage.setEnabled(isOpen and isGrabbing)
# ch: 初始化app, 绑定控件与函数 | en: Init app, bind ui and api
app = QApplication(sys.argv)
mainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(mainWindow)
ui.bnEnum.clicked.connect(enum_devices)
ui.bnOpen.clicked.connect(open_device)
ui.bnClose.clicked.connect(close_device)
ui.bnStart.clicked.connect(start_grabbing)
ui.bnStop.clicked.connect(stop_grabbing)
ui.bnSoftwareTrigger.clicked.connect(trigger_once)
ui.radioTriggerMode.clicked.connect(set_software_trigger_mode)
ui.radioContinueMode.clicked.connect(set_continue_mode)
ui.bnGetParam.clicked.connect(get_param)
ui.bnSetParam.clicked.connect(set_param)
ui.bnSaveImage.clicked.connect(save_bmp)
mainWindow.show()
app.exec_()
close_device()
# ch:反初始化SDK | en: finalize SDK
MvCamera.MV_CC_Finalize()
sys.exit()