View images in OpenCV from Python using an external USB camera on your MacBook

Overview

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.

Trigger

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.

environment

Get camera image

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()

Install CamTwist

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 # ~".

CamTwist.png

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

View images in OpenCV from Python using an external USB camera on your MacBook
Load images from URLs using Pillow in Python 3
[Blender Python] Display images on 3D View using OpenGL (bgl)
Print PDF in Python on Windows: Use an external application
Shoot time-lapse from a PC camera using Python and OpenCV
Posture detection by openpose from USB camera image using python code
Notes on using MeCab from Python
Operate Maya from an external Python interpreter using the rpyc module
Extract text from images in Python
Build and try an OpenCV & Python environment in minutes using Docker
Try to log in to Netflix automatically using python on your PC
Object extraction in images by pattern matching using OpenCV with Python
Load an external library from your Lambda function using AWS Lambda Layers. The Python environment for Amazon Linux 2 is also in place. (Python3.6, Anaconda)
Detect Japanese characters from images using Google's Cloud Vision API in Python
Mosaic images in various shapes (Python, OpenCV)
Image acquisition from camera with Python + OpenCV
Download images from URL list in Python
Create an OpenCV3 + python3 environment on OSX
Video processing using Python + OpenCV on Mac
Notes on using code formatter in Python
Until you create an Ubuntu boot USB on your Macbook and install Ubuntu on your Thinkpad
Send an email directly by pulling an MX record from your email address in Python
Email attachments using your gmail account in python.
Tips for using Realsense SR300 on MacBook in 2020
Notes on using dict in python [Competition Pro]
View drug reviews using a list in Python
Use Python in your environment from Win Automation
Solve simultaneous equations in an instant using Python
Build an OpenCV4 environment on Raspberry Pi using Poetry
Try projective transformation of images using OpenCV with Python
Dry-run sql query using psycopg2 on Redshift in Python
Read logging settings from an external file in Flask
Build a Python environment on your Mac using pyenv
Create your first GDSII file in Python using gdspy
Display USB camera video with Python OpenCV with Raspberry Pi
[Python] Create an infrastructure diagram in 3 minutes using diagrams