forked from kinivi/tello-gesture-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (126 loc) · 4.44 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configargparse
import cv2 as cv
from gestures.tello_gesture_controller import TelloGestureController
from utils import CvFpsCalc
from djitellopy import Tello
from gestures import *
import threading
def get_args():
print('## Reading configuration ##')
parser = configargparse.ArgParser(default_config_files=['config.txt'])
parser.add('-c', '--my-config', required=False, is_config_file=True, help='config file path')
parser.add("--device", type=int)
parser.add("--width", help='cap width', type=int)
parser.add("--height", help='cap height', type=int)
parser.add("--is_keyboard", help='To use Keyboard control by default', type=bool)
parser.add('--use_static_image_mode', action='store_true', help='True if running on photos')
parser.add("--min_detection_confidence",
help='min_detection_confidence',
type=float)
parser.add("--min_tracking_confidence",
help='min_tracking_confidence',
type=float)
parser.add("--buffer_len",
help='Length of gesture buffer',
type=int)
args = parser.parse_args()
return args
def select_mode(key, mode):
number = -1
if 48 <= key <= 57: # 0 ~ 9
number = key - 48
if key == 110: # n
mode = 0
if key == 107: # k
mode = 1
if key == 104: # h
mode = 2
return number, mode
def main():
# init global vars
global gesture_buffer
global gesture_id
global battery_status
# Argument parsing
args = get_args()
KEYBOARD_CONTROL = args.is_keyboard
WRITE_CONTROL = False
in_flight = False
# Camera preparation
tello = Tello()
tello.connect()
tello.streamon()
cap = tello.get_frame_read()
# Init Tello Controllers
gesture_controller = TelloGestureController(tello)
keyboard_controller = TelloKeyboardController(tello)
gesture_detector = GestureRecognition(args.use_static_image_mode, args.min_detection_confidence,
args.min_tracking_confidence)
gesture_buffer = GestureBuffer(buffer_len=args.buffer_len)
def tello_control(key, keyboard_controller, gesture_controller):
global gesture_buffer
if KEYBOARD_CONTROL:
keyboard_controller.control(key)
else:
gesture_controller.gesture_control(gesture_buffer)
def tello_battery(tello):
global battery_status
try:
battery_status = tello.get_battery()[:-2]
except:
battery_status = -1
# FPS Measurement
cv_fps_calc = CvFpsCalc(buffer_len=10)
mode = 0
number = -1
battery_status = -1
tello.move_down(20)
while True:
fps = cv_fps_calc.get()
# Process Key (ESC: end)
key = cv.waitKey(1) & 0xff
if key == 27: # ESC
break
elif key == 32: # Space
if not in_flight:
# Take-off drone
tello.takeoff()
in_flight = True
elif in_flight:
# Land tello
tello.land()
in_flight = False
elif key == ord('k'):
mode = 0
KEYBOARD_CONTROL = True
WRITE_CONTROL = False
tello.send_rc_control(0, 0, 0, 0) # Stop moving
elif key == ord('g'):
KEYBOARD_CONTROL = False
elif key == ord('n'):
mode = 1
WRITE_CONTROL = True
KEYBOARD_CONTROL = True
if WRITE_CONTROL:
number = -1
if 48 <= key <= 57: # 0 ~ 9
number = key - 48
# Camera capture
image = cap.frame
debug_image, gesture_id = gesture_detector.recognize(image, number, mode)
gesture_buffer.add_gesture(gesture_id)
# Start control thread
threading.Thread(target=tello_control, args=(key, keyboard_controller, gesture_controller,)).start()
threading.Thread(target=tello_battery, args=(tello,)).start()
debug_image = gesture_detector.draw_info(debug_image, fps, mode, number)
# Battery status and image rendering
cv.putText(debug_image, "Battery: {}".format(battery_status), (5, 720 - 5),
cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.imshow('Tello Gesture Recognition', debug_image)
tello.land()
tello.end()
cv.destroyAllWindows()
if __name__ == '__main__':
main()