python使用opencv读取网络视频流报错error while deconding

golds 2021-08-14 PM 3360℃ 0条

在yolov5中使用opencv读取多个网络视频流出现:

图片:errorWhileDecoding

查询Stack Overflow

https://stackoverflow.com/questions/49233433/opencv-read-errorh264-0x8f915e0-error-while-decoding-mb-53-20-bytestream

原因:

The frame value is the image of each frame. But if you add the recognition operation for each frame in the while code block, such as adding Tensorflow to recognize the small animals in it, such an error will be reported and the while loop will be interrupted due to this exception.
在读取帧的循环中如果加入较为耗时的其他操作,例如使用Tensorflow识别小动物,便会产生这种报错,循环也会被打断。

opencv不直接支持H264,通过FFMPEG读rtsp流,该报错:
https://github.com/FFmpeg/FFmpeg/blob/release/3.2/libavcodec/h264_slice.c#L2475
可通过多线程解决问题,一个线程读取帧,另一个线程进行相关操作,可使用队列或其他数据结构,需注意内存溢出及线程锁。
在yolov5中多个网络视频流的读取采用多线程进行读取,出现该问题时,对于每个线程执行的函数通过读取帧的返回值判断,返回值是False可重新读取该视频流保证系统持续运行。

import cv2
cap = cv2.VideoCapture("rtsp://xxx")
while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        frameDeal(frame)
    else:
        cap.release()
        cap = cv2.VideoCapture("rtsp://xxx")
标签: python, opencv, 视频流, yolov5

非特殊说明,本博所有文章均为博主原创。

评论啦~