I used a USB-connected camera instead of the built-in camera (FaceTime HD Camera) on my MacBook Pro to get the camera image from Python and display it.
I tried connecting a USB camera on my MacBook, but it didn't work smoothly. It seems that it can be solved by using software called CamTwist. And you need to keep CamTwist running.
If you want to get the built-in camera image, you can display it with this script.
camera.py
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('camera', frame)
if cv2.waitKey(1)==27: #Exit with ESC
break
cap.release()
cv2.destroyAllWindows()
http://camtwiststudio.com/ Download dmg and launch pkg (installer) to get the .app file.
Go to webcam-> AppleScript-> Webcam and change "FaceTime HD Camera" to "USB_Camera # ~".
With CamTwist running, change the device ID of VideoCapture to 1 ~ and start it. I got it in 2 when I ran it.
camera.py
import cv2
cap = cv2.VideoCapture(2)
while True:
ret, frame = cap.read()
cv2.imshow('camera', frame)
if cv2.waitKey(1)==27: #Exit with ESC
break
cap.release()
cv2.destroyAllWindows()
Recommended Posts