简体   繁体   English

模块 'cv2' 没有属性 'read'

[英]module 'cv2' has no attribute 'read'

import cv2 as cv

frameWidth = 640
frameHeight = 800

capture = cv.VideoCapture(0)
capture.set(3, frameWidth)
capture.set(4, frameHeight)
capture.set(10, 140)

while True:
    passed, frame = cv.read()
    cv.imshow('Camera Capture', frame)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
capture.destroyAllWindows()

I've been trying to capture my camera using OpenCV.我一直在尝试使用 OpenCV 拍摄我的相机。 However it gives the error "module 'cv2' has no attribute 'read'" I looked at various codes and sources including OpenCV's own documentation.但是它给出了错误“模块'cv2'没有属性'read'”我查看了各种代码和来源,包括OpenCV自己的文档。 They all use the same code without errors.他们都使用相同的代码而没有错误。 I tried uninstalling and installing opencv and opencv-contrib.我尝试卸载和安装 opencv 和 opencv-contrib。

In your section of code:在您的代码部分中:

while True:
    passed, frame = cv.read()
    cv.imshow('Camera Capture', frame)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

The issue is that you are trying to call read() on the module, rather you want to call read on the cv.VideoCapture object you created called capture so it should be as such.问题是您试图在模块上调用read() ,而不是您想在您创建的cv.VideoCapture capture上调用 read,所以它应该是这样的。

while True:
    passed, frame = capture.read()
    cv.imshow('Camera Capture', frame)
    if cv.waitKey(1) & 0xFF == ord('q'):
        break

You need to use the VideoCapture to read your cam, instead您需要使用 VideoCapture 来读取您的摄像头,而不是

passed, frame = capture.read()

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

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