-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathobject_tracker.py
212 lines (179 loc) · 6.65 KB
/
object_tracker.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
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
import logging
import time
import arc852.cli_args as cli
import arc852.opencv_utils as utils
import cv2
import imutils
import numpy as np
from arc852.camera import Camera
from arc852.cli_args import setup_cli_args
from arc852.image_server import ImageServer
logger = logging.getLogger(__name__)
BLACK = np.uint8((0, 0, 0))
class ObjectTracker(object):
def __init__(self,
width,
middle_percent,
display,
flip_x,
flip_y,
mask_x,
mask_y,
usb_camera,
usb_port,
camera_name,
http_host,
http_file,
http_delay_secs,
http_verbose):
self.__width = width
self.__middle_percent = middle_percent
self.__orig_width = width
self.__orig_middle_percent = middle_percent
self.__display = display
self.__flip_x = flip_x
self.__flip_y = flip_y
self.__mask_x = mask_x
self.__mask_y = mask_y
self.__filters = None
self.__stopped = False
self.__cnt = 0
self.__cam = Camera(usb_camera=usb_camera, usb_port=usb_port)
self.__image_server = ImageServer(http_file,
camera_name=camera_name,
http_host=http_host,
http_delay_secs=http_delay_secs,
http_verbose=http_verbose)
@property
def width(self):
return self.__width
@width.setter
def width(self, width):
if 200 <= width <= 4000:
self.__width = width
if self.__filters:
for f in self.__filters:
f.reset()
@property
def middle_percent(self):
return self.__middle_percent
@middle_percent.setter
def middle_percent(self, val):
if 2 <= val <= 98:
self.__middle_percent = val
if self.__filters:
for filter in self.__filters:
filter.reset()
@property
def markup_image(self):
return self.__display or self.__image_server.enabled
# Do not run this in a background thread. cv2.waitKey has to run in main thread
def start(self, *filters):
self.__filters = filters
if self.__filters:
for f in self.__filters:
f.start()
self.__image_server.start()
if not self.__cam.is_open():
logger.error("Camera is closed")
while self.__cam.is_open() and not self.__stopped:
try:
image = self.__cam.read()
if image is None:
logger.error("Null image read from camera")
time.sleep(.5)
continue
image = imutils.resize(image, width=self.width)
image = self.flip(image)
# Apply masks
if self.__mask_y != 0:
height, width = image.shape[:2]
mask_height = abs(int((self.__mask_y / 100.0) * height))
if self.__mask_y < 0:
image[0: mask_height, 0: width] = BLACK
else:
image[height - mask_height: height, 0: width] = BLACK
if self.__mask_x != 0:
height, width = image.shape[:2]
mask_width = abs(int((self.__mask_x / 100.0) * width))
if self.__mask_x < 0:
image[0: height, 0: mask_width] = BLACK
else:
image[0: height, width - mask_width: width] = BLACK
if self.__filters:
for f in self.__filters:
f.process_image(image)
for f in self.__filters:
if f.predicate:
f.predicate(f)
f.publish_data()
f.markup_image(image)
self.__image_server.image = image
if self.__display:
self.display_image(image)
self.__cnt += 1
except KeyboardInterrupt as e:
raise e
except BaseException as e:
logger.error("Unexpected error in main loop [{0}]".format(e), exc_info=True)
time.sleep(1)
self.__cam.close()
def stop(self):
self.__stopped = True
if self.__filters:
for f in self.__filters:
f.stop()
self.__image_server.stop()
def display_image(self, image):
cv2.imshow("Image", image)
key = cv2.waitKey(1) & 0xFF
if key == 255:
pass
elif key == ord("w"):
self.width -= 10
elif key == ord("W"):
self.width += 10
elif key == ord("-") or key == ord("_") or key == 0:
self.middle_percent -= 1
elif key == ord("+") or key == ord("=") or key == 1:
self.middle_percent += 1
elif key == ord("r"):
self.width = self.__orig_width
self.middle_percent = self.__orig_middle_percent
elif key == ord("s"):
utils.write_image(image, log_info=True)
elif key == ord("q"):
self.stop()
def flip(self, image):
img = image
if self.__flip_x:
img = cv2.flip(img, 0)
if self.__flip_y:
img = cv2.flip(img, 1)
return img
@staticmethod
def cli_args():
return setup_cli_args(cli.bgr,
cli.usb_camera,
cli.usb_port,
cli.width,
cli.middle_percent,
cli.minimum_pixels,
cli.hsv_range,
cli.grpc_port,
cli.leds,
cli.flip_x,
cli.flip_y,
cli.mask_x,
cli.mask_y,
cli.vertical_lines,
cli.horizontal_lines,
cli.camera_name_optional,
cli.display,
cli.draw_contour,
cli.draw_box,
cli.http_host,
cli.http_file,
cli.http_delay_secs,
cli.http_verbose,
cli.log_level)