Face recognizing drone

Drones are in our todays life. They are affordable and become more and more a part of hobby photographers. More expensive drones are used to record TV shows and movies. But also the police and firefighters are using them for surveillance and rescue missions. In this post, I would like to show you, how you can access and control the 100€ drone „Tello“ with your Computer by using the programming language Python. Furthermore, I will show you how I implemented a simple face recognition algorithm, which makes it possible for the drone to find and follow human faces.

What do you need?

Actually, you just need two things:

If you don’t have a Python environment installed on your computer, I can highly recommend Anaconda, which makes it easy to install and maintain Python and its modules.

How to connect your Tello drone with a computer to access the video stream?

To connect the Tello drone with your computer, you need to install the Python package called „tellopy“ first. The easiest way to do it is by using pip:

pip install tellopy

When this is done, you can try to run the example video_effect.py from the tellopy folder. You will also find it here.
To run this file in the terminal just type:

python video_effect.py

The result should look like that:

If it doesn’t work because the „av“ module or the „cv“ module are missing, you can easily install these with:

conda install av -c conda-forge

pip install opencv-python

When the python code is running, it is time to connect your computers Wi-Fi with the Tello drone. To do that, switch your Tello drone on and search in your computer Wi-Fi settings for a network name like „Tello-xxxxx“. If the video stream from the drone doesn’t show up on your computer screen, then just restart the python code.

How to control the Tello drone with your keyboard?

To implement the keyboard actions to the python code, I decided to use the package „Pygame“, which can be installed with:

pip install Pygame

The code to control the drone with the keyboard is separated in two parts. First, we need to establish a connection to the drone. This is done as follows:

# coding=utf-8
import sys

#------------------Connect to drone----------------
import tellopy

def handler(event, sender, data, **args):
global prev_flight_data
global video_player
drone = sender
data = str(data)

print("Connect..")
drone = tellopy.Tello()
drone.connect()
drone.subscribe(drone.EVENT_FLIGHT_DATA, handler)

speed=50
#--------------------------------------------------

The speed variable is set here and defines later the speed of all drone moves. To control the drone with our keyboard we need keys for yaw, throttle, pitch and roll maneuvers. We keep it simple and follow the standard keyboard layout for computer games, which are usually using the following keys:

„A“ and „D“ for left and right movement,
„W“ and „S“ for forward and backward,
„Q“ and „E“ for left and right turn,
„M“ and „N“ for upward and downward,
„T“ and „L“ for takeoff and landing maneuvers.

The keyboard commands are all implemented in the code below and every key executes another tellopy definition as long it is pressed down. It is important that the window, which will be opened after executing the code, is active (that means you have to click on it once). Otherwise, the keyboard input will not work.

# imports the Pygame library
import pygame

def main():
try:
# initializes Pygame
pygame.init()

# sets the window title
pygame.display.set_caption(u'Keyboard events')

# sets the window size
pygame.display.set_mode((200, 200))

# infinite loop
while True:
# gets a single event from the event queue
event = pygame.event.wait()

# if the 'close' button of the window is pressed
if event.type == pygame.QUIT:
# stops the application
break

# captures the 'KEYDOWN' and 'KEYUP' events
if event.type in (pygame.KEYDOWN, pygame.KEYUP):
# gets the key name
key_name = pygame.key.name(event.key)

# converts to uppercase the key name
key_name = key_name.upper()

# if any key is pressed
if event.type == pygame.KEYDOWN:
# prints on the console the key pressed
print(key_name)

if key_name == "T":
drone.takeoff()

if key_name == "L":
drone.land()

if key_name == "W":
drone.forward(speed)

if key_name == "S":
drone.backward(speed)

if key_name == "D":
drone.right(speed)

if key_name == "A":
drone.left(speed)

if key_name == "Q":
drone.counter_clockwise(speed)

if key_name == "E":
drone.clockwise(speed)

if key_name == "N":
drone.down(speed)

if key_name == "M":
drone.up(speed)

# if any key is released
if event.type == pygame.KEYUP:
# prints on the console the released key
print(" ")
drone.set_yaw(0)
drone.set_throttle(0)
drone.set_pitch(0)
drone.set_roll(0)

except:
print("Error")

# finalizes Pygame
pygame.quit()

if __name__ == '__main__':
main()

You will find the whole code to download here. Now, you should be able to control the Tello drone with your computer keyboard.

How to detect a face with a camera?

I will try to keep this section short, because the face detection algorithm, which I am using is explained in detail at the blog from MJRoBot on Hackster.io. In the following, we will use the face detection algorithm for the computer webcam. This makes it easy, because we don’t need to be connected to the drone to test the algorithm.

The face detection algorithm is based on OpenCV and the Cascade Classifier. For this reason, it is important that the file „haarcascade_frontalface_default.xml“ is loaded:


faceCascade = cv2.CascadeClassifier('/path_to_file/haarcascade_frontalface_default.xml')

Because I wanted to make the drone following anybody’s face autonomously I needed to set some parameters. That means, when the face is not in the center of the picture, the drone has to move to adjust. For this reason I marked the center of the face by a simple cross:


cv2.line(image,(x_pos,int(round(y+h*0.35))), (x_pos,int(round(y+h*0.65))),(0,0,255,10)) #Cross
cv2.line(image,(int(round(x+w*0.35)),y_pos), (int(round(x+w*0.65)),y_pos),(0,0,255,10))

If the cross is passing over a threshold of the picture, then the drone has to react. For example, the drone has to turn to the right if the face is too far to the right in the picture:


elif x_pos < 640/2 - x_offset:
cv2.putText(image,"Turn: right", (5,140), font, 0.5, (70,234,199), 1)

The offset parameter sets a range of pixels (in this case 20 pixels). If the cross is inside this range, the drone doesn’t need to turn at all to avoid constant adjustments.

That the Python code detects very well what the drone has to do to keep the face in the center is show the video below. You will find the whole code including the face recognition algorithm here.

Using Tello to follow faces

In the explanations above you learned how to control the Tello drone with your keyboard and how to detect a face with a camera. Now, you only need to combine these codes. You can do that by yourself or you just download my code, which you can find here. I tested this code successfully on a windows machine. The result is shown in the video a the top of this page. Enjoy flying around with your drone and adapting the code.

Thanks for reading!