-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
125 lines (94 loc) · 4.1 KB
/
run.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
import argparse
import logging
import cv2
from flask import Flask, Response, render_template
from trackstormsbot.camera import CameraStream
from trackstormsbot.controller import DistanceSensorController, MotorController
from trackstormsbot.detectors import *
from trackstormsbot.gesture_recognisers import *
from trackstormsbot.utils import *
log = logging.getLogger(__name__)
app = Flask(__name__, template_folder='trackstormsbot/templates')
DETECTOR_MAP = {
'haarcascade': CascadeDetector,
'yunet': YuNetDetector,
'mediapipe': MediapipeDetector,}
def get_args():
parser = argparse.ArgumentParser('Trackstormsbot')
parser.add_argument('-c', '--camera', type=int, default=0, help='Camera index')
parser.add_argument('-d',
'--detector',
type=str,
default='yunet',
choices=DETECTOR_MAP.keys(),
help='Name of detector model to use')
parser.add_argument('--motor_ports', type=str, nargs=2, default=['A', 'B'], help='Motor Ports (X-axis, Y-axis)')
parser.add_argument('--distance_port', type=str, default='C', help='Distance Sensor Port')
parser.add_argument('--disable_controller', action='store_true', help='Disable controller')
parser.add_argument('--detector_rate', type=int, default=30, help='Detector rate (FPS)')
parser.add_argument('--gesture_rate', type=int, default=30, help='Gesture recogniser rate (FPS)')
return parser.parse_args()
def get_detector(detector_name, camera, rate, **kwargs):
detector_class = DETECTOR_MAP[detector_name]
return detector_class(camera, rate, **kwargs)
def video_stream():
args = get_args()
camera = CameraStream(args.camera)
detector = get_detector(args.detector, camera, args.detector_rate)
gesture_recogniser = MediapipeRecogniser(camera, args.gesture_rate)
controller = MotorController(args.motor_ports)
distance_sensor = DistanceSensorController(args.distance_port)
camera.open()
gesture_recogniser.start()
detector.start()
frame_size = camera.size()
frame_middle = (frame_size[0] // 2, frame_size[1] // 2)
while (camera.is_opened()):
frame = camera.read()
if frame is None:
continue
detections, _ = detector.read()
gesture_detections, gesture = gesture_recogniser.read()
if len(detections) > 0:
detection = detections[0]
else:
detection = None
frame_vis = camera.read()
if not args.disable_controller:
if detection is not None:
relative_size = detection[2] * detection[3] / (frame_size[0] * frame_size[1])
middle = calculate_middle_xywh(detection)
frame_vis = visualise_detection(frame_vis, detection)
frame_vis = visualise_landmarks(frame_vis, gesture_detections)
controller.move_to_middle(
frame_middle=frame_middle,
detection_middle=middle,
detection_size=relative_size,
)
else:
controller.stop()
if gesture == 'point':
distance_sensor.set_eyes(100, 100, 100, 100)
else:
distance_sensor.set_eyes(0, 0, 0, 0)
stats = {
'FPS (Camera)': int(camera.fps()),
'FPS (Detector)': int(detector.fps()),
'FPS (Gesture)': int(gesture_recogniser.fps()),
'Distance (cm)': distance_sensor.get_distance(),
'Gesture': gesture,}
frame_vis = visualise_stats(frame_vis, stats)
ret, buffer = cv2.imencode('.jpeg', frame_vis)
frame_vis = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-type: image/jpeg\r\n\r\n' + frame_vis + b'\r\n')
camera.close()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(video_stream(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
app.run(host='0.0.0.0', debug=True)