如何使用目标检测Yolov8输出直播

问题描述 投票:0回答:2

我正在尝试将 Yolov8 对象检测模型连接到直播。我在Python中使用Opencv。下面是代码。脚本运行,工作正常几分钟,然后给出以下错误:

[h264 @ 0000021fec1f0040] cabac decode of qscale diff failed at 33 32
[h264 @ 0000021fec1f0040] error while decoding MB 33 32, bytestream 145146
[h264 @ 0000021fec56f300] error while decoding MB 56 14, bytestream -5.

这是代码:

import cv2
from ultralytics import YOLO

# Load the YOLOv8 model
 model = YOLO(r'...\best.pt')

# Open the video file
video_path = "rtsp://..."
cap = cv2.VideoCapture(video_path)

# Loop through the video frames
while cap.isOpened():

    success, frame = cap.read()

    if success:
    
        results = model.track(frame, persist=True)

    
        annotated_frame = results[0].plot()

    
        cv2.imshow("Palka", annotated_frame)

我以为这是一个编码问题,但脚本可以工作并识别这些项目,但随后会出现错误

python opencv video-streaming h.264 rtsp
2个回答
0
投票

我使用此代码解决了问题:

import cv2
from ultralytics import YOLO

model = YOLO(r'best.pt')
results = model.track(source='rtsp:...', show=True)
video_cap = cv2.VideoCapture(results)

while True:
    ret, frame = video_cap.read()
    model.cpu()

    if not ret:
        break

    cv2.imshow("Frame", frame)

    if cv2.waitKey(1) == ord("q"):
        break

video_cap.release()
cv2.destroyAllWindows()

但是现在我无法使用此代码在屏幕上显示形状:

annotated_frame = results[0].plot()
cv2.rectangle(annotated_frame, (100, 900), (600, 700), (0, 0, 255), 2)
cv2.rectangle(annotated_frame, (1250, 710), (1800, 900), (0, 0, 255), 2)

有人知道如何解决这个问题吗?


0
投票

在yolo上的以前版本上工作过一次,根据ffmpeg、RTSP、HLS制作输出流视频以流式传输到Web客户端,

# function will start sub process depends on FFMPEG to handle YOLO output stream to HLS/RTSP stream
# call once when initial
HLS_OUTPUT = '{your_hls_dirictory}/hls/'
def run_ffmpeg(width, height, fps):
   ffmpg_cmd = [
       'ffmpeg',
       '-y',
       '-f', 'rawvideo',
       '-vcodec', 'rawvideo',
       '-pix_fmt', 'bgr24',
       '-s', "{}x{}".format(width, height),
       '-r', str(fps),
       '-i', '-', 
       '-hls_time', '5',
       '-hls_list_size', '6',
       '{HLS_OUTPUT}index.m3u8'
   ]
   return subprocess.Popen(ffmpg_cmd, stdin=subprocess.PIPE)

然后在主函数中当你处理检测后的单帧时

        # Stream results from yolo
        im0 = annotator.result() 
        #Stream single frame as process output to FFMPEG 
        ffmpeg_process.stdin.write(im0 )
      
        # Normal YOLO output
        if view_img:
            cv2.imshow(str(p), im0)
            cv2.waitKey(1)  # 1 millisecond
© www.soinside.com 2019 - 2024. All rights reserved.