-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy patharavis.py
128 lines (94 loc) · 2.97 KB
/
aravis.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
"""
DeepLabCut Toolbox (deeplabcut.org)
© A. & M. Mathis Labs
Licensed under GNU Lesser General Public License v3.0
"""
import ctypes
import numpy as np
import time
import gi
gi.require_version("Aravis", "0.6")
from gi.repository import Aravis
import cv2
from dlclivegui.camera import Camera
class AravisCam(Camera):
@staticmethod
def arg_restrictions():
Aravis.update_device_list()
n_cams = Aravis.get_n_devices()
ids = [Aravis.get_device_id(i) for i in range(n_cams)]
return {"id": ids}
def __init__(
self,
id="",
resolution=[720, 540],
exposure=0.005,
gain=0,
rotate=0,
crop=None,
fps=100,
display=True,
display_resize=1.0,
):
super().__init__(
id,
resolution=resolution,
exposure=exposure,
gain=gain,
rotate=rotate,
crop=crop,
fps=fps,
use_tk_display=display,
display_resize=display_resize,
)
def set_capture_device(self):
self.cam = Aravis.Camera.new(self.id)
self.no_auto()
self.set_exposure(self.exposure)
self.set_crop(self.crop)
self.cam.set_frame_rate(self.fps)
self.stream = self.cam.create_stream()
self.stream.push_buffer(Aravis.Buffer.new_allocate(self.cam.get_payload()))
self.cam.start_acquisition()
return True
def no_auto(self):
self.cam.set_exposure_time_auto(0)
self.cam.set_gain_auto(0)
def set_exposure(self, val):
val = 1 if val > 1 else val
val = 0 if val < 0 else val
self.cam.set_exposure_time(val * 1e6)
def set_crop(self, crop):
if crop:
left = crop[0]
width = crop[1] - left
top = crop[3]
height = top - crop[2]
self.cam.set_region(left, top, width, height)
self.im_size = (width, height)
def get_image_on_time(self):
buffer = None
while buffer is None:
buffer = self.stream.try_pop_buffer()
frame = self._convert_image_to_numpy(buffer)
self.stream.push_buffer(buffer)
return frame, time.time()
def _convert_image_to_numpy(self, buffer):
""" from https://github.com/SintefManufacturing/python-aravis """
pixel_format = buffer.get_image_pixel_format()
bits_per_pixel = pixel_format >> 16 & 0xFF
if bits_per_pixel == 8:
INTP = ctypes.POINTER(ctypes.c_uint8)
else:
INTP = ctypes.POINTER(ctypes.c_uint16)
addr = buffer.get_data()
ptr = ctypes.cast(addr, INTP)
frame = np.ctypeslib.as_array(
ptr, (buffer.get_image_height(), buffer.get_image_width())
)
frame = frame.copy()
if frame.ndim < 3:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
return frame
def close_capture_device():
self.cam.stop_acquisition()