-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
187 lines (145 loc) · 5.18 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
import sys
from time import sleep
from threading import Lock, Thread
import numpy as np
import cv2
import torch
import pyzed
import pyzed.sl as sl
import yolov7
from yolov7.hubconf import create
from filter import Filter
from InverseKinematics import IK
import servo
BIRD = 14
# BIRD = 0
lock = Lock()
run_signal = False
exit_signal = False
def results_to_detections(results):
detections = []
for det in results.xyxy[0]:
if det[5] == BIRD:
detection = sl.CustomBoxObjectData()
detection.unique_object_id = sl.generate_unique_id()
detection.probability = det[4]
detection.label = det[5]
box = np.zeros((4, 2))
xmin = det[0]
xmax = det[2]
ymin = det[1]
ymax = det[3]
box[0][0] = xmin
box[0][1] = ymin
box[1][0] = xmax
box[1][1] = ymin
box[2][0] = xmin
box[2][1] = ymax
box[3][0] = xmax
box[3][1] = ymax
detection.bounding_box_2d = box
detection.is_grounded = False
detections.append(detection)
return detections
def torch_thread():
global lock, exit_signal, run_signal, cv_img, results
model = yolov7.load('yolov7.pt', device="cuda:0", trace=False, size=1280)
while not exit_signal:
if run_signal:
lock.acquire()
results = model(cv_img)
lock.release()
run_signal = False
sleep(0.01)
def main():
global lock, exit_signal, run_signal, cv_img, results
model_thread = Thread(target=torch_thread)
model_thread.start()
# Create a ZED camera object
zed = sl.Camera()
init_params = sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD720
init_params.camera_fps = 15
init_params.coordinate_units = sl.UNIT.METER
init_params.depth_mode = sl.DEPTH_MODE.ULTRA
init_params.coordinate_system = sl.COORDINATE_SYSTEM.RIGHT_HANDED_Y_UP
init_params.depth_maximum_distance = 20
init_params.sdk_verbose = True
# Open the camera
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
print(repr(err))
exit()
positional_tracking_parameters = sl.PositionalTrackingParameters()
zed.enable_positional_tracking(positional_tracking_parameters)
detection_parameters = sl.ObjectDetectionParameters()
detection_parameters.detection_model = sl.DETECTION_MODEL.CUSTOM_BOX_OBJECTS
detection_parameters.image_sync = True
detection_parameters.enable_tracking = True
detection_parameters.enable_mask_output = True
print("Object Detection: Loading Module...")
err = zed.enable_object_detection(detection_parameters)
if err != sl.ERROR_CODE.SUCCESS:
print("Error {}, exit program".format(err))
zed.close()
exit()
runtime_params = sl.RuntimeParameters()
obj_runtime_param = sl.ObjectDetectionRuntimeParameters()
obj_runtime_param.detection_confidence_threshold = 45
image_left = sl.Mat()
cam_w_pose = sl.Pose()
filter = Filter()
ik = IK()
while zed.grab(runtime_params) == sl.ERROR_CODE.SUCCESS and not exit_signal:
lock.acquire()
zed.retrieve_image(image_left, sl.VIEW.LEFT)
cv_img = image_left.get_data()
lock.release()
run_signal = True
# -- Detection running on the other thread
while run_signal:
sleep(0.001)
# Wait for detections
lock.acquire()
# -- Ingest detections
detections = results_to_detections(results)
zed.ingest_custom_box_objects(detections)
lock.release()
objects = sl.Objects()
err = zed.retrieve_objects(objects, obj_runtime_param)
if err != sl.ERROR_CODE.SUCCESS:
print("Error {}, exit program".format(err))
zed.close()
exit()
target = filter.getTarget(objects)
if target is not None:
# if all target.position are not nan
if not np.isnan(target.position).any():
servo_target = ik.calc(*target.position)
servo.move_servos(servo_target["rotate"], servo_target["tilt"])
# -- Draw detections
for obj in objects.object_list:
# continue if any of the position is nan
if np.isnan(obj.position).any():
continue
color = (0, 255, 0)
if obj.id == target.id:
color = (0, 0, 255)
cv2.rectangle(cv_img, (int(obj.bounding_box_2d[0][0]), int(obj.bounding_box_2d[0][1])), (int(obj.bounding_box_2d[2][0]), int(obj.bounding_box_2d[2][1])), color, 2)
# display text
text = f"{obj.confidence}%"
cv2.putText(cv_img, text, (int(obj.bounding_box_2d[0][0]), int(obj.bounding_box_2d[0][1]) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
cv2.imshow("Robo-Scarecrow", cv_img)
key = cv2.waitKey(10)
if key == 27:
exit_signal = True
break
cv2.destroyAllWindows()
zed.disable_object_detection()
zed.close()
print("Exiting...")
exit()
if __name__ == "__main__":
with torch.no_grad():
main()
# -0.33 0.22 -0.50