Try using the camera with Python's OpenCV

Overview

This is a commentary for those who want to use the camera with Python. This is an article for beginners that explains how to acquire camera images and save images and videos.

environment

macOS Catalina 10.15.4 Python 3.7.5 opencv-python 4.2.0.34 numpy 1.18.2

OpenCV installation

$ pip install opencv-python

numpy will be installed as soon as you install opencv-python

Preview the camera image for the time being

``camera.py`


import cv2
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break
cv2.destroyAllWindows()
cap.release()

Specify the camera number with the argument of cv2.VideoCapture (). 0 is assigned when using the built-in camera such as a laptop computer or when only one camera is connected. Press q on your keyboard to exit.

Save the image when you press the keyboard

``camera.py`


import cv2
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break
    elif key == ord('c'):
        cv2.imwrite('image.jpg', frame)
cv2.destroyAllWindows()
cap.release()

Specify the path to save the image (either absolute path or relative path) in the first argument of cv2.imwrite (). Save current frame with c on keyboard

Save the video

`` video.py`


import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
video = cv2.VideoWriter('video.mp4', fourcc, fps, size)
while True:
    ret, frame = cap.read()
    video.write(frame)
    cv2.imshow('frame', frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break
cv2.destroyAllWindows()
video.release()
cap.release()

Save the video from running the program to pressing q on the keyboard. The save destination of the video can be specified by the first argument of cv2.VideoWriter ().

Summary

This time, I introduced how to use OpenCV easily. If I have another chance, I would like to introduce face recognition using OpenCV.

Recommended Posts

Try using the camera with Python's OpenCV
Try blurring the image with opencv2
Try using Python's networkx with AtCoder
Try using Python's feedparser.
Try using Python's Tkinter
Try using PythonTex with Texpad.
Face recognition with Python's OpenCV
Try edge detection with OpenCV
Try using matplotlib with PyCharm
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
Try using the Twitter API
Try using OpenCV on Windows
Try projective transformation of images using OpenCV with Python
Try using the Twitter API
Try using the PeeringDB 2.0 API
Try OpenCV with Google Colaboratory
Try using folium with anaconda
Using OpenCV with Python @Mac
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Finding the simplest mistakes with OpenCV
Try face recognition with python + OpenCV
Face recognition with camera with opencv3 + python2.7
Using cgo with the go command
Try mining Bitcoin with Python's hashlib
Try using the Python Cmd module
Try to operate the database using Python's ORM Peewee (August 2019 version)
The fastest way to get camera images regularly with python opencv
Try using the Wunderlist API in Python
Try mathematical formulas using Σ with python
Try using the web application framework Flask
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Try using the $ 6 discount LiDAR (Camsense X1)
Try using the HL band in order
Try the Variational-Quantum-Eigensolver (VQE) algorithm with Blueqat
Try using Dropbox API v2 with Go
Try using Python with Google Cloud Functions
Working with OpenStack using the Python SDK
Try cluster analysis using the K-means method
Image acquisition from camera with Python + OpenCV
Try rewriting the file with the less command
Execute the COPY command using python's Psycopg
Play with puns using the COTOHA API
Get and estimate the shape of the head using Dlib and OpenCV with python
Try to separate the background and moving object of the video with OpenCV
Try using Tkinter
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
Try using cookiecutter
Try using PDFMiner
Try to solve the man-machine chart with Python
Region extraction method using cellular automaton Try region extraction from the image with growcut (Python)
Python: Try using the UI on Pythonista 3 on iPad
Try using the Chinese morphological analysis engine jieba
Try using geopandas
How to try the friends-of-friends algorithm with pyfof
Try using conda virtual environment with Jupyter Notebook
Try using Selenium