-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_api_test.py
208 lines (158 loc) · 6.68 KB
/
c_api_test.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
import ctypes
import time
import numpy as np
import cv2
import time
from sys import platform
if platform == "linux" or platform == "linux2":
libname = 'libcnokhwa.so'
elif platform == "darwin":
libname = 'libcnokhwa.dylib'
elif platform == "win32":
libname = 'cnokhwa.dll'
lib = ctypes.CDLL('target/release/' + libname)
lib.cnokhwa_initialize.argtypes = []
lib.cnokhwa_initialize.restype = ctypes.c_int32
lib.cnokhwa_ask_videocapture_auth.argtypes = []
lib.cnokhwa_has_videocapture_auth.argtypes = []
lib.cnokhwa_has_videocapture_auth.restype = ctypes.c_int32
lib.cnokhwa_devices_count.argtypes = []
lib.cnokhwa_devices_count.restype = ctypes.c_int32
lib.cnokhwa_device_name.argtypes = [ctypes.c_int32, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t]
lib.cnokhwa_device_name.restype = ctypes.c_size_t
lib.cnokhwa_device_unique_id.argtypes = [ctypes.c_int32, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t]
lib.cnokhwa_device_unique_id.restype = ctypes.c_size_t
lib.cnokhwa_device_model_id.argtypes = [ctypes.c_int32, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t]
lib.cnokhwa_device_model_id.restype = ctypes.c_size_t
lib.cnokhwa_device_format_width.argtypes = [ctypes.c_int32, ctypes.c_int32]
lib.cnokhwa_device_format_width.restype = ctypes.c_int32
lib.cnokhwa_device_format_height.argtypes = [ctypes.c_int32, ctypes.c_int32]
lib.cnokhwa_device_format_height.restype = ctypes.c_int32
lib.cnokhwa_device_format_frame_rate.argtypes = [ctypes.c_int32, ctypes.c_int32]
lib.cnokhwa_device_format_frame_rate.restype = ctypes.c_int32
lib.cnokhwa_device_format_type.argtypes = [ctypes.c_int32, ctypes.c_int32, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t]
lib.cnokhwa_device_format_type.restype = ctypes.c_size_t
lib.cnokhwa_has_first_frame.argtypes = [ctypes.c_int32]
lib.cnokhwa_has_first_frame.restype = ctypes.c_int32
lib.cnokhwa_frame_width.argtypes = [ctypes.c_int32]
lib.cnokhwa_frame_width.restype = ctypes.c_int32
lib.cnokhwa_frame_height.argtypes = [ctypes.c_int32]
lib.cnokhwa_frame_height.restype = ctypes.c_int32
lib.cnokhwa_frame_bytes_per_row.argtypes = [ctypes.c_int32]
lib.cnokhwa_frame_bytes_per_row.restype = ctypes.c_int32
lib.cnokhwa_grab_frame.argtypes = [ctypes.c_int32, ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t]
lib.cnokhwa_grab_frame.restype = ctypes.c_int32
OK = 0
RESULT_YES = OK
RESULT_NO = -256
def get_string_from_function(func, *args, buffer_size=256):
buf = (ctypes.c_char * buffer_size)()
func(*args, buf, buffer_size)
return buf.value.decode('utf-8')
if lib.cnokhwa_initialize() != OK:
print("Initialization failed")
exit(1)
if lib.cnokhwa_has_videocapture_auth() != 0:
print('Asking videocapture auth...')
lib.cnokhwa_ask_videocapture_auth()
else:
print('Already has videocapture auth')
device_count = lib.cnokhwa_devices_count()
print(f"Number of devices: {device_count}")
if device_count < 1:
exit(0)
max_width = 0
max_height = 0
for device_index in range(device_count):
name = get_string_from_function(lib.cnokhwa_device_name, device_index)
unique_id = get_string_from_function(lib.cnokhwa_device_unique_id, device_index)
model_id = get_string_from_function(lib.cnokhwa_device_model_id, device_index)
print(f"Device {device_index + 1}:")
print(f" Name: {name}")
print(f" Unique ID: {unique_id}")
print(f" Model ID: {model_id}")
format_count = lib.cnokhwa_device_formats_count(device_index)
print(f" Number of formats: {format_count}")
for format_index in range(format_count):
width = lib.cnokhwa_device_format_width(device_index, format_index)
height = lib.cnokhwa_device_format_height(device_index, format_index)
fps = lib.cnokhwa_device_format_frame_rate(device_index, format_index)
format_type = get_string_from_function(
lib.cnokhwa_device_format_type, device_index, format_index
)
if device_index == 0 and width > max_width:
max_width = width
max_height = height
print(f" Format {format_index + 1}: {width}x{height} {fps}fps, Type: {format_type}")
# Staring capture
device_index = 0
width = max_width
height = max_height
# Start capture
result = lib.cnokhwa_start_capture(device_index, width, height)
if result != OK:
print(f"Error starting capture: {result}")
exit(1)
try:
time.sleep(1)
# Wait for a new frame
print("Grabbing a frame...")
while True:
has_frame = lib.cnokhwa_has_first_frame(device_index)
if has_frame == RESULT_NO:
print('Still no frame available')
time.sleep(0.01)
continue
elif has_frame == RESULT_YES:
print("First frame available!")
break
else:
print(f"Error checking for first frame: {has_frame}")
break
# Get frame dimensions
frame_width = lib.cnokhwa_frame_width(device_index)
frame_height = lib.cnokhwa_frame_height(device_index)
bytes_per_row = lib.cnokhwa_frame_bytes_per_row(device_index)
buffer_size = frame_width * frame_height * 3 # RGB format
print(frame_width, frame_height, bytes_per_row)
# Allocate buffer
buffer = (ctypes.c_uint8 * buffer_size)()
# Grab the frame
start = time.time()
result = lib.cnokhwa_grab_frame(device_index, buffer, buffer_size)
if result != OK:
print(f"Error grabbing frame: {result}")
exit(1)
print("Grab time: " + str(time.time() - start))
# Convert to NumPy array
frame_array = np.ctypeslib.as_array(buffer)
# Handle potential padding (stride)
if bytes_per_row > frame_width * 3:
# Reshape to (frame_height, bytes_per_row)
frame_array = frame_array.reshape((frame_height, bytes_per_row))
# Extract the actual image data
image_data = frame_array[:, :frame_width * 3]
# Reshape to (frame_height, frame_width, 3)
frame_array = image_data.reshape((frame_height, frame_width, 3))
else:
# No padding, reshape directly
frame_array = frame_array.reshape((frame_height, frame_width, 3))
# Ensure data type is uint8
frame_array = frame_array.astype(np.uint8)
# Convert RGBA to BGR
frame_bgr = cv2.cvtColor(frame_array, cv2.COLOR_RGBA2BGR)
print(f"frame_array.shape: {frame_array.shape}")
print(f"frame_array.dtype: {frame_array.dtype}")
print(f"frame_bgr.shape: {frame_bgr.shape}")
print(f"frame_bgr.dtype: {frame_bgr.dtype}")
filename = 'frame.jpg'
print('Writing to ' + filename)
if not cv2.imwrite(filename, frame_bgr):
print('Error writing image')
else:
print('Image written successfully')
finally:
# Stop capture
result = lib.cnokhwa_stop_capture(device_index)
if result != OK:
print(f"Error stopping capture: {result}")