How to Make Tello Drone Capable of Barcode Scanning through Python
In the warehouse management system, Drone can be used to automate barcode scanning for inventory counts. To learn drone piloting and try barcode scanning in the air, I bought Ryze Tello, which is a mini drone powered by DJI. In this article, I will share how to detect and decode barcodes from Tello drone’s video stream using Dynamsoft Barcode Reader in real-time.
Porting Tello Video Sample from Python 2.7 to Python 3.7
The sample code dji-sdk/Tello-Python is a good crash course for Tello drone control. However, the maintenance of the repository seems to be halted, and the code is only available for Python 2.7.
Since Python 2 is no longer supported and I am using Python 3.7, I have to change the Python code and rebuild the dependent h264 decoding library in order to be compatible with Python 3.
Steps for Running Tello Video Sample on Windows
Get the source code from DJI’s GitHub repositories.
git clone https://github.com/dji-sdk/Tello-Python.git
Import the project to your coding tools. Globally search and substitute the following code snippet for Python 3.7:
# 2.7 import Tkinter # 3.7 import tkinter # 2.7 print "" # 3.7 print() # 2.7 packet_data = "" # 3.7 packet_data = bytes()
To build h264 decoding library, firstly, install ffmpeg via vcpkg:
vcpkg.exe install ffmpeg:x64-windows
After that, get the source code of h264decoder and build the Python module:
git clone [https://github.com/DaWelter/h264decoder.git](https://github.com/DaWelter/h264decoder.git) cd h264decoder python setup.py build_ext --cmake-args="-DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake"
Why Using Dynamsoft Python Barcode SDK
Barcode scanning is a CPU-intensive operation, therefore, running it in a Python thread will suffer from GIL’s performance bottleneck. To avoid the performance issue, Dynamsoft Python Barcode SDK provides a set of video decoding APIs implemented based on C/C++ native thread:
Tello Drone with Barcode Scanning
Install Dynamsoft Python barcode SDK, which is available for Python 3.6, 3.7 and 3.8：
pip install dbr
Apply for a free trial license to unlock all functions.
Create a barcode reader object in tello.py:
from dbr import * self.reader = BarcodeReader() self.reader.init_license('DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==')
Initialize parameters and start the video mode:
parameters = self.reader.init_frame_decoding_parameters() self.frameWidth = 640 # max: 960 self.frameHeight = 480 # max: 720 self.results = None parameters.image_pixel_format = EnumImagePixelFormat.IPF_RGB_888 parameters.max_queue_length = 2 parameters.max_result_queue_length = 2 parameters.width = self.frameWidth parameters.height = self.frameHeight parameters.stride = self.frameWidth * 3 parameters.auto_filter = 1 self.reader.start_video_mode(parameters, self.on_barcode_result)
The resolution of Tello drone’s camera is 720P. We can save the decoding results via callback function for later UI rendering:
def on_barcode_result(self, data): self.results = data
Next, go to the _receive_video_thread() function to append received frames to the barcode decoding queue:
for frame in self._h264_decode(packet_data): self.frame = cv2.resize(frame, (self.frameWidth, self.frameHeight)) try: ret = self.reader.append_video_frame(self.frame) except: pass
Note: It is not necessary to resize the frame unless you want to accelerate the decoding speed (Large images are better for recognition accuracy).
Finally, we add stop_video_mode() to the destructor method:
def __del__(self): self.reader.stop_video_mode()
With a few lines of Python code, I’ve implemented barcode scanning for the Tello drone. Let’s try the Tello drone GUI app:
How Environment Impacts the Drone’s Vision System
While I was testing the Drone’s video stream in my office, I found the image was out of focus.
There is no command mentioned for focus adjustment in the Tello SDK user guide.
Besides, Tello drone is difficult to stay still while hovering in the low-light environment, even though I have calibrated the IMU(Inertial Measurement Unit).