Ubuntu 22.04下无法获取DJI Tello无人机实时视频流求助
DJI Tello实时视频流在Ubuntu 22.04.3 LTS无法显示OpenCV窗口的解决办法
问题情况
代码在Windows系统可正常实现Tello无人机实时视频流显示,但在Ubuntu 22.04.3 LTS运行时,OpenCV窗口无法弹出,控制台输出H264解码相关错误。
控制台错误输出
base) nirbhay@nirbhay:~/Desktop/Tello_code$ python LiveStream.py [INFO] tello.py - 129 - Tello instance was initialized. Host: '192.168.10.1'. Port: '8889'. [INFO] tello.py - 438 - Send command: 'command' [INFO] tello.py - 462 - Response command: 'ok' Drone Battery Percentage: 100 Live Stream Address: udp://@0.0.0.0:11111 [INFO] tello.py - 438 - Send command: 'streamon' [INFO] tello.py - 462 - Response streamon: 'ok' non-existing PPS 0 referenced non-existing PPS 0 referenced decode_slice_header error no frame! non-existing PPS 0 referenced non-existing PPS 0 referenced decode_slice_header error no frame! non-existing PPS 0 referenced non-existing PPS 0 referenced decode_slice_header error no frame! non-existing PPS 0 referenced non-existing PPS 0 referenced decode_slice_header error no frame! non-existing PPS 0 referenced non-existing PPS 0 referenced decode_slice_header error no frame! non-existing PPS 0 referenced non-existing PPS 0 referenced decode_slice_header error no frame! error while decoding MB 22 30, bytestream 127 error while decoding MB 58 37, bytestream -6
原代码
from djitellopy import tello import cv2 import time #import libh264decoder drone = tello.Tello() drone.connect() print("Drone Battery Percentage:") print(drone.get_battery()) print("Live Stream Address:") print(drone.get_udp_video_address()) drone.streamon() while True: img=drone.get_frame_read().frame #print("image:",img) img=cv2.resize(img,(660,420)) cv2.imshow("Image",img) #time.sleep(3) cv2.waitKey(1) #time.sleep(1)
依赖与环境
- Python包版本:
djitellopy 2.5.0 opencv-python 4.9.0.80 - 操作系统:Ubuntu 22.04.3 LTS
解决步骤
1. 安装系统级H264解码库
Ubuntu下OpenCV依赖系统解码器,缺失会导致H264流解码失败,执行以下命令安装:
sudo apt update && sudo apt install libavcodec-dev libavformat-dev libswscale-dev libh264decoder-dev
2. 修改代码修复解码与帧处理逻辑
启用libh264decoder并优化帧读取逻辑,避免空帧导致窗口无法渲染:
from djitellopy import tello import cv2 import time import libh264decoder # 取消注释启用内置解码器 drone = tello.Tello() drone.connect() print("Drone Battery Percentage:") print(drone.get_battery()) print("Live Stream Address:") print(drone.get_udp_video_address()) drone.streamon() frame_read = drone.get_frame_read() # 提前获取帧读取对象,避免循环内重复初始化 while True: # 检查流是否停止 if frame_read.stopped: frame_read.stop() break img = frame_read.frame # 跳过空帧,避免OpenCV报错 if img is None: continue img = cv2.resize(img, (660, 420)) cv2.imshow("Image", img) # 添加窗口关闭逻辑,按q退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 清理资源 drone.streamoff() cv2.destroyAllWindows()
3. 强制指定OpenCV窗口后端(可选)
如果窗口仍不显示,强制使用GTK或QT后端,在代码开头添加:
# 强制使用GTK后端 cv2.namedWindow("Image", cv2.WINDOW_GTK_NORMAL) # 或者使用QT后端 # cv2.namedWindow("Image", cv2.WINDOW_QT_NORMAL)
4. 开放UDP端口(若启用防火墙)
如果Ubuntu开启了ufw防火墙,允许UDP 11111端口的入站流量:
sudo ufw allow 11111/udp
原因说明
控制台的解码错误是H264流解码失败的典型表现,Linux下OpenCV依赖系统级解码器,而djitellopy默认逻辑在Linux下对空帧处理不完善,导致OpenCV无法正常渲染窗口。通过补充系统解码器、优化帧读取逻辑,即可解决问题。
内容的提问来源于stack exchange,提问作者NIrbhay Mathur
相关产品推荐
相关产品推荐

