0

Recently, I want to show tello stream with image detection. My first thought is to save model’s output to my local with save() method, and show it by cv2.imshow() method. It works but the stream with objects detection will have a delay about 4~5 second.

My code:

from threading import Thread
from djitellopy import Tello
import cv2, math, time
import torch
import os
import numpy as np
import asyncio
import imutils
from PIL import Image


path = r'C:\yolov5-master'
model = torch.hub.load(path, 'yolov5s',source='local', pretrained=True)
tello = Tello()
tello.connect()
tello.streamon()

frame_read = tello.get_frame_read()
    
class VideoStreamWidget(object):
    def __init__(self, src=0):
        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream
        global frame
        while True:
            self.frame = cv2.cvtColor(frame_read.frame,cv2.COLOR_RGB2BGR)
            time.sleep(.01)

    def show_frame(self): 
        # Display frames in main program
        wee = model(self.frame)
        arr = wee.datah().cpu().numpy()
        img = Image.fromarray.fromarray(arr, 'RGB')
        result = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
        cv2.imshow('frame', result)
        key = cv2.waitKey(1)

if __name__ == '__main__':
    video_stream_widget = VideoStreamWidget()
    time.sleep(1)
    while True:
        try:
            video_stream_widget.show_frame()
        except AttributeError:
            pass

I'm wondering what data type is the output of model( ).

And I tried:

wee = model(self.frame)
print( type( wee ) )

output:

<class 'models.common.Detections'>

How can I convert this kind of data to the thing fit cv2.imshow( ) method? Or is there any way to show a real-time stream with object detection without delay?

Appreciate.

Zoe
  • 9
  • 3
  • Check out https://stackoverflow.com/a/66641911/3957794 – YScharf May 22 '22 at 10:26
  • @YScharf I tried permute() method but error "AttributeError: 'Detections' object has no attribute 'permute' " appeared. – Zoe May 22 '22 at 12:40
  • yolo networks do NOT output images (but lists of box proposals, which you have to filter by class threshold and apply NMS upon. then you may overlay the original image with the remaining boxes) – berak May 22 '22 at 13:42
  • @berak So it means I should choose the boxes after NMS for each class from the output of model( ) and cover them to the original image. Have any advice to deal with boxes? I am going to learn about it. Thanks. – Zoe May 22 '22 at 14:13
  • i've no idea how to do that with a torch model, but for inspiration, [here's the opencv dnn version](https://github.com/opencv/opencv/blob/7e845a3b87e9e729e6f10b049b2845331f966a6a/samples/dnn/object_detection.py#L148-L166) – berak May 22 '22 at 14:25
  • @berak I will give it a try and update my question once I have new issue or I finish my code. Thanks. – Zoe May 22 '22 at 14:45

0 Answers0