-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathids_camera.py
More file actions
233 lines (195 loc) · 8.15 KB
/
ids_camera.py
File metadata and controls
233 lines (195 loc) · 8.15 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
"""
ids_camera.py — Low-level wrapper around ids_peak for the UI-3040CP-M.
Handles device lifecycle, parameter setting, and frame grabbing.
Key design: the DataStream is opened once and reused across stop/start cycles
to avoid GC_ERR_RESOURCE_IN_USE errors.
"""
import ids_peak.ids_peak as ids
import ids_peak_ipl.ids_peak_ipl as ipl
import numpy as np
class IDSCamera:
def __init__(self):
self._device = None
self._data_stream = None
self._node_map = None
self._streaming = False
self._num_buffers = 5
ids.Library.Initialize()
# ------------------------------------------------------------------
# Device open / close
# ------------------------------------------------------------------
def open(self):
dm = ids.DeviceManager.Instance()
dm.Update()
if dm.Devices().empty():
raise RuntimeError("No IDS camera found. Check USB connection.")
self._device = dm.Devices()[0].OpenDevice(ids.DeviceAccessType_Control)
self._node_map = self._device.RemoteDevice().NodeMaps()[0]
def close(self):
self.stop_stream()
self._close_data_stream()
del self._device
self._device = None
self._node_map = None
ids.Library.Close()
# ------------------------------------------------------------------
# Camera info
# ------------------------------------------------------------------
def info(self) -> dict:
nm = self._node_map
return {
"model": nm.FindNode("DeviceModelName").Value(),
"serial": nm.FindNode("DeviceSerialNumber").Value(),
"width": nm.FindNode("Width").Value(),
"height": nm.FindNode("Height").Value(),
}
# ------------------------------------------------------------------
# Parameters (all safe to call while streaming)
# ------------------------------------------------------------------
def set_exposure(self, value_us: float):
node = self._node_map.FindNode("ExposureTime")
value_us = max(node.Minimum(), min(node.Maximum(), value_us))
node.SetValue(value_us)
def get_exposure(self) -> float:
return self._node_map.FindNode("ExposureTime").Value()
def get_exposure_range(self) -> tuple:
node = self._node_map.FindNode("ExposureTime")
return node.Minimum(), node.Maximum()
def set_gain(self, value: float):
node = self._node_map.FindNode("Gain")
value = max(node.Minimum(), min(node.Maximum(), value))
node.SetValue(value)
def get_gain(self) -> float:
return self._node_map.FindNode("Gain").Value()
def get_gain_range(self) -> tuple:
node = self._node_map.FindNode("Gain")
return node.Minimum(), node.Maximum()
def set_roi(self, x: int, y: int, width: int, height: int):
"""
Set hardware ROI.
Stops acquisition, changes ROI, reallocates buffers, restarts.
The data stream object is reused — never reopened.
"""
was_streaming = self._streaming
if was_streaming:
self._stop_acquisition()
nm = self._node_map
def _clamp(val, minimum, maximum, increment):
val = max(minimum, min(maximum, val))
val = minimum + round((val - minimum) / increment) * increment
return int(val)
w_node = nm.FindNode("Width")
h_node = nm.FindNode("Height")
x_node = nm.FindNode("OffsetX")
y_node = nm.FindNode("OffsetY")
x_node.SetValue(0)
y_node.SetValue(0)
w = _clamp(width, w_node.Minimum(), w_node.Maximum(), w_node.Increment())
h = _clamp(height, h_node.Minimum(), h_node.Maximum(), h_node.Increment())
ox = _clamp(x, x_node.Minimum(), x_node.Maximum(), x_node.Increment())
oy = _clamp(y, y_node.Minimum(), y_node.Maximum(), y_node.Increment())
w_node.SetValue(w)
h_node.SetValue(h)
x_node.SetValue(ox)
y_node.SetValue(oy)
# Reallocate buffers for new payload size (ROI changed frame size)
if self._data_stream is not None:
self._revoke_buffers()
self._allocate_buffers()
if was_streaming:
self._start_acquisition()
def get_roi(self) -> dict:
nm = self._node_map
return {
"x": nm.FindNode("OffsetX").Value(),
"y": nm.FindNode("OffsetY").Value(),
"width": nm.FindNode("Width").Value(),
"height": nm.FindNode("Height").Value(),
}
def get_sensor_size(self) -> tuple:
nm = self._node_map
nm.FindNode("OffsetX").SetValue(0)
nm.FindNode("OffsetY").SetValue(0)
return nm.FindNode("Width").Maximum(), nm.FindNode("Height").Maximum()
# ------------------------------------------------------------------
# Streaming public API
# ------------------------------------------------------------------
def start_stream(self, num_buffers: int = 5):
if self._streaming:
return
self._num_buffers = num_buffers
# Open data stream only once
if self._data_stream is None:
ds_list = self._device.DataStreams()
if ds_list.empty():
raise RuntimeError("No data stream available.")
self._data_stream = ds_list[0].OpenDataStream()
self._allocate_buffers()
self._start_acquisition()
def stop_stream(self):
if not self._streaming:
return
self._stop_acquisition()
# Buffers stay allocated — re-queue them for next start_stream
try:
self._data_stream.Flush(ids.DataStreamFlushMode_DiscardAll)
for buf in self._data_stream.AnnouncedBuffers():
try:
self._data_stream.QueueBuffer(buf)
except Exception:
pass
except Exception:
pass
def grab_frame(self, timeout_ms: int = 2000) -> np.ndarray:
buffer = self._data_stream.WaitForFinishedBuffer(timeout_ms)
ipl_img = ipl.Image.CreateFromSizeAndBuffer(
buffer.PixelFormat(),
buffer.BasePtr(),
buffer.Size(),
buffer.Width(),
buffer.Height(),
)
try:
converted = ipl_img.ConvertTo(ipl.PixelFormatName_Mono8)
except Exception:
converted = ipl_img.ConvertTo(ipl.PixelFormatName_BGRa8)
arr = converted.get_numpy_1D().reshape(
buffer.Height(), buffer.Width(), -1).squeeze().copy()
self._data_stream.QueueBuffer(buffer)
return arr
# ------------------------------------------------------------------
# Private helpers
# ------------------------------------------------------------------
def _start_acquisition(self):
self._data_stream.StartAcquisition()
self._node_map.FindNode("TLParamsLocked").SetValue(1)
self._node_map.FindNode("AcquisitionStart").Execute()
self._node_map.FindNode("AcquisitionStart").WaitUntilDone()
self._streaming = True
def _stop_acquisition(self):
try:
self._node_map.FindNode("AcquisitionStop").Execute()
self._node_map.FindNode("AcquisitionStop").WaitUntilDone()
self._node_map.FindNode("TLParamsLocked").SetValue(0)
self._data_stream.StopAcquisition()
except Exception:
pass
self._streaming = False
def _allocate_buffers(self):
payload_size = self._node_map.FindNode("PayloadSize").Value()
for _ in range(self._num_buffers):
buf = self._data_stream.AllocAndAnnounceBuffer(payload_size)
self._data_stream.QueueBuffer(buf)
def _revoke_buffers(self):
try:
self._data_stream.Flush(ids.DataStreamFlushMode_DiscardAll)
for buf in self._data_stream.AnnouncedBuffers():
self._data_stream.RevokeBuffer(buf)
except Exception:
pass
def _close_data_stream(self):
"""Full teardown — call only on camera close."""
if self._data_stream is None:
return
self._revoke_buffers()
self._data_stream = None