[Python] Cannot turn on Mac Webcam through OpenCV python

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to opencv and trying to access my Macbook's built-in camera through OpenCV python but it gives an error.

    import cv2

    frameWidth = 640
    frameHeight = 480
    cap = cv2.VideoCapture(0)
    cap.set(3, frameWidth)
    cap.set(4, frameHeight)
    cap.set(10,150)

    while True:
    success, img = cap.read()
    cv2.imshow("Result", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
    break



    Traceback (most recent call last):
    File "/Users/hasanaktas/PycharmProjects/OpencvPython/project3.py", line 12, in <module>
    cv2.imshow("Result", img)
    cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'



    already tried changing VideoCapture(0) to VideoCapture(1) and adding the following code but still didn't help. Btw using PyCharm

    cap.release()
    cv2.destroyAllWindows()

    Continue reading...

Compartilhe esta Página