-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand_following.py
59 lines (46 loc) · 1.69 KB
/
hand_following.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
"""
Credit for Cansik's Yolo-hand-detection pretrained models!
https://github.com/cansik/yolo-hand-detection.git
"""
import djitellopy as tello
import cv2
import numpy as np
from yolo_hand_detector.yolo import YOLO
path = "/home/muyejia1202/ComputerVision/project/yolo_hand_detector/"
yolo_model = YOLO(path + "models/cross-hands-tiny-prn.cfg", path + "models/cross-hands-tiny-prn.weights", ["hand"])
yolo_model.size = 416
yolo_model.confidence = 0.5
hand_count = 1
def hand_detector(img, vis=True):
"""
Output:
cx: x location of hand's center
cy: y location of hand's center
confidence: how confident the detector is about the result
"""
width, height, inference_time, results = yolo_model.inference(img)
if len(results) > 0:
id, name, confidence, x, y, w, h = results[0]
cx = x + (w / 2)
cy = y + (h / 2)
if vis:
# draw a bounding box rectangle and label on the image
color = (0, 255, 255)
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
text = "%s (%s)" % (name, round(confidence, 2))
cv2.putText(img, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,
0.5, color, 2)
cv2.imshow("hand", img)
cv2.waitKey(100)
return cx, cy, confidence, width, height
return -1, -1, -1, -1, -1
if __name__ == "__main__":
drone = tello.Tello()
drone.connect()
print("Drone connected!")
print("remaining battery: " + str(drone.get_battery()))
drone.streamon()
while True:
img = drone.get_frame_read().frame
img = cv2.resize(img, (480, 480))
cx, cy, confidence, w, h = hand_detector(img)