DEV Community

0xkoji
0xkoji

Posted on

Solve OpenCV: Cannot use FaceTime HD Camera (Built-in) OpenCV: camera failed to properly initialize!

$ poetry run python HandTrackingMin.py
OpenCV: error in [AVCaptureDeviceInput initWithDevice:error:]
2021-05-02 18:59:21.855 python[6761:45627] OpenCV: Cannot use FaceTime HD Camera (Built-in)
OpenCV: camera failed to properly initialize!
None
Traceback (most recent call last):
  File "HandTrackingMin.py", line 11, in <module>
    cv2.imshow("image", img)
cv2.error: OpenCV(4.5.1) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-oe0iat4a/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
Enter fullscreen mode Exit fullscreen mode

The issue is that iTerm2 doesn't have a permission to access to the FaceTime HD Camera (Built-in).

System Preferences --> Security & Privacy --> Camera
System Preferences will show something like this.

Alt Text

What we need to do is to check iTerm2 then we can use FaceTime HD Camera (Built-in) with OpenCV.

Sample Code

import numpy as np
import cv2

# built-in camera
cap = cv2.VideoCapture(0)

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

    if frame is not None:
        cv2.imshow("image", frame)

    rval, frame = cap.read()

    # gray image
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame', gray)
    # when hit 'q', terminate the program
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)