Camera capture with Python + OpenCV

code

import cv2
def capture_camera(mirror=True, size=None):
    """Capture video from camera"""
    #Capture the camera
    cap = cv2.VideoCapture(0) #0 is the camera device number

    while True:
        #ret gets image success flag
        ret, frame = cap.read()

        #Whether it looks like a mirror
        if mirror is True:
            frame = frame[:,::-1]

        #Resize frame
        #size is, for example(800, 600)
        if size is not None and len(size) == 2:
            frame = cv2.resize(frame, size)

        #Show frame
        cv2.imshow('camera capture', frame)

        k = cv2.waitKey(1) #Wait 1msec
        if k == 27: #Exit with ESC key
            break

    #Release the capture
    cap.release()
    cv2.destroyAllWindows()

How to use

$ python
>>> capture_camera()

Recommended Posts

Camera capture with Python + OpenCV
Python, OpenCV camera capture
Face recognition with camera with opencv3 + python2.7
Binarization with OpenCV / Python
Image acquisition from camera with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
Face detection with Python + OpenCV
Using OpenCV with Python @Mac
[Windows] [Python] Camera calibration of fisheye lens with OpenCV
Display USB camera video with Python OpenCV with Raspberry Pi
Shining life with Python and OpenCV
Use PointGrey camera with Python (PyCapture2)
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Get web screen capture with python
Easy Python + OpenCV programming with Canopy
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Load gif images with Python + OpenCV
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
Draw arrows (vectors) with opencv / python
Basic study of OpenCV with Python
Face detection with Python + OpenCV (rotation invariant)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Python2.7 + CentOS7 + OpenCV3
Scraping with Python
Save video frame by frame with Python OpenCV
Python with Go
Try using the camera with Python's OpenCV
Capturing images with Pupil, python and OpenCV
Twilio with Python
How to make a surveillance camera (Security Camera) with Opencv and Python
Integrate with Python
Play with 2016-Python
I tried non-photorealistic rendering with Python + opencv
AES256 with python
Image processing with Python & OpenCV [Tone Curve]
Single pixel camera to experience with Python
Tested with Python
python starts with ()
[python, openCV] base64 Face recognition with images
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
with syntax (Python)
OpenCV Samples (Python)
Bingo with python
[Python] Read images with OpenCV (for beginners)
Zundokokiyoshi with python
[Note] openCV + python
WEB camera capture
Until you can use opencv with python
Light image processing with Python x OpenCV
Smoothing edge-saved with python + OpenCV (BilateralFilter, NLMeansFilter)
Excel with Python