Share via


Azure Cloud Services for Tello Drones: How to Send Recorded Videos to Azure Blob Storage

Introduction

Tello is a programmable mini drone, which is perfect and popular for beginners. Users can easily control it by programming languages such as Scratch, Python, and Swift. Microsoft Azure provides a variety of cloud computing services including artificial intelligence, machine learning, IoT, storage, security, networking, media, integration and so on. Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data, for example, images, documents, video and audio files.
In this article, we will walk you through the steps required to record and send videos from Tello to Azure Blob Storage with Azure Storage Blobs client library for Python.

Prerequisites

  1. Tello Drone.
  2. Azure Storage Blobs client library for Python
  3. IDE: PyCharm Community
  4. Azure subscription

Network Access for PC

Since Tello is connected to the PC by Wi-Fi, it is straightforward to know that our PC should equipped with two network interface cards, one is for connecting with Tello, and the other one is for connecting with Internet. 

Fig. 1 System Overview

Azure Blob Storage

Azure Blob Storage is Microsoft's object storage solution for the cloud. Blob Storage is optimized for storing massive amounts of unstructured data. It is designed for:

  • Serving images or documents directly to a browser.
  • Storing files for distributed access.
  • Streaming video and audio.
  • Writing to log files.
  • Storing data for backup and restore, disaster recovery, and archiving.
  • Storing data for analysis by an on-premises or Azure-hosted service.

In this project, we make use of Azure Blob Storage to store the video captured by the Tello.

Create an Azure Storage Account

We can create an Azure Storage Account using the Azure Portal. Please refer to the “Create a storage account” section on this page: Create a storage account. Then, in order to send the files to Azure Storage, we should get the connection string. The “View account access keys” section is available on this page Manage storage account access keys to help you with this. This connection string will be used by Python code to communicate with the Azure Storage.

Install Python Packages

In this project, we will install “djitellopy”, “azure-storage-blob” and “Pygame” packages to accelerate the development. Please refer to the “Install Python Azure IoT SDK and Tello Python SDK” section of the article “Azure Cloud Services for Tello Drones: How to send telemetry to Azure IoTHub” to complete this step. 

Create and Debug Python Code on Your PC

Copy and paste the following code to your PyCharm project.

from djitellopy import tello
import KeyPressModule as kp
import time
import cv2
from azure.storage.blob import ContentSettings, BlobClient
 
kp.init()
me =  tello.Tello()
me.connect()
print(me.get_battery())
me.streamon()
 
conn_str =  "your connection string"
container_name =  "raspberrypic"
blob_name =  "tellovideo.mp4"
 
vid_cod =  cv2.VideoWriter_fourcc(*'XVID')
vid_output =  cv2.VideoWriter("Resources/Videos/cam_video.mp4", vid_cod, 20.0, (640,480))
 
 
def getKeyboardInput():
    lr, fb, ud, yv =  0, 0, 0, 0
    speed =  50
 
    if kp.getKey("LEFT"):
        lr =  -speed
    elif kp.getKey("RIGHT"):
        lr =  speed
 
    if kp.getKey("UP"):
        fb =  speed
    elif kp.getKey("DOWN"):
        fb =  -speed
 
    if kp.getKey("w"):
        ud =  speed
    elif kp.getKey("s"):
        ud =  -speed
 
    if kp.getKey("a"):
        yv =  speed
    elif kp.getKey("d"):
        yv =  -speed
 
    if kp.getKey("q"): me.land()
    if kp.getKey("e"): me.takeoff()
 
    if kp.getKey("z"):
        # stop recording
        global vid_output
        vid_output.release()
        # upload the image to Azure Blob Storage, Overwrite if it already exists!
        blob =  BlobClient.from_connection_string(conn_str, container_name, blob_name)
        image_content_setting =  ContentSettings(content_type='video/mp4')
        with open(f'Resources/Videos/cam_video.mp4', "rb") as data:
            try:
                blob.upload_blob(data, overwrite=True, content_settings=image_content_setting)
                print("Blob storage uploading completed")
            except ValueError:
                print("Blob storage uploading error")
        # start new recording
        global vid_cod
        vid_output =  cv2.VideoWriter("Resources/Videos/cam_video.mp4", vid_cod, 20.0, (640, 480))
 
    return [lr, fb, ud, yv]
 
 
