Mac Opencv cannot open the camera problem

created at 12-15-2021 views: 54

error

import cv2

cap = cv2.VideoCapture(0)
cv2.namedWindow("newWindow")
ret, frame = cap.read()
print(cap.isOpened())
while ret:
    cv2.imshow('frame', frame)
    if cv2.waitKey(15) & 0xFF == ord('q'):
        break
    ret, frame = cap.read()

cap.release()
cv2.destroyAllWindows()

The error message is as follows:

cv2.error: OpenCV(4.5.3) /Users/runner/miniforge3/conda-bld/libopencv_1633800950233/work/modules/videoio/src/cap.cpp:239: error: (-215:Assertion failed) !info.backendFactory.empty() in function 'open'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/Users/superstar/PycharmProjects/pythonProject/opencvLearn/main.py", line 4, in <module> cap = cv2.VideoCapture(0)SystemError: <class 'cv2.VideoCapture'> returned a result with an error setProcess finished with exit code 1

I checked the documentation and found that the MAC OS series seemed to use this parameter. I tried it with the mentality of giving it a try.

doc

Final solution

cap = cv2.VideoCapture(0, cv2.CAP_AVFOUNDATION)
created at:12-15-2021
edited at: 12-15-2021: