- Joined
- Apr 17, 2021
- Messages
- 30
- Reaction score
- 16
...threading
Basically the OpenCV GUI *has* to run on the main thread, and does not share well. So the trick is to do the drone control on a separate worker thread:
(Using my new tello-asyncio library here, but the principle would be the same for DJITelloPy)

Basically the OpenCV GUI *has* to run on the main thread, and does not share well. So the trick is to do the drone control on a separate worker thread:
(Using my new tello-asyncio library here, but the principle would be the same for DJITelloPy)
Python:
#!/usr/bin/env python3
import asyncio
from threading import Thread
import cv2 # requires python-opencv
from tello_asyncio import Tello, VIDEO_URL
##############################################################################
# drone control in worker thread
def fly():
async def main():
drone = Tello()
try:
await drone.connect()
await drone.start_video()
await drone.takeoff()
await drone.turn_clockwise(360)
await drone.land()
finally:
await drone.stop_video()
await drone.disconnect()
asyncio.run(main())
fly_thread = Thread(target=fly, daemon=True)
fly_thread.start()
##############################################################################
# Video capture and GUI in main thread
capture = cv2.VideoCapture(VIDEO_URL)
capture.open(VIDEO_URL)
while True:
grabbed, frame = capture.read()
if grabbed:
cv2.imshow('tello-asyncio', frame)
if cv2.waitKey(1) != -1:
break
capture.release()
cv2.destroyAllWindows()