Search code examples
pythonopencvwebcamvideo-capture

Python Opencv VideoCapture takes at most 1 argument (2 given)


I want to use VideoCaptureAPIs(cv2.CAP_DSHOW) to adjust my resolution. link: https://reurl.cc/NXZgp6

Because I can not change the scale into 16:9. It's always 4:3 although I set the resolution as 1280*720. It doesn't work but giving me a image like this.

Both side are black LUL

The webcam is Logitech C992 Pro stream webcam. And I have already upgrade the driver.

Do anyone knows how to fix it?

import cv2

cap = cv2.VideoCapture(1,cv2.CAP_DSHOW)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

while(True):
    ret, frame = cap.read()

    cv2.imshow('frame', frame)

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

cap.release()

cv2.destroyAllWindows()

And the error code is below:

Traceback (most recent call last):
  File "d:/VisualCode/Weight Scale2/test.py", line 3, in <module>
    cap = cv2.VideoCapture(1,cv2.CAP_DSHOW)
TypeError: VideoCapture() takes at most 1 argument (2 given)

Solution

  • [Solution] Okay, so I solved the problem by using python3.7.9 and opencv 4.5.1.48 and it works!! I think there are some definition conflicts when using opencv with some versions of python, like 3.6 and 3.8.

    So I just upgraded the python environment and opencv version:

    pip  install --upgrade opencv-python==4.5.1.48
    

    Hope this answer helps.