【问题标题】:How to link the logitech Webcam C922 pro with python on Windows如何在 Windows 上将罗技 Webcam C922 pro 与 python 链接
【发布时间】:2019-11-26 02:43:25
【问题描述】:

我正在尝试将我的 Logitech C922 Pro 网络摄像头链接到 Python 并使用 Windows 从中读取,但我收到了一个断言错误。这是我正在使用的代码:

import cv2
import time
cap = cv2.VideoCapture(1)
cap.set(5, 60)

framecount = 0
prevMillis = 0

print(cap.get(5))

def fpsCount():
    global prevMillis
    global framecount
    millis = int(round(time.time() * 1000))
    framecount += 1
    if millis - prevMillis > 1000:
        print(framecount)
        prevMillis = millis 
        framecount = 0

while True:
    __, frame = cap.read()
    #frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #blur = cv2.blur(frame, (5, 5))
    #ret, thresh = cv2.threshold(blur, 170, 255, 0)
    cv2.imshow("Image", frame)


    fpsCount()    
    k = cv2.waitKey(1) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

但我得到的错误是:

cv2.imshow("Image", frame)
cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

【问题讨论】:

  • 看起来framewidthheight小于0。你能检查frame的尺寸吗?

标签: python python-3.x opencv webcam


【解决方案1】:

您的代码很好,只需更改一行

改变这个:

cap = cv2.VideoCapture(1)

到这里:

cap = cv2.VideoCapture(0)

【讨论】:

    【解决方案2】:

    这一行将完成您的工作:

    cap = cv2.VideoCapture(0)
    

    如果您想从目录加载视频,请使用:

    cap = cv2.VideoCapture('image_path')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 2021-09-16
      • 2020-07-13
      • 1970-01-01
      • 1970-01-01
      • 2011-10-22
      • 2018-06-19
      相关资源
      最近更新 更多