-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
196 lines (164 loc) · 6.63 KB
/
main.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
import time
import cv2
import fire
from detect import detect_hostiles
from play_sound import get_sound, play_sound
try:
from stepper_control_thread import StepperThread
from gpio_control import Pin
except:
from dummy_stepper_control_thread import StepperThread
from dummy_gpio_control import Pin
from video_get import VideoGet
STEP_X_PIN = 12
DIR_X_PIN = 16
DIR_Y_PIN = 40
STEP_Y_PIN = 38
ENABLE_X_PIN = 8
ENABLE_Y_PIN = 36
AIR_PIN = 37
def draw_hostile_box(frame, target, radius):
cv2.rectangle(frame, (target[0] - radius // 2, target[1] - radius // 2),
(target[0] + radius // 2, target[1] + radius // 2), (0, 0, 255), 2)
cv2.putText(frame, "Hostile Detected",
(target[0] - 5 - radius // 2, target[1] - 15 - radius // 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
x_stepper = None
y_stepper = None
def sentry(dry_run=False, verbose=False, display_frame=False, display_mask=False, min_radius=50, scale=0, output_scale=0, hostile_output=False, shoot_box_x=50, shoot_box_y=50, sleep_time=0.0025, sound=False):
'''
The main sentry loop.
'''
video_getter = VideoGet(0).start()
frame = None
while frame is None:
frame = video_getter.frame
# get the frame height and width
height, width = frame.shape[:2]
# get the center
center_x = width // 2
center_y = height // 2
shoot_range_x = range(center_x - shoot_box_x, center_x + shoot_box_x + 1)
shoot_range_y = range(center_y - shoot_box_y, center_y + shoot_box_y + 1)
deadzone_range_x = range(center_x - shoot_box_x //
2, center_x + shoot_box_x // 2 + 1)
deadzone_range_y = range(center_y - shoot_box_y //
2, center_y + shoot_box_y // 2 + 1)
if scale:
height = int(height * scale)
width = int(width * scale)
center_x = width // 2
center_y = height // 2
shoot_range_x = range(center_x - shoot_box_x,
center_x + shoot_box_x + 1)
shoot_range_y = range(center_y - shoot_box_y,
center_y + shoot_box_y + 1)
deadzone_range_x = range(center_x - shoot_box_x //
2, center_x + shoot_box_x // 2 + 1)
deadzone_range_y = range(center_y - shoot_box_y //
2, center_y + shoot_box_y // 2 + 1)
hostile_count = 0
print("Video width: ", width)
print("Video height: ", height)
print("Center x: ", center_x)
print("Center y: ", center_y)
print("Shoot Box X: ", shoot_box_x)
print("Shoot Box Y: ", shoot_box_y)
print("Shoot range x: ", shoot_range_x)
print("Shoot range y: ", shoot_range_y)
print("Deadzone range x: ", deadzone_range_x)
print("Deadzone range y: ", deadzone_range_y)
print("Min radius: ", min_radius)
if not dry_run:
x_stepper = StepperThread(
step_pin=STEP_X_PIN, direction_pin=DIR_X_PIN, enable_pin=ENABLE_X_PIN, sleep_time=sleep_time).start()
y_stepper = StepperThread(step_pin=STEP_Y_PIN,
direction_pin=DIR_Y_PIN, enable_pin=ENABLE_Y_PIN, sleep_time=sleep_time).start()
shoot_pin = Pin(pin=AIR_PIN)
shoot_pin.setup()
sound_gap = 5
last_sound = 0
if sound:
play_sound(get_sound('turret_search'))
last_sound = time.time()
try:
while True:
frame = video_getter.frame
if scale:
# scale the image
frame = cv2.resize(
frame, (int(width * scale), int(height * scale)))
start = time.time()
x, y, r, mask = detect_hostiles(frame)
end = time.time()
if verbose:
print("Detect time: ", end - start)
print(x, y, r)
if r >= min_radius and not x in deadzone_range_x and not y in deadzone_range_y:
draw_hostile_box(frame, (int(x), int(y)), int(r))
if hostile_output:
hostile_count += 1
cv2.imwrite(f"hostile_{hostile_count}.jpg", frame)
if x > center_x:
if not dry_run:
if verbose:
print("Moving right")
x_stepper.set_direction(direction=1)
if verbose:
print("Hostile is to the right")
elif x < center_x:
if not dry_run:
if verbose:
print("Moving left")
x_stepper.set_direction(direction=0)
if verbose:
print("Hostile is to the left")
if y > center_y:
if not dry_run:
if verbose:
print("Moving down")
y_stepper.set_direction(direction=1)
if verbose:
print("Hostile is below")
elif y < center_y:
if not dry_run:
if verbose:
print("Moving up")
y_stepper.set_direction(direction=0)
if verbose:
print("Hostile is above")
else:
if not dry_run:
x_stepper.stop()
y_stepper.stop()
if r >= min_radius and x in shoot_range_x and y in shoot_range_y:
if not dry_run:
shoot_pin.on()
if verbose:
print("Shooting")
if sound:
if time.time() > last_sound + 2:
play_sound(get_sound('turret_deploy'))
last_sound = time.time()
else:
if not dry_run:
shoot_pin.off()
if sound:
if time.time() > last_sound + sound_gap:
play_sound(get_sound('turret_retire'))
last_sound = time.time()
if display_mask:
cv2.imshow("Mask", mask)
elif display_frame:
if output_scale:
frame = cv2.resize(
frame, (int(width * output_scale), int(height * output_scale)))
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
video_getter.stop()
if not dry_run:
x_stepper.cleanup()
y_stepper.cleanup()
if __name__ == "__main__":
fire.Fire(sentry)