forked from JoeHowse/VisualizingTheInvisible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPySpinCapture.py
137 lines (104 loc) · 4.58 KB
/
PySpinCapture.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
import PySpin
import cv2
__author__ = 'Joseph Howse'
__copyright__ = 'Copyright (c) 2018, Nummist Media Corporation Limited'
__credits__ = ['Joseph Howse']
__license__ = 'BSD 3-Clause'
__version__ = '0.0.1'
__maintainer__ = 'Joseph Howse'
__email__ = '[email protected]'
__status__ = 'Prototype'
class PySpinCapture:
def __init__(self, index, roi, binning_radius=1, is_monochrome=False):
self._system = system = PySpin.System.GetInstance()
self._camera_list = system.GetCameras()
self._camera = self._camera_list.GetByIndex(index)
self._camera.Init()
self._nodemap = self._camera.GetNodeMap()
# Enable continuous acquisition mode.
node_acquisition_mode = PySpin.CEnumerationPtr(self._nodemap.GetNode(
'AcquisitionMode'))
node_acquisition_mode_continuous = node_acquisition_mode.GetEntryByName(
'Continuous')
acquisition_mode_continuous = node_acquisition_mode_continuous.GetValue()
node_acquisition_mode.SetIntValue(acquisition_mode_continuous)
# Set the pixel format.
node_pixel_format = PySpin.CEnumerationPtr(self._nodemap.GetNode('PixelFormat'))
if is_monochrome:
# Enable Mono8 mode.
node_pixel_format_mono8 = PySpin.CEnumEntryPtr(
node_pixel_format.GetEntryByName('Mono8'))
pixel_format_mono8 = node_pixel_format_mono8.GetValue()
node_pixel_format.SetIntValue(pixel_format_mono8)
else:
# Enable BGR8 mode.
node_pixel_format_bgr8 = PySpin.CEnumEntryPtr(
node_pixel_format.GetEntryByName('BGR8'))
pixel_format_bgr8 = node_pixel_format_bgr8.GetValue()
node_pixel_format.SetIntValue(pixel_format_bgr8)
# Set the vertical binning radius.
# The horizontal binning radius is automatically set to the same value.
node_binning_vertical = PySpin.CIntegerPtr(self._nodemap.GetNode(
'BinningVertical'))
node_binning_vertical.SetValue(binning_radius)
# Set the ROI.
x, y, w, h = roi
node_offset_x = PySpin.CIntegerPtr(self._nodemap.GetNode('OffsetX'))
node_offset_x.SetValue(int(x))
node_offset_y = PySpin.CIntegerPtr(self._nodemap.GetNode('OffsetY'))
node_offset_y.SetValue(int(y))
node_width = PySpin.CIntegerPtr(self._nodemap.GetNode('Width'))
node_width.SetValue(int(w))
node_height = PySpin.CIntegerPtr(self._nodemap.GetNode('Height'))
node_height.SetValue(int(h))
self._camera.BeginAcquisition()
def get(self, propId):
if propId == cv2.CAP_PROP_FRAME_WIDTH:
node_width = PySpin.CIntegerPtr(self._nodemap.GetNode('Width'))
return float(node_width.GetValue())
if propId == cv2.CAP_PROP_FRAME_HEIGHT:
node_height = PySpin.CIntegerPtr(self._nodemap.GetNode('Height'))
return float(node_height.GetValue())
if propId == cv2.CAP_PROP_GAIN:
node_gain = PySpin.CFloatPtr(self._nodemap.GetNode('Gain'))
return node_gain.GetValue()
return 0.0
def set(self, propId, value):
if propId == cv2.CAP_PROP_FRAME_WIDTH:
node_width = PySpin.CIntegerPtr(self._nodemap.GetNode('Width'))
node_width.SetValue(int(value))
return True
if propId == cv2.CAP_PROP_FRAME_HEIGHT:
node_height = PySpin.CIntegerPtr(self._nodemap.GetNode('Height'))
node_height.SetValue(int(value))
return True
if propId == cv2.CAP_PROP_GAIN:
node_gain = PySpin.CFloatPtr(self._nodemap.GetNode('Gain'))
node_gain.SetValue(value)
return True
return False
def __del__(self):
self.release()
def read(self, image=None):
camera_image = self._camera.GetNextImage()
if camera_image.IsIncomplete():
return False, None
h = camera_image.GetHeight()
w = camera_image.GetWidth()
num_channels = camera_image.GetNumChannels()
if num_channels > 1:
camera_image_data = camera_image.GetData().reshape(h, w, num_channels)
else:
camera_image_data = camera_image.GetData().reshape(h, w)
if image is None:
image = camera_image_data.copy()
else:
image[:] = camera_image_data
camera_image.Release()
return True, image
def release(self):
self._camera.EndAcquisition()
self._camera.DeInit()
del self._camera
self._camera_list.Clear()
self._system.ReleaseInstance()