-
Notifications
You must be signed in to change notification settings - Fork 3
/
ptzcontroller.py
368 lines (310 loc) · 12 KB
/
ptzcontroller.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import cv2, time, argparse, time
import pyopenpose as op
import paho.mqtt.client as mqtt
from enum import IntEnum
from signal import signal, SIGINT
from sys import exit
from movecontrol import Move, ViscaMoveControl
from videosource import CaptureVideoSource, NDIVideoSource
class Edge(IntEnum):
LEFT = 0
TOP = 1
RIGHT = 2
BOTTOM = 3
class PTZTrack:
control_camera = None
def __init__(self, args):
self.args = args
self.move = ViscaMoveControl(self.args)
# OpenPose Setup
params = dict()
params["model_folder"] = "/openpose/models"
params["net_resolution"] = self.args.net_resolution
self.openpose = op.WrapperPython()
self.openpose.configure(params)
self.openpose.start()
# Video Source Setup
if self.args.video_source == "device":
self.video_source = CaptureVideoSource(args)
elif self.args.video_source == "ndi":
self.video_source = NDIVideoSource(args)
else:
print("Invalid video source provided.")
exit()
# MQTT Setup
if self.args.mqtt:
self.mqttc = mqtt.Client("PTZTrack")
self.mqttc.connect(self.args.mqtt_host)
self.mqttc.loop_start()
self.mqttc.subscribe("PTZ_SETSTATE")
self.mqttc.on_message = self.mqtt_message
self.mqtt_publish_state()
print("MQTT Connected")
else:
print("Skipping MQTT Connection")
signal(SIGINT, self.sigint_handler)
def mqtt_message(self, client, userdata, message):
data = message.payload.decode("utf-8")
print("Control message:", data)
if data.startswith("control state"):
self.mqtt_publish_state()
elif data.startswith("control on"):
self.control_camera = True
self.mqtt_publish_state()
elif data.startswith("control off"):
self.control_camera = False
self.mqtt_publish_state()
elif data.startswith("control toggle"):
self.control_camera = not self.control_camera
self.mqtt_publish_state()
def calculate_speed(self, smin: int, val: int, smax: int):
speed_ratio = (val - smin) / smax
speed = int(
((self.args.speed_max - self.args.speed_min) * speed_ratio)
+ self.args.speed_min
)
return speed
def move_state_str(self):
return "on" if self.control_camera else "off"
def get_keypoints_rectangle(self, keypoints, threshold=0.2):
numberKeypoints = keypoints.shape[0]
if numberKeypoints < 1:
return "Number body parts must be > 0."
minX = minY = float("inf")
maxX = maxY = float("-inf")
for keypoint in keypoints:
score = keypoint[2]
if score > threshold:
x = keypoint[0]
y = keypoint[1]
if maxX < x:
maxX = x
if minX > x:
minX = x
if maxY < y:
maxY = y
if minY > y:
minY = y
if maxX >= minX and maxY >= minY:
return int(minX), int(minY), int(maxX), int(maxY)
def sigint_handler(self, signal_received, frame):
print("Program exit requested... Exiting gracefully")
self.control_camera = False
if self.args.mqtt:
self.mqtt_publish_state()
time.sleep(0.1)
self.mqttc.loop_stop()
self.video_source.close()
cv2.destroyAllWindows()
exit(0)
def read_frame(self):
ret, frame = self.video_source.frame_read()
if ret and (frame.shape[0] != 720) and (frame.shape[1] != 1280):
frame = cv2.resize(frame, (1280, 720))
return ret, frame
def mqtt_publish_state(self):
self.mqttc.publish("PTZ_STATE", self.move_state_str())
def show_ui(self, frame):
cv2.imshow("PTZTrack Frame", frame)
if cv2.waitKey(25) & 0xFF == ord("q"):
return True
def calculate_edges(self, frame_shape):
l_edge = int(frame_shape[1] * self.args.boundary)
r_edge = frame_shape[1] - l_edge
height = frame_shape[0]
width = frame_shape[1]
bounding = [frame_shape[1], height, 0, 0]
return l_edge, r_edge, height, width, bounding
def process_datum_keypoints(self, frame, datum):
regions = []
if datum.poseKeypoints:
for i in range(0, datum.poseKeypoints.shape[0]):
p = self.get_keypoints_rectangle(datum.poseKeypoints[i], 0.1)
regions.append([p[0], p[1], p[2] - p[0], p[3] - p[1]])
cv2.rectangle(frame, (p[0], p[1]), (p[2], p[3]), (0, 255, 255), 2)
return frame, regions
def calculate_boundaries(self, bounding, regions):
for (x, y, w, h) in regions:
if x < bounding[Edge.LEFT]:
bounding[Edge.LEFT] = x
if y < bounding[Edge.TOP]:
bounding[Edge.TOP] = y
if x + w > bounding[Edge.RIGHT]:
bounding[Edge.RIGHT] = x + w
if y + h > bounding[Edge.BOTTOM]:
bounding[Edge.BOTTOM] = y + h
return bounding
def main_loop(self):
bounding = []
self.move.set_direction(Move.STOP)
last_direction = self.move.direction
self.move.set_speed(self.args.speed_min)
last_speed = self.move.speed
frame_count = 0
while self.video_source.source_available():
check, frame = self.read_frame()
# If there is no video data
if not check:
# sleep momentarily so we can't waste time in an endless loop
time.sleep(0.01)
continue
# Publish control state occasionally
frame_count += 1
if self.args.mqtt and frame_count == 20:
self.mqtt_publish_state()
frame_count = 0
# Don't spend time processing if we're not going to control the camera
if not self.control_camera:
continue
l_edge, r_edge, height, width, bounding = self.calculate_edges(frame.shape)
# Pass the frame data to openpose
openpose_datum = op.Datum()
openpose_datum.cvInputData = frame
self.openpose.emplaceAndPop(op.VectorDatum([openpose_datum]))
frame = openpose_datum.cvOutputData
# Actually get openpose to process the keypoints
frame, regions = self.process_datum_keypoints(frame, openpose_datum)
if len(regions) > 0:
# calculate the bounding boxes of all the people
bounding = self.calculate_boundaries(bounding, regions)
# Calculate the middle of the box
lrmiddle = int(
((bounding[Edge.RIGHT] - bounding[Edge.LEFT]) / 2)
+ bounding[Edge.LEFT]
)
udmiddle = int(
((bounding[Edge.BOTTOM] - bounding[Edge.TOP]) / 2)
+ bounding[Edge.TOP]
)
# Draw the bounding boxes and middle point
cv2.rectangle(
frame,
(bounding[Edge.LEFT], bounding[Edge.TOP]),
(bounding[Edge.RIGHT], bounding[Edge.BOTTOM]),
(0, 200, 0),
2,
)
cv2.rectangle(
frame,
(lrmiddle - 1, udmiddle - 1),
(lrmiddle + 1, udmiddle + 1),
(255, 255, 0),
4,
)
cv2.rectangle(frame, (l_edge, 0), (r_edge, height), (255, 0, 0), 4)
# Calculate the move speed as a ratio of the distance between the bounding box edge and frame edge.
if lrmiddle < l_edge:
self.move.set_speed(
self.calculate_speed(0, l_edge - lrmiddle, l_edge)
)
self.move.set_direction(Move.LEFT)
elif lrmiddle > r_edge:
self.move.set_speed(self.calculate_speed(r_edge, lrmiddle, width))
self.move.set_direction(Move.RIGHT)
else:
self.move.set_speed(self.args.speed_min)
self.move.set_direction(Move.STOP)
else:
self.move.set_direction(Move.STOP)
# If either the speed or direction have changed then send the move command to the camera
if self.move.direction != last_direction or self.move.speed != last_speed:
if self.control_camera:
self.move.do_move()
last_direction = self.move.direction
last_speed = self.move.speed
# Showing the output Image
if self.args.ui:
if self.show_ui(frame):
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Automatic control of a PTZ camera using image recognition"
)
parser.add_argument(
"visca_ip",
help="IP address of the VISCA interface for the camera to be controlled. Required.",
)
parser.add_argument(
"-p",
"--visca_port",
default=52381,
help="Port number of the VISCA interface for the camera to be controlled (default: %(default)s)",
)
parser.add_argument(
"-m",
"--mqtt",
action="store_true",
help="Enable remote control over MQTT (default: %(default)s)",
)
parser.add_argument(
"--mqtt_host",
default="127.0.0.1",
help="Hostname or IP of the MQTT Broker (default: %(default)s)",
)
parser.add_argument(
"-c",
"--control",
action="store_true",
help="If provided and MQTT is enabled, will start controlling the camera on launch, otherwise program will wait for a control command over MQTT. (default: %(default)s)",
)
parser.add_argument(
"-b",
"--boundary",
default=0.35,
type=float,
help="Width of the target box to keep the tracked person inside, as a percentage of screen width (default: %(default)s)",
)
parser.add_argument(
"-s",
"--speed_min",
default=1,
type=int,
help="Minimum speed to move the camera at. Not more than speed_max, min 1. (default: %(default)s)",
)
parser.add_argument(
"-S",
"--speed_max",
default=12,
type=int,
help="Maximum speed to move the camera at. Not less than speed_min, max 24. (default: %(default)s)",
)
parser.add_argument(
"--net_resolution",
default="-1x128",
help="Argument for net_resolution passed directly to OpenCV (default: %(default)s)",
)
parser.add_argument(
"--ui",
action="store_true",
help="If provided, display a UI interface visualising the processing (default: %(default)s)",
)
parser.add_argument(
"-v",
"--video_source",
default="device",
help="Type of video input source to use. 'device' for web camera/v4l2, or 'ndi' for NDI input. (default: %(default)s)",
)
parser.add_argument(
"--video_device",
default=0,
type=int,
help="Video device number to read frames from (default: %(default)s)",
)
parser.add_argument(
"--ndi_source",
default=None,
help="NDI device/source name. Required if using NDI.",
)
parser.add_argument(
"--ndi_extra_ips",
default=None,
help="Device IPs to pass to NDI. Generally required when using docker as multicast packets can't be receieved.",
)
args = parser.parse_args()
ptztrack = PTZTrack(args)
if args.mqtt:
ptztrack.control_camera = args.control
else:
ptztrack.control_camera = True
ptztrack.main_loop()
ptztrack.sigint_handler(None, None) # force tidy exit