-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
81 lines (67 loc) · 2.55 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
from djitellopy import Tello
import cv2
import mediapipe as mp
import tensorflow as tf
import time
from utils import predict_gesture, get_current_landmarks, Action
drone = Tello()
drone.connect()
print(f"BATTERY : {drone.get_battery()}%")
HOLD_TIME_THRESHOLD = 1
model_save_path = "main_model.hdf5"
classFile = 'gesture.names'
model = tf.keras.models.load_model(model_save_path)
with open(classFile, 'rt') as f:
classNames = f.read().split('\n')
print()
print()
print()
print()
print(classNames)
print()
print()
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic
hands = mp_hands.Hands(model_complexity=0, min_detection_confidence=0.7, min_tracking_confidence=0.5)
first_run = True
set_gesture = None
cap = cv2.VideoCapture(0)
time.sleep(3)
drone.takeoff()
while cap.isOpened():
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
fps = int(cap.get(cv2.CAP_PROP_FPS))
cv2.putText(frame, f"FPS: {fps}", (1000, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Detections
results = hands.process(image)
# image back to BGR for rendering
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(image, hand_landmarks, mp_holistic.HAND_CONNECTIONS)
landmarks = get_current_landmarks(image, hand_landmarks)
current_gesture = predict_gesture(model, landmarks)
cv2.putText(image, current_gesture, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2, cv2.LINE_AA)
if first_run:
set_gesture = current_gesture
hold_start_time = time.time()
prev_gesture = current_gesture
first_run = False
if current_gesture != prev_gesture:
hold_start_time = time.time()
prev_gesture = current_gesture
if time.time() - hold_start_time > HOLD_TIME_THRESHOLD:
set_gesture = current_gesture
if set_gesture != current_gesture:
print(f"Next Change: Hold {current_gesture} for {round(HOLD_TIME_THRESHOLD - (time.time() - hold_start_time), 2)}s")
print(f"label: {set_gesture}\n")
Action(set_gesture, drone)
cv2.imshow('Webcam Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
# Clean up
# tello.streamoff()
cv2.destroyAllWindows()