-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectShow.py
66 lines (53 loc) · 2.05 KB
/
DirectShow.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
import numpy as N
import VideoCapture
from Camera import Camera, CameraError
class DirectShow(Camera):
'''Camera that interfaces through DirectShow'''
plugin_info = {
'name': 'DirectShow',
'description': 'Video camera interfacing through DirectShow',
'author': 'Philip Chimento',
'copyright year': '2011',
}
def __init__(self, **traits):
super(DirectShow, self).__init__(camera_number=0, **traits)
self._cam = None
def open(self):
self._cam = VideoCapture.Device(self.camera_number)
# Capture a throwaway frame in order to get the resolution
# and bytes per pixel
buffer, width, height = self._cam.getBuffer()
self.resolution = (width, height)
itemsize = len(buffer) / (width * height * 3)
# Pick an appropriate dtype and cache it
if itemsize == 1:
self._dtype = N.uint8
elif itemsize == 2:
self._dtype = N.uint16
elif itemsize == 4:
self._dtype = N.uint32
else:
raise CameraError(
"Unsupported bytes per pixel '{}'".format(itemsize),
self.camera_number)
def close(self):
# No other application can use the camera as long as the Device
# object stays alive
del self._cam
self._cam = None
def query_frame(self):
buffer, width, height = self._cam.getBuffer()
self.resolution = (width, height)
self.frame = N.ndarray(shape=(height, width, 3),
buffer=buffer, dtype=self._dtype)
def _id_string_default(self):
try:
return self._cam.getDisplayName() + ' (DirectShow driver)'
except AttributeError:
return 'DirectShow driver'
def _resolution_changed(self, value):
width, height = value
self._cam.setResolution(width, height)
def configure(self):
self._cam.displayCaptureFilterProperties()
self._cam.displayCapturePinProperties()