-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Live_tutorial.py
More file actions
343 lines (279 loc) · 13.7 KB
/
Copy pathPython_Live_tutorial.py
File metadata and controls
343 lines (279 loc) · 13.7 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
import cv2
import numpy as np
import ctypes
import time
from ctypes import *
from enum import Enum
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
qhyccddll = cdll.LoadLibrary(r"C:\Program Files\QHYCCD\AllInOne\sdk\x64\qhyccd.dll")
QHYCCD_SUCCESS = 0
QHYCCD_ERROR = 0xFFFFFFFF
WINDOW_NAME = "Show"
# get camera id
qhyccddll.GetQHYCCDId.argtypes = [ctypes.c_uint32, ctypes.c_char_p]
# get handle via camera id
qhyccddll.OpenQHYCCD.argtypes = [ctypes.c_char_p]
qhyccddll.OpenQHYCCD.restype = ctypes.c_void_p
# close camera
qhyccddll.CloseQHYCCD.argtypes = [ctypes.c_void_p]
qhyccddll.CloseQHYCCD.restype = ctypes.c_uint32
# read mode
qhyccddll.GetQHYCCDNumberOfReadModes.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_uint32)]
qhyccddll.GetQHYCCDNumberOfReadModes.restype = ctypes.c_uint32
qhyccddll.GetQHYCCDReadModeName.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_char_p]
qhyccddll.GetQHYCCDReadModeName.restype = ctypes.c_uint32
qhyccddll.GetQHYCCDReadModeResolution.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint32)]
qhyccddll.GetQHYCCDReadModeResolution.restype = ctypes.c_uint32
qhyccddll.SetQHYCCDReadMode.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
qhyccddll.SetQHYCCDReadMode.restype = ctypes.c_uint32
# set single stream mode or live stream mode
qhyccddll.SetQHYCCDStreamMode.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
qhyccddll.SetQHYCCDStreamMode.restype = ctypes.c_uint32
# initialize camera
qhyccddll.InitQHYCCD.argtypes = [ctypes.c_void_p]
qhyccddll.InitQHYCCD.restype = ctypes.c_uint32
# get camera chip information
qhyccddll.GetQHYCCDChipInfo.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_double), ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_uint32)]
qhyccddll.GetQHYCCDChipInfo.restype = ctypes.c_uint32
# get parameters value
qhyccddll.GetQHYCCDParam.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
qhyccddll.GetQHYCCDParam.restype = ctypes.c_double
# set parameters
qhyccddll.SetQHYCCDParam.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_double]
qhyccddll.SetQHYCCDParam.restype = ctypes.c_uint32
# set debayer on or off, only for color camera
qhyccddll.SetQHYCCDDebayerOnOff.argtypes = [ctypes.c_void_p, ctypes.c_bool]
qhyccddll.SetQHYCCDDebayerOnOff.restype = ctypes.c_uint32
# set bin mode
qhyccddll.SetQHYCCDBinMode.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint32]
qhyccddll.SetQHYCCDBinMode.restype = ctypes.c_uint32
# set resolution and ROI
qhyccddll.SetQHYCCDResolution.argtypes = [ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32,
ctypes.c_uint32]
qhyccddll.SetQHYCCDResolution.restype = ctypes.c_uint32
# get camera buffer size
qhyccddll.GetQHYCCDMemLength.argtypes = [ctypes.c_void_p]
qhyccddll.GetQHYCCDMemLength.restype = ctypes.c_uint32
# start single stream mode exposing
qhyccddll.ExpQHYCCDSingleFrame.argtypes = [ctypes.c_void_p]
qhyccddll.ExpQHYCCDSingleFrame.restype = ctypes.c_uint32
# get single frame data
qhyccddll.GetQHYCCDSingleFrame.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint8)]
qhyccddll.GetQHYCCDSingleFrame.restype = ctypes.c_uint32
# cancel single exposing and camera will NOT output frame data
qhyccddll.CancelQHYCCDExposingAndReadout.argtypes = [ctypes.c_void_p]
qhyccddll.CancelQHYCCDExposingAndReadout.restype = ctypes.c_uint32
# start live stream mode
qhyccddll.BeginQHYCCDLive.argtypes = [ctypes.c_void_p]
qhyccddll.BeginQHYCCDLive.restype = ctypes.c_uint32
# get live frame data
qhyccddll.GetQHYCCDLiveFrame.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint32), ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint8)]
qhyccddll.GetQHYCCDLiveFrame.restype = ctypes.c_uint32
# stop live stream mode
qhyccddll.StopQHYCCDLive.argtypes = [ctypes.c_void_p]
qhyccddll.StopQHYCCDLive.restype = ctypes.c_uint32
# convert image data
qhyccddll.Bits16ToBits8.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint8), ctypes.POINTER(ctypes.c_uint8),
ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint16, ctypes.c_uint16]
qhyccddll.Bits16ToBits8.restype = ctypes.c_uint32
class CONTROL_ID(Enum):
CONTROL_BRIGHTNESS = 0
CONTROL_CONTRAST = 1
CONTROL_WBR = 2
CONTROL_WBB = 3
CONTROL_WBG = 4
CONTROL_GAMMA = 5
CONTROL_GAIN = 6
CONTROL_OFFSET = 7
CONTROL_EXPOSURE = 8
CONTROL_SPEED = 9
CONTROL_TRANSFERBIT = 10
CONTROL_CHANNELS = 11
CONTROL_USBTRAFFIC = 12
CONTROL_CURTEMP = 14
CONTROL_CURPWM = 15
CONTROL_MANULPWM = 16
CONTROL_CFWPORT = 17
CONTROL_COOLER = 18
CONTROL_ST4PORT = 19
CAM_COLOR = 20
CAM_BIN1X1MODE = 21
CAM_BIN2X2MODE = 22
CAM_BIN3X3MODE = 23
CAM_BIN4X4MODE = 24
CAM_8BITS = 34
CAM_16BITS = 35
CAM_GPS = 36
CONTROL_AMPV = 41
CONTROL_CFWSLOTSNUM = 44
CAM_SINGLEFRAMEMODE = 57
CAM_LIVEVIDEOMODE = 58
CAM_IS_COLOR = 59
def check_ret(ret, func_name):
if ret != QHYCCD_SUCCESS:
raise RuntimeError(f"{func_name} failed (ret={ret})")
def open_first_camera():
camhandle = 0
num = qhyccddll.ScanQHYCCD()
print("ScanQHYCCD() num =", num)
for index in range(num):
print("index =", index)
id_buffer = ctypes.create_string_buffer(40)
ret = qhyccddll.GetQHYCCDId(index, id_buffer)
result_id = id_buffer.value.decode("utf-8")
print("GetQHYCCDId() ret =", ret, "id =", result_id)
camhandle = qhyccddll.OpenQHYCCD(id_buffer)
handle_text = hex(camhandle) if camhandle else "0x0"
print("OpenQHYCCD() camhandle =", handle_text)
if camhandle != 0:
return camhandle
raise RuntimeError("OpenQHYCCD() failed: camera not found or busy")
def frame_buffer_to_image(camhandle, imgdata, imgdata_raw, width, height, bits, channels):
pixel_count = int(width * height)
if bits == 16 and channels == 1:
img16 = np.ctypeslib.as_array(imgdata, shape=(pixel_count * 2,)).view(np.uint16)[:pixel_count].reshape(height, width).copy()
vmin = int(img16.min())
vmax = int(img16.max())
if vmax == vmin:
return np.zeros_like(img16, dtype=np.uint8)
return ((img16.astype(np.float32) - vmin) * 255.0 / (vmax - vmin)).astype(np.uint8)
if bits == 8 and channels == 1:
return np.ctypeslib.as_array(imgdata, shape=(pixel_count,)).reshape(height, width).copy()
if bits == 8 and channels == 3:
return np.ctypeslib.as_array(imgdata, shape=(pixel_count * 3,)).reshape(height, width, 3).copy()
raise RuntimeError(f"Unsupported frame format: bits={bits}, channels={channels}")
def main():
camhandle = 0
live_started = False
ret = qhyccddll.InitQHYCCDResource()
print("InitQHYCCDResource() ret =", ret)
check_ret(ret, "InitQHYCCDResource")
try:
camhandle = open_first_camera()
readmodenum = ctypes.c_uint32()
ret = qhyccddll.GetQHYCCDNumberOfReadModes(camhandle, byref(readmodenum))
print("GetQHYCCDNumberOfReadModes() ret =", ret, "num =", readmodenum.value)
check_ret(ret, "GetQHYCCDNumberOfReadModes")
for index in range(readmodenum.value):
print("index =", index)
name_buffer = ctypes.create_string_buffer(40)
ret = qhyccddll.GetQHYCCDReadModeName(camhandle, index, name_buffer)
result_name = name_buffer.value.decode("utf-8")
print("GetQHYCCDReadModeName() ret =", ret, "name =", result_name)
width = ctypes.c_uint32()
height = ctypes.c_uint32()
ret = qhyccddll.GetQHYCCDReadModeResolution(camhandle, index, byref(width), byref(height))
print("GetQHYCCDReadModeResolution() ret =", ret, "width =", width.value, "height =", height.value)
if readmodenum.value > 0:
# QHY294M Pro live capture is stable in 11M MODE.
read_mode_index = 0
ret = qhyccddll.SetQHYCCDReadMode(camhandle, read_mode_index)
print("SetQHYCCDReadMode() ret =", ret, "index =", read_mode_index)
check_ret(ret, "SetQHYCCDReadMode")
ret = qhyccddll.SetQHYCCDStreamMode(camhandle, 1)
print("SetQHYCCDStreamMode() ret =", ret)
check_ret(ret, "SetQHYCCDStreamMode")
ret = qhyccddll.InitQHYCCD(camhandle)
print("InitQHYCCD() ret =", ret)
check_ret(ret, "InitQHYCCD")
ret = qhyccddll.SetQHYCCDParam(camhandle, CONTROL_ID.CONTROL_TRANSFERBIT.value, 16.0)
print("SetQHYCCDParam() transferbit 16 ret =", ret)
check_ret(ret, "SetQHYCCDParam(CONTROL_TRANSFERBIT)")
chipW = ctypes.c_double()
chipH = ctypes.c_double()
imageW = ctypes.c_uint32()
imageH = ctypes.c_uint32()
pixelW = ctypes.c_double()
pixelH = ctypes.c_double()
imageB = ctypes.c_uint32()
ret = qhyccddll.GetQHYCCDChipInfo(camhandle, byref(chipW), byref(chipH), byref(imageW), byref(imageH), byref(pixelW),
byref(pixelH), byref(imageB))
print("GetQHYCCDChipInfo() ret =", ret)
check_ret(ret, "GetQHYCCDChipInfo")
print("GetQHYCCDChipInfo() chip info =", chipW.value, "x", chipH.value, "mm")
print("GetQHYCCDChipInfo() pixel info =", pixelW.value, "x", pixelH.value, "um")
print("GetQHYCCDChipInfo() image info =", imageW.value, "x", imageH.value, imageB.value, "bits")
ret = qhyccddll.SetQHYCCDBinMode(camhandle, 1, 1)
print("SetQHYCCDBinMode() ret =", ret)
check_ret(ret, "SetQHYCCDBinMode")
ret = qhyccddll.SetQHYCCDResolution(camhandle, 0, 0, imageW.value, imageH.value)
print("SetQHYCCDResolution() ret =", ret)
check_ret(ret, "SetQHYCCDResolution")
ret = qhyccddll.SetQHYCCDParam(camhandle, CONTROL_ID.CONTROL_EXPOSURE.value, 50000.0)
print("SetQHYCCDParam() exposure 50ms ret =", ret)
check_ret(ret, "SetQHYCCDParam(CONTROL_EXPOSURE)")
ret = qhyccddll.SetQHYCCDParam(camhandle, CONTROL_ID.CONTROL_GAIN.value, 10.0)
print("SetQHYCCDParam() gain 10 ret =", ret)
check_ret(ret, "SetQHYCCDParam(CONTROL_GAIN)")
ret = qhyccddll.SetQHYCCDParam(camhandle, CONTROL_ID.CONTROL_OFFSET.value, 10.0)
print("SetQHYCCDParam() offset 10 ret =", ret)
check_ret(ret, "SetQHYCCDParam(CONTROL_OFFSET)")
mem_length = qhyccddll.GetQHYCCDMemLength(camhandle)
if mem_length == 0:
mem_length = imageW.value * imageH.value * 4
print("GetQHYCCDMemLength() =", mem_length)
imgdata = (ctypes.c_uint8 * mem_length)()
raw_length = max(1, int(imageW.value) * int(imageH.value))
imgdata_raw = (ctypes.c_uint8 * raw_length)()
ret = qhyccddll.BeginQHYCCDLive(camhandle)
print("BeginQHYCCDLive() ret =", ret)
check_ret(ret, "BeginQHYCCDLive")
live_started = True
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
print("Press 'q' or ESC to exit.")
frame_count = 0
live_fail_count = 0
while True:
w = ctypes.c_uint32()
h = ctypes.c_uint32()
b = ctypes.c_uint32()
c = ctypes.c_uint32()
ret = qhyccddll.GetQHYCCDLiveFrame(camhandle, byref(w), byref(h), byref(b), byref(c), imgdata)
if ret == QHYCCD_SUCCESS and w.value > 0 and h.value > 0:
live_fail_count = 0
frame_count += 1
if frame_count % 30 == 1:
print("GetQHYCCDLiveFrame() ret =", ret, "w =", w.value, "h =", h.value, "b =", b.value, "c =", c.value, "count =", frame_count,
"data size =", int(w.value * h.value * b.value * c.value / 8))
img = frame_buffer_to_image(camhandle, imgdata, imgdata_raw, w.value, h.value, b.value, c.value)
cv2.imshow(WINDOW_NAME, img)
else:
live_fail_count += 1
if live_fail_count % 50 == 1:
print("GetQHYCCDLiveFrame() waiting... ret =", ret, "w =", w.value, "h =", h.value, "b =", b.value, "c =", c.value)
time.sleep(0.005)
key = cv2.waitKey(1) & 0xFF
if key in (27, ord("q")):
break
try:
if cv2.getWindowProperty(WINDOW_NAME, cv2.WND_PROP_VISIBLE) < 1:
break
except cv2.error:
break
finally:
cv2.destroyAllWindows()
if camhandle != 0 and live_started:
ret = qhyccddll.StopQHYCCDLive(camhandle)
print("StopQHYCCDLive() ret =", ret)
if camhandle != 0:
ret = qhyccddll.CancelQHYCCDExposingAndReadout(camhandle)
print("CancelQHYCCDExposingAndReadout() ret =", ret)
ret = qhyccddll.CloseQHYCCD(camhandle)
print("CloseQHYCCD() ret =", ret)
ret = qhyccddll.ReleaseQHYCCDResource()
print("ReleaseQHYCCDResource() ret =", ret)
if __name__ == "__main__":
main()