简体   繁体   中英

How to link the logitech Webcam C922 pro with python on Windows

I am attempting to link my Logitech C922 Pro webcam to Python and read from it using Windows, but I am getting an assertion error. This is the code I am using:

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()

But the error I am getting is:

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'

Your code is fine, there is just one line needs to be changed

Change this:

cap = cv2.VideoCapture(1)

To this:

cap = cv2.VideoCapture(0)

This line will get your job done:

cap = cv2.VideoCapture(0)

If you want to load a video from your directory then use:

cap = cv2.VideoCapture('image_path')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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