opencv读取错误:解码MB 53 20字节流-7时出现[h264 @ 0x8f915e0]错误 [英] opencv read error:[h264 @ 0x8f915e0] error while decoding MB 53 20, bytestream -7

查看:303
本文介绍了opencv读取错误:解码MB 53 20字节流-7时出现[h264 @ 0x8f915e0]错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的配置:

  ubuntu 16.04
  opencv 3.3.1
  gcc version 5.4.0 20160609
  ffmpeg version 3.4.2-1~16.04.york0

我用以下方法构建了opencv:

and I built opencv with:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_EXECUTABLE=$(which python) -D OPENCV_EXTRA_MODULES_PATH=/home/xxx/opencv_contrib/modules -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_IPP=ON -D WITH_OPENNI2=ON -D WITH_V4L=ON -D WITH_FFMPEG=ON -D WITH_GSTREAMER=OFF -D WITH_OPENMP=ON -D WITH_VTK=ON -D BUILD_opencv_java=OFF -D BUILD_opencv_python3=OFF -D WITH_CUDA=ON -D ENABLE_FAST_MATH=1 -D WITH_NVCUVID=ON -D CUDA_FAST_MATH=ON -D BUILD_opencv_cnn_3dobj=OFF -D FORCE_VTK=ON  -D WITH_CUBLAS=ON -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D WITH_GDAL=ON -D WITH_XINE=ON -D BUILD_EXAMPLES=OFF -D BUILD_DOCS=ON -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF  -D BUILD_opencv_dnn=OFF -D BUILD_PROTOBUF=OFF -D opencv_dnn_BUILD_TORCH_IMPORTER=OFF -D opencv_dnn_PERF_CAFFE=OFF -D opencv_dnn_PERF_CLCAFFE=OFF -DBUILD_opencv_dnn_modern=OFF -D CUDA_ARCH_BIN=6.1 ..

并使用以下python代码阅读和显示:

and use these python code to read and show:

import cv2
from com.xxx.cv.core.Image import Image

capture=cv2.VideoCapture("rtsp://192.168.10.184:554/mpeg4?username=xxx&password=yyy")
while True:
    grabbed,content=capture.read()
    if grabbed:
        Image(content).show()
        doSomething()
    else:
        print "nothing grabbed.."

每次读取大约50帧后,都会出现类似以下错误:

Everytime, after reading about 50 frames,it will give an error like:

[h264 @ 0x8f915e0] error while decoding MB 53 20, bytestream -7

然后什么也无法抓住,而奇怪的是:

then nothing can be grabbed further,and the strange thing is:

1,comment doSomething() or
2,keep doSomething() and recording the stream from same IPCamera,then run
  code against recorded video

在两种情况下,代码都可以正常工作,有人可以告诉我如何解决此问题吗?

both cases,code works fine,can anyone tell how to solve this problem?Thank in advance!

推荐答案

让我们首先看一个用于读取RTSP的简单示例程序

Let's first look at a simple sample program for reading RTSP

import cv2
cap=cv2.VideoCapture("rtsp://admin:admin_123@172.0.0.0")

ret,frame = cap.read()
while ret:
    ret,frame = cap.read()
    cv2.imshow("frame",frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()
cap.release()

帧值是每个帧的图像。但是,如果您在while代码块中为每个帧添加了识别操作,例如添加Tensorflow来识别其中的小动物,则会报告此错误,并且由于该异常而将while循环中断。

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.

[h264 @ 0x55abeda05080] left block unavailable for requested intra mode
[h264 @ 0x55abeda05080] error while decoding MB 0 14, bytestream 104435

事实证明, FFMPEG Lib不支持rtsp协议中的H264视频,因此解决方案是编写两个不同的线程分别处理每个帧的图像,然后另一个线程处理每个帧的图像。

It turns out that FFMPEG Lib does not support H264 videos in the rtsp protocol, so the solution is to write two different threads to process the images of each frame separately, and then another thread to process the images of each frame.

其思想如下:使用队列,采用先进先出-先进先出策略,开始在一个线程中接收数据,并在另一个线程中逐帧处理数据

The idea is as follows: use a queue, adopt a first-in-first-out strategy, start receiving data in one thread, and process frame-by-frame data in another thread

解决方案代码如下:

import cv2
import queue
import time
import threading
q=queue.Queue()

def Receive():
    print("start Reveive")
    cap = cv2.VideoCapture("rtsp://admin:admin_123@172.0.0.0")
    ret, frame = cap.read()
    q.put(frame)
    while ret:
        ret, frame = cap.read()
        q.put(frame)


def Display():
     print("Start Displaying")
     while True:
         if q.empty() !=True:
            frame=q.get()
            cv2.imshow("frame1", frame)
         if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            
if __name__=='__main__':
    p1=threading.Thread(target=Receive)
    p2 = threading.Thread(target=Display)
    p1.start()
    p2.start()

Receive用作接收数据的线程,而Display则显示为一个简单的过程。

Receive is used as a thread for receiving data, and Display is displayed as a simple process.

这篇关于opencv读取错误:解码MB 53 20字节流-7时出现[h264 @ 0x8f915e0]错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