In this Tutorial we are going to program a drone to track a face. We will do this using opencv and apply a PID controller to have smooth movements.
First we will create a utilities file in which we will add all the functions. Then we will import all the tello and the cv2 packages.
from djitellopy import Tello
import cv2
Then we will create the tello intitialization function that will setup the tello drone for flight and send commands. We will set all the speed to 0. We have 4 types of speeds
def intializeTello():
# CONNECT TO TELLO
myDrone = Tello()
myDrone.connect()
myDrone.for_back_velocity = 0
myDrone.left_right_velocity = 0
myDrone.up_down_velocity = 0
myDrone.yaw_velocity = 0
myDrone.speed = 0
print(myDrone.get_battery())
myDrone.streamoff()
myDrone.streamon()
return myDrone
Now we will call this function in the main script.
myDrone = intializeTello()
Once we have setup the tello drone we will get the frame/image from it. We will create a simple function for this, that will take the drone object as the input argument and return the current image.
def telloGetFrame(myDrone,w=360,h=240):
# GET THE IMGAE FROM TELLO
myFrame = myDrone.get_frame_read()
myFrame = myFrame.frame
img = cv2.resize(myFrame, (w, h))
return img
Now we will call this function inside a while loop.
while True:
## STEP 1
img = telloGetFrame(myDrone)
# DISPLAY IMAGE
cv2.imshow("MyResult", img)
# WAIT FOR THE 'Q' BUTTON TO STOP
if cv2.waitKey(1) and 0xFF == ord('q'): # replace the 'and' with '&'
myDrone.land()
break
#python #opencv