尝试直播摄像头时出现 OpenCV 错误

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

相机不会流式传输广告我收到此错误

[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (2401) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (1356) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global ./modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
Traceback (most recent call last):

    cv2.imshow('frame', frame)
cv2.error: OpenCV(4.6.0) ./modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

我想在一个窗口中显示我的 pi 相机源,其中包含我从 YouTube 视频复制的代码。我从 Raspberry pi 4 开始,更改了操作系统 4 次并重新安装了 opencv。我换成了树莓派5。 我已经擦拭了SSD卡8次并使用Raspberry pi和opencv的方法重新安装了oencv 具有相同的结果。 我正在使用的代码:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

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

cap.release()
cv2.destroyAllWindows()
python opencv computer-vision
1个回答
0
投票

确保关于

ret
的所有内容都是正确的,并且您忘记添加 cv2。
waitKey(1)
之后,这是修改后的版本:

import cv2

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    
    if not ret:
        print("Error: Could not read frame.")
        break

    cv2.imshow('frame', frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.