-
Notifications
You must be signed in to change notification settings - Fork 152
/
dshowcapture.py
341 lines (309 loc) · 13.1 KB
/
dshowcapture.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
340
341
import os
import platform
import sys
import numpy as np
from ctypes import *
from PIL import Image
import cv2
import json
def resolve(name):
f = os.path.join(os.path.dirname(__file__), name)
return f
lib = None
bm_lib = None
bm_options = None
bm_enabled = False
def set_bm_enabled(v):
global bm_enabled
bm_enabled = v
def set_options(str):
global bm_options
bm_options = str
def create_frame_buffer(width, height, factor):
buffer = bytearray(width * height * 4 * factor)
char_array = c_char * len(buffer)
return char_array.from_buffer(buffer)
class DShowCapture():
def __init__(self):
global lib
global bm_lib
global bm_options
global bm_enabled
if lib is None or bm_lib is None:
if platform.architecture()[0] == '32bit':
dll_path = resolve(os.path.join("dshowcapture", "dshowcapture_x86.dll"))
lib = cdll.LoadLibrary(dll_path)
if bm_enabled:
dll_path = resolve(os.path.join("dshowcapture", "libminibmcapture32.dll"))
bm_lib = cdll.LoadLibrary(dll_path)
else:
dll_path = resolve(os.path.join("dshowcapture", "dshowcapture_x64.dll"))
lib = cdll.LoadLibrary(dll_path)
if bm_enabled:
dll_path = resolve(os.path.join("dshowcapture", "libminibmcapture64.dll"))
bm_lib = cdll.LoadLibrary(dll_path)
# DirectShow
lib.create_capture.restype = c_void_p
lib.get_devices.argtypes = [c_void_p]
lib.get_device.argtypes = [c_void_p, c_int, c_char_p, c_int]
lib.capture_device.argtypes = [c_void_p, c_int, c_int, c_int, c_int]
lib.capture_device_by_dcap.argtypes = [c_void_p, c_int, c_int, c_int, c_int, c_longlong]
lib.capture_device_default.argtypes = [c_void_p, c_int]
lib.get_width.argtypes = [c_void_p]
lib.get_height.argtypes = [c_void_p]
lib.get_fps.argtypes = [c_void_p]
lib.get_flipped.argtypes = [c_void_p]
lib.get_colorspace.argtypes = [c_void_p]
lib.get_colorspace_internal.argtypes = [c_void_p]
lib.capturing.argtypes = [c_void_p]
lib.get_frame.argtypes = [c_void_p, c_int, c_char_p, c_int]
lib.get_size.argtypes = [c_void_p]
lib.stop_capture.argtypes = [c_void_p]
lib.get_json_length.argtypes = [c_void_p]
lib.get_json.argtypes = [c_void_p, c_char_p, c_int]
lib.destroy_capture.argtypes = [c_void_p]
# Blackmagic
if bm_enabled:
bm_lib.start_capture_single.argtypes = [c_int, c_int, c_void_p]
bm_lib.read_frame_bgra32_blocking.argtypes = [c_char_p, c_int]
bm_lib.stop_capture_single.argtypes = []
bm_lib.get_json_length.argtypes = []
bm_lib.get_json.argtypes = [c_char_p, c_int]
if bm_options is not None:
bm_lib.set_options.argtypes = [c_char_p]
bm_lib.set_options(bm_options.encode())
self.lib = lib
if bm_enabled:
self.bm_lib = bm_lib
self.cap = lib.create_capture()
self.name_buffer = create_string_buffer(255);
self.buffer = None
self.have_devices = False
self.size = None
self.real_size = None
self.info = None
self.type = None
def __del__(self):
if self.buffer is not None:
del self.buffer
del self.name_buffer
self.destroy_capture()
def get_devices(self):
self.have_devices = True
return self.lib.get_devices(self.cap)
def get_device(self, device_number):
self.lib.get_device(self.cap, device_number, self.name_buffer, 255)
name_str = str(self.name_buffer.value.decode('utf8', 'surrogateescape'))
return name_str
def get_info(self):
global bm_enabled
# DirectShow
json_length = self.lib.get_json_length(self.cap);
json_buffer = create_string_buffer(json_length)
self.lib.get_json(self.cap, json_buffer, json_length);
json_text = str(json_buffer.value.decode('utf8', 'surrogateescape'))
self.info = json.loads(json_text)
for cam in self.info:
cam["type"] = "DirectShow"
cam["index"] = cam["id"]
# Blackmagic
if bm_enabled:
json_length = self.bm_lib.get_json_length();
json_buffer = create_string_buffer(json_length)
self.bm_lib.get_json(json_buffer, json_length);
json_text = str(json_buffer.value.decode('utf8', 'surrogateescape'))
bm_info = json.loads(json_text)
dshow_len = len(self.info)
for cam in bm_info:
cam["type"] = "Blackmagic"
cam["index"] = cam["id"] + dshow_len
self.info.extend(bm_info)
return self.info
def capture_device(self, cam, width, height, fps):
if not self.have_devices:
self.get_devices()
ret = self.lib.capture_device(self.cap, cam, width, height, fps) == 1
if ret:
self.type = "DirectShow"
self.width = self.get_width()
self.height = self.get_height()
self.fps = self.get_fps()
self.flipped = self.get_flipped()
self.colorspace = self.get_colorspace()
self.colorspace_internal = self.get_colorspace_internal()
self.size = self.width * self.height * 4
self.buffer = create_frame_buffer(self.width, self.height, 4)
else:
self.size = None
self.real_size = None
return ret;
def capture_device_by_dcap(self, cam, dcap, width, height, fps):
global bm_enabled
if not self.have_devices:
self.get_devices()
if self.info is None:
self.get_info()
fps = int(10000000 / fps)
ret = False
if self.info[cam]['type'] == "DirectShow":
ret = self.lib.capture_device_by_dcap(self.cap, cam, dcap, width, height, fps) == 1
elif bm_enabled and self.info[cam]['type'] == "Blackmagic":
ret = self.bm_lib.start_capture_single(self.info[cam]['id'], self.info[cam]['caps'][dcap]['bmModecode'], None) == 1
if ret:
self.type = self.info[cam]['type']
if self.type == "DirectShow":
self.width = self.get_width()
self.height = self.get_height()
self.fps = self.get_fps()
self.flipped = self.get_flipped()
self.colorspace = self.get_colorspace()
self.colorspace_internal = self.get_colorspace_internal()
self.size = self.width * self.height * 4 * 4
self.buffer = create_frame_buffer(self.width, self.height, 4)
elif bm_enabled and self.type == "Blackmagic":
self.width = self.info[cam]['caps'][dcap]['minCX']
self.height = self.info[cam]['caps'][dcap]['minCY']
self.fps = int(10000000 / self.info[cam]['caps'][dcap]['minInterval'])
self.flipped = True
self.colorspace = 101
self.colorspace_internal = self.colorspace
self.size = self.width * self.height * 4
self.buffer = create_frame_buffer(self.width, self.height, 1)
self.real_size = self.size
else:
self.size = None
self.real_size = None
return ret;
def capture_device_default(self, cam):
if not self.have_devices:
self.get_devices()
ret = self.lib.capture_device_default(self.cap, cam) == 1
if ret:
self.type = "DirectShow"
self.width = self.get_width()
self.height = self.get_height()
self.fps = self.get_fps()
self.flipped = self.get_flipped()
self.colorspace = self.get_colorspace()
self.colorspace_internal = self.get_colorspace_internal()
self.size = self.width * self.height * 4 * 4
self.buffer = create_frame_buffer(self.width, self.height, 4)
else:
self.size = None
self.real_size = None
return ret;
def get_width(self):
return self.lib.get_width(self.cap)
def get_height(self):
return self.lib.get_height(self.cap)
def get_fps(self):
return self.lib.get_fps(self.cap)
def get_flipped(self):
return self.lib.get_flipped(self.cap) != 0
def get_colorspace(self):
colorspace = self.lib.get_colorspace(self.cap)
if colorspace == 0:
colorspace = self.lib.get_colorspace_internal(self.cap)
return colorspace
def get_colorspace_internal(self):
return self.lib.get_colorspace_internal(self.cap)
def capturing(self):
if self.type == "Blackmagic":
return 1
return self.lib.capturing(self.cap) == 1
def get_frame(self, timeout):
global bm_enabled
if self.size is None:
return None
if self.type == "DirectShow":
self.real_size = self.lib.get_frame(self.cap, timeout, self.buffer, self.size)
elif bm_enabled and self.type == "Blackmagic":
self.bm_lib.read_frame_bgra32_blocking(self.buffer, self.size)
else:
return None
img = np.frombuffer(self.buffer, dtype=np.uint8)[0:self.real_size]
if self.colorspace in [100, 101]:
if self.real_size == self.height * self.width * 4:
img = cv2.cvtColor(img.reshape((self.height,self.width,4)), cv2.COLOR_BGRA2BGR)
elif self.real_size == self.height * self.width * 3:
img = img.reshape((self.height,self.width,3))
else:
return None
if not self.flipped:
img = cv2.flip(img, 0)
return img
elif self.colorspace == 200 and self.real_size == (3 * self.height // 2) * self.width:
img = cv2.cvtColor(img.reshape((3 * self.height // 2,self.width,1)), cv2.COLOR_YUV2BGR_I420)
elif self.colorspace == 201 and self.real_size == (3 * self.height // 2) * self.width:
img = cv2.cvtColor(img.reshape((3 * self.height // 2,self.width,1)), cv2.COLOR_YUV2BGR_NV12)
elif self.colorspace == 202 and self.real_size == (3 * self.height // 2) * self.width:
img = cv2.cvtColor(img.reshape((3 * self.height // 2,self.width,1)), cv2.COLOR_YUV2BGR_YV12)
elif self.colorspace == 203 and self.real_size == self.height * self.width:
img = cv2.cvtColor(img.reshape((self.height,self.width,1)), cv2.COLOR_GRAY2BGR)
elif self.colorspace == 300 and self.real_size == self.height * self.width * 2:
img = cv2.cvtColor(img.reshape((self.height,self.width,2)), cv2.COLOR_YUV2BGR_YVYU)
elif self.colorspace == 301 and self.real_size == self.height * self.width * 2:
img = cv2.cvtColor(img.reshape((self.height,self.width,2)), cv2.COLOR_YUV2BGR_YUY2)
elif self.colorspace == 302 and self.real_size == self.height * self.width * 2:
img = cv2.cvtColor(img.reshape((self.height,self.width,2)), cv2.COLOR_YUV2BGR_UYVY)
elif self.colorspace == 303:
return None
elif self.colorspace == 400 and self.real_size > 4:
img = cv2.imdecode(img, cv2.IMREAD_COLOR);
else:
return None
if self.flipped:
img = cv2.flip(img, 0)
return img
def stop_capture(self):
global bm_enabled
self.size = None
self.real_size = None
if self.type == "DirectShow":
return self.lib.stop_capture(self.cap)
elif bm_enabled and self.type == "Blackmagic":
return self.bm_lib.stop_capture_single() != 0
return True
def destroy_capture(self):
if self.cap is None:
return 0
self.stop_capture()
ret = self.lib.destroy_capture(self.cap)
self.cap = None
self.size = None
self.real_size = None
return ret
if __name__ == "__main__":
cam = 0
width = 1280
height = 720
fps = 30
if len(sys.argv) > 1:
cam = int(sys.argv[1])
if len(sys.argv) > 2:
width = int(sys.argv[2])
if len(sys.argv) > 3:
height = int(sys.argv[3])
if len(sys.argv) > 4:
fps = int(sys.argv[4])
cap = DShowCapture()
devices = cap.get_devices()
print("Devices: ", devices)
for i in range(devices):
print(f"{i} {cap.get_device(i)}")
print(f"Capturing: {cap.capture_device(cam, width, height, fps)}")
width = cap.get_width()
height = cap.get_height()
flipped = cap.get_flipped()
print(f"Width: {width} Height: {height} FPS: {cap.get_fps()} Flipped: {flipped} Colorspace: {cap.get_colorspace()} Internal: {cap.get_colorspace_internal()}")
while True:
img = cap.get_frame(1000)
if img is not None:
cv2.imshow("DShowCapture", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
sys.exit(0)
else:
print("Lost frame")
cap.stop_capture()
cap.destroy_capture()
del cap