def main():
    print("Capture and send Tello video to Azure Blob Storage")
    while True:
        start_time =  time.time()
        print("battery: " +  str(me.get_battery()))
 
        vals =  getKeyboardInput()
        me.send_rc_control(vals[0], vals[1], vals[2], vals[3])
 
        try:
            img =  me.get_frame_read().frame
            img =  cv2.resize(img, (640, 480))
 
            if (time.time() - start_time) > 0:
                fpsInfo =  "FPS: " +  str(1.0  / (time.time() - start_time))  # FPS = 1 / time to process loop
                font =  cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(img, fpsInfo, (10, 20), font,  0.4, (255, 255,  255), 1)
 
            cv2.imshow('DJI Tello Camera', img)
            global vid_output
            vid_output.write(img)
        except Exception as e:
            print(f'exc: {e}')
            pass
 
        if cv2.waitKey(1) & 0xFF  ==  ord('q'):
            break
 
    # close the already opened camera, and the video file
    vid_output.release()
    cv2.destroyAllWindows()
 
 
if __name__ == "__main__":
    main()

In this Python application, we define a function getKeyboardInput to receive the keyboard input of the user, which return the control parameters to the loop in main function. It is quite the same as we designed in this article “Azure Cloud Services for Tello Drones: How to Control Tello by Azure C2D Messages”. 
To achieve the video recording, we first set the saved path of the video in the folder “Resources/Videos/” of the project. When we input “z” from keyboard, the video will be stopped recording. Then, a BlobClient is created to send the video to the Azure Storage by its upload_blob method.
Please do substitute the connection string with yours that created before. Then, power on the Tello, connect your PC with Tello by Wi-Fi. You will notice that the LED on the Tello will flash quickly with yellow color. Press Run or Debug button to start the process. You will see the information in output window as shown in Fig. 2.

Fig. 2 Realtime Debug Information

After a few seconds, the real time image that streamed from the Tello, as well as the Pygame window will be shown on the screen, as presented in Fig. 3. We calculate the FPS (frame per second) in the loop of the main function. And it is marked on the top left of the image.

Fig. 3 Real time image and Pygame window

Then, we can click the mouse on the Pygame window to focus user input on it. After that, we can use “w, s, a, d, e, q, up arrow, down arrow, left arrow, right arrow” to control the movement of the Tello drone. Once the “z” is pressed, we first manage to stop video recording to the specific path. Then, it is transmitted to the Azure Storage Blob with content type “video/mp4”. After the task is completed, we restart the video recording from the Tello live stream. So, we should know that the video file will be overwritten once the user presses the “z” button. And the video file will be stored in the local project path “Resources/Videos/cam_video.mp4”. 

Once the video file is transmitted to the Azure Blob storage, we can see the log information on the debug window. Then, we can use Azure Storage Explorer to review the file as shown in Fig. 4.

Fig. 4 Review video file with Azure Storage Explorer

Summary

In this tutorial, we have presented the steps and Python codes on how to send video streamed from Tello drone to Azure Blob Storage.

Resources

  1. MS Docs for Azure Blob storage.
  2. MS Docs for Create a storage account.
  3. MS Docs for Manage storage account access keys

See Also

  1. Azure Cloud Services for Tello Drones: How to send telemetry to Azure IoTHub
  2. Azure Cloud Services for Tello Drones: How to Control Tello by Azure C2D Messages
  3. Azure Cloud Services for Tello Drones: How to Config Tello by Azure Direct Methods
  4. Azure Cloud Services for Tello Drones: How to Send Images to Azure Blob Storage
  5. Azure Cloud Services for Tello Drones: How to Analyze Images by Azure Computer Vision
  6. Azure Cloud Services for Tello Drones: How to Use Azure Custom Vision Services