-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcamera.py
139 lines (111 loc) · 3.5 KB
/
camera.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
"""
DeepLabCut Toolbox (deeplabcut.org)
© A. & M. Mathis Labs
Licensed under GNU Lesser General Public License v3.0
"""
import cv2
import time
class CameraError(Exception):
"""
Exception for incorrect use of cameras
"""
pass
class Camera(object):
""" Base camera class. Controls image capture, writing images to video, pose estimation and image display.
Parameters
----------
id : [type]
camera id
exposure : int, optional
exposure time in microseconds, by default None
gain : int, optional
gain value, by default None
rotate : [type], optional
[description], by default None
crop : list, optional
camera cropping parameters: [left, right, top, bottom], by default None
fps : float, optional
frame rate in frames per second, by default None
use_tk_display : bool, optional
flag to use tk image display (if using GUI), by default False
display_resize : float, optional
factor to resize images if using opencv display (display is very slow for large images), by default None
"""
@staticmethod
def arg_restrictions():
""" Returns a dictionary of arguments restrictions for DLCLiveGUI
"""
return {}
def __init__(
self,
id,
resolution=None,
exposure=None,
gain=None,
rotate=None,
crop=None,
fps=None,
use_tk_display=False,
display_resize=1.0,
):
""" Constructor method
"""
self.id = id
self.exposure = exposure
self.gain = gain
self.rotate = rotate
self.crop = [int(c) for c in crop] if crop else None
self.set_im_size(resolution)
self.fps = fps
self.use_tk_display = use_tk_display
self.display_resize = display_resize if display_resize else 1.0
self.next_frame = 0
def set_im_size(self, res):
"""[summary]
Parameters
----------
default : [, optional
[description], by default None
Raises
------
DLCLiveCameraError
throws error if resolution is not set
"""
if not res:
raise CameraError("Resolution is not set!")
self.im_size = (
(int(res[0]), int(res[1]))
if self.crop is None
else (self.crop[3] - self.crop[2], self.crop[1] - self.crop[0])
)
def set_capture_device(self):
""" Sets frame capture device with desired properties
"""
raise NotImplementedError
def get_image_on_time(self):
""" Gets an image from frame capture device at the appropriate time (according to fps).
Returns
-------
`np.ndarray`
image as a numpy array
float
timestamp at which frame was taken, obtained from :func:`time.time`
"""
frame = None
while frame is None:
cur_time = time.time()
if cur_time > self.next_frame:
frame = self.get_image()
timestamp = cur_time
self.next_frame = max(
self.next_frame + 1.0 / self.fps, cur_time + 0.5 / self.fps
)
return frame, timestamp
def get_image(self):
""" Gets image from frame capture device
"""
raise NotImplementedError
def close_capture_device(self):
""" Closes frame capture device
"""
raise NotImplementedError