如何使用cv2. videoccapture()从openv捕获Tello SDK 2.0的流 &g



我想写一个代码,显示流从tello与yolov5火炬模型实时低延迟。

但是我被如何使用cv2.VideoCapture()与tello卡住了。

from threading import Thread
from djitellopy import Tello
import cv2, math, time
import os

tello = Tello()
tello.connect(False)
tello.streamon()
print(tello.get_udp_video_address())
print(type(tello.get_udp_video_address()))
cap = cv2.VideoCapture(tello.get_udp_video_address())
try:
while True:
img = cap.read()
cv2.imshow('frame', img)
except KeyboardInterrupt:
exit(1)
finally:
print("fin")

输出:

[INFO] tello.py - 107 - Tello instance was initialized. Host: '192.168.10.1'. Port: '8889'.
[INFO] tello.py - 422 - Send command: 'command'
[WARNING] tello.py - 432 - Aborting command 'command'. Did not receive a response after 7 seconds
[INFO] tello.py - 422 - Send command: 'command'
[INFO] tello.py - 446 - Response command: 'ok'
[INFO] tello.py - 422 - Send command: 'streamon'
[INFO] tello.py - 446 - Response streamon: 'ok'
udp://@0.0.0.0:11111
<class 'str'>
fin
Traceback (most recent call last):
File "bruh.py", line 16, in <module>
cv2.imshow('frame', img)
cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'imshow'
> Overload resolution failed:
>  - mat is not a numerical tuple
>  - Expected Ptr<cv::cuda::GpuMat> for argument 'mat'
>  - Expected Ptr<cv::UMat> for argument 'mat'
[INFO] tello.py - 422 - Send command: 'streamoff'

我想使用cv2.VideoCapture(),因为有更多的来源与网络摄像头&实时对象检测。我尝试了很多方法来处理它,但它们似乎对我不起作用。方法包括给捕获一个随机的udp端口,让它自己找出正确的端口。

感谢大家的回复。

这是从tello获取视频流的基本代码:

from djitellopy import tello
import cv2
me = tello.Tello()
#cap = cv2.VideoCapture(0)
me.connect()
print(me.get_battery())
me.streamon()

while True:
img = me.get_frame_read().frame
img = cv2.resize(img, (360, 240))
cv2.imshow("results", img)
cv2.waitKey(1)

当你这样做的时候,你必须调用'tell .streamon()'然后在循环中你让图像变量:

img = me.get_frame_read().frame

我将它与你的代码合并,结果如下:

from threading import Thread
from djitellopy import Tello
import cv2, math, time
import os

tello = Tello()
tello.connect()#
tello.streamon()
#print(tello.get_udp_video_address())
#print(type(tello.get_udp_video_address()))
#cap = cv2.VideoCapture(tello.get_udp_video_address())
try:
while True:
img = tello.get_frame_read().frame#
cv2.imshow('frame', img)
cv2.waitKey(1)#
except KeyboardInterrupt:
exit(1)
finally:
print("fin")

我在我添加/更改的所有内容后面加上注释/#,并注释掉所有不必要的内容,你基本上只需要调用img = tello.get_frame_read.frame就可以了。然后显示它并添加1毫秒的延迟。我建议你看看这个视频:https://www.youtube.com/watch?v=LmEcyQnfpDA,里面有很多很酷的东西,你可能想尝试一下。享受吧!

最新更新