opencv-python获取rtsp视频流数据程序运行过程中发生错误,直至异常结束

opencv处理rtsp视频发生异常错误,至程序异常结束

问题:

**在使用opencv读取rtsp监控摄像头数据的时候发生的错误信息**

代码示例

首先一下是我的代码,读取的是监控视频rtsp数据


q = queue.Queue(5)
cap = cv2.VideoCapture("rtsp://user:passwod@xx.xx.xx.xx/")


def func1():
    while cap.isOpened():
        try:
            ret, img0 = cap.read()
            if q.qsize() >= 5:
                q.get()
            else:
                q.put(img0)
        except Exception as e:
            print(str(e))
        # time.sleep(0.005)


def func2():
    while True:
        img0 = q.get()
        cv2.imshow("win2", cv2.resize(img0, (1024, 800)))
        if cv2.waitKey(5) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
        time.sleep(0.01)

    cap.release()


if __name__ == '__main__':
    t1 = threading.Thread(target=func1)
    t2 = threading.Thread(target=func2)
    t1.start()
    t2.start()

代码解释

利用Queue是官方推荐的方法,将读取视频帧图片放入消息队列,由另外一个线程去读取消息队列中的数据进行处理,可以避免一下解码的问题发生

错误信息

这段代码本身没有问题,环境配置也没有问题,但是在运行的过程中会报出一些错误信息,如下

[h264 @ 0x3000e00] error while decoding MB 209 68, bytestream
[h264 @ 0x3000e00] non-existing PPS 2 referenced
[h264 @ 0x3000e00] decode_slice_header error

[NULL @ 0x2f39100] SEI type 79 size 1568 truncated at 960
[NULL @ 0x2f39100] missing picture in access unit with size 5728
[h264 @ 0x2f3bfc0] SEI type 79 size 1568 truncated at 951
[h264 @ 0x2f3bfc0] no frame!

[rtsp @ 0x2f35c40] RTP: PT=60: bad cseq 307a expected=232b
[NULL @ 0x2f39100] crop values invalid 1 7 4 0 / 16 32
[h264 @ 0x2f3bfc0] crop values invalid 1 7 4 0 / 16 32

[rtsp @ 0x2f35c40] Undefined type (31)
[rtsp @ 0x2f35c40] RTP: PT=60: bad cseq 6b86 expected=5fb5
[h264 @ 0x2fa2300] cabac decode of qscale diff failed at 207 39
[h264 @ 0x2fa2300] error while decoding MB 207 39, bytestream 738

[h264 @ 0x2f3bfc0] left block unavailable for requested intra4x4 mode -1

[NULL @ 0x2f39100] missing picture in access unit with size 47925
[h264 @ 0x358e240] no frame!

[h264 @ 0x2fa2300] top block unavailable for requested intra mode -1
[h264 @ 0x2fa2300] error while decoding MB 13 0, bytestream 53962
[h264 @ 0x2f3bfc0] number of reference frames (0+2) exceeds max (1; probably corrupt input), discarding one
[h264 @ 0x2f3bfc0] Missing reference picture, default is 65584
[h264 @ 0x3000e00] top block unavailable for requested intra mode
[h264 @ 0x3000e00] error while decoding MB 10 0, bytestream 54023
[h264 @ 0x2f3bfc0] Reference 4 >= 4
[h264 @ 0x2f3bfc0] error while decoding MB 88 0, bytestream 33732

结束程序的错误:
cv2.error: OpenCV(3.4.9) /io/opencv/modules/imgproc/src/resize.cpp:4045: error: (-215:Assertion failed) !ssize.empty() in function ‘resize’

以上所有错误信息是整理过后的,主要发生在程序运行期间
错误信息主要分为 h264、 NULL、rtsp,以及程序在最后运行时的错误 cv2.error
cv2.error 导致了整个程序的结束

问题解决方案

​ 经过很长时间的研究发现,忽略了一件很重要的事情----硬件设备配置

​ opencv读取的如果是图片信息,那么不存在硬件配置的问题,发生的错误肯定是代码上的问题,比如路径写错了之类的。
​ 但在此处,我读取的是视频数据,opencv处理视频的方式就是将每一帧的图片获取出来,进一步进行处理分析,此过程是相当消耗cpu运行的。将此程序运行起来后,可以很明显的发现视频窗口会有明显的卡顿,这时就已经发生了丢包丢帧的问题,查看cpu你会发现cpu的占用率可能是100%,所以丢帧丢包报错就很正常了,最后cpu处理不过来,就会发生cv2.error的问题,告诉你error: (-215:Assertion failed) !ssize.empty() in function ‘resize’,然后程序就结束了。
​ 所以如果用opencv读取视频数据,首要的问题是要注意你的硬件配置cpu是否负担的起读取视频的消耗。以上代码换个16核或者32核处理器应该是不会报错的

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值