繁体   English   中英

从 IP 相机馈送时,OpenCV 的 VideoCapture 出现故障

[英]OpenCV's VideoCapture malfunctioning when fed from IP Camera

我只是想通过OpenCV的简单代码读取IP Camera live stream,即如下:

import numpy as np
import cv2

src = 'rtsp://id:pass@xx.xx.xx.xx'

cap = cv2.VideoCapture(src)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

这里的问题是,有时它通过显示正在运行的实时视频就像一个魅力,但有时它会创建很多空白 windows 一直弹出直到作业被终止。 如下图: 在此处输入图像描述

为什么会发生,我们又该如何避免呢?

也许您应该涵盖视频捕获无法建立健康 stream 的情况。

请注意,即使视频捕获打开,在某些情况下也可能不接收帧。 这可能是由于网络流量拥塞、计算资源不足、部分 IP 相机的省电模式等多种原因造成的。

因此,我建议您检查帧大小并确保您的 VideoCapture object 以正确的形状接收帧。 (您可以调试并查看可见帧的大小,以了解相机的预期分辨率。)

如下所示的循环更改可能会有所帮助

min_expected_frame_size = [some integer]
while(cap.isOpened()):
    ret, frame = cap.read()
    
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    
    if ret==True and ((width*height) >= min_expected_frame_size):    
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM