[PYTHON] Try converting webcam / camcorder videos in real time with OpenCV

Introduction

OpenCV (Open Source Computer Vision Library) is a collection of BSD-licensed video / image processing libraries. There are many algorithms for image filtering, template matching, object recognition, video analysis, machine learning, and more.

Example of motion tracking using OpenCV (OpenCV Google Summer of Code 2015) https://www.youtube.com/watch?v=OUbUFn71S4s

Click here for installation and easy usage http://qiita.com/olympic2020/items/d5d475a446ec9c73261e

Click here for still image filtering Try edge detection with OpenCV

Click here for processing video files Try converting videos in real time with OpenCV

This time, I will try to process videos that have input in real time, such as webcams and video cameras.

Smoothing

Smoothing is a filter used to determine the area of characters and the area of moving objects to be tracked. As an example, smoothing is performed in the following flow to determine the area.

blur.png

OpenCV supports the following smoothing:

--Gaussian smoothing: cv2.GaussianBlur --Smoothing with averaging filter: cv2.blur --Smoothing using median: cv2.medianBlur --Smoothing using bilateral filter: cv2.bilateralFilter

program

This time, I will create a program that performs Gaussian smoothing on video input in real time, displays windows, and saves files.

sebcam.py


import cv2

# cv2.cv.CV_FOURCC
def cv_fourcc(c1, c2, c3, c4):
    return (ord(c1) & 255) + ((ord(c2) & 255) << 8) + \
        ((ord(c3) & 255) << 16) + ((ord(c4) & 255) << 24)


if __name__ == '__main__':
    #Constant definition
    ESC_KEY = 27     #Esc key
    INTERVAL= 33     #Waiting time
    FRAME_RATE = 30  # fps
    
    ORG_WINDOW_NAME = "org"
    GAUSSIAN_WINDOW_NAME = "gaussian"

    GAUSSIAN_FILE_NAME = "gaussian.avi"

    DEVICE_ID = 0

    #Camera image acquisition
    cap = cv2.VideoCapture(DEVICE_ID)

    #Preparing the saved video file
    end_flag, c_frame = cap.read()
    height, width, channels = c_frame.shape
    rec = cv2.VideoWriter(GAUSSIAN_FILE_NAME, \
                          cv_fourcc('X', 'V', 'I', 'D'), \
                          FRAME_RATE, \
                          (width, height), \
                          True)

    #Window preparation
    cv2.namedWindow(ORG_WINDOW_NAME)
    cv2.namedWindow(GAUSSIAN_WINDOW_NAME)

    #Conversion processing loop
    while end_flag == True:
        #Gaussian smoothing
        g_frame = cv2.GaussianBlur(c_frame, (15, 15), 10)

        #Frame display
        cv2.imshow(ORG_WINDOW_NAME, c_frame)
        cv2.imshow(GAUSSIAN_WINDOW_NAME, g_frame)

        #Frame writing
        rec.write(g_frame)

        #Exit with Esc key
        key = cv2.waitKey(INTERVAL)
        if key == ESC_KEY:
            break
        
        #Read next frame
        end_flag, c_frame = cap.read()

    #End processing
    cv2.destroyAllWindows()
    cap.release()
    rec.release()

The operation of the script was confirmed in the following environment.

Video files and camera input can be treated in much the same way in a program. When handling input from the camera, it is OK if you specify the device ID in the place of the file name.

cv2.VideoCapture(DEVICE_ID)

If you have only one camera, specify DEVICE_ID = 0.

Execution result

mov_org.png ** No filtering **

mov_gaussian.png ** With Gaussian smoothing processing **

Click here to watch the video.

--No filtering Simply record with the software attached to the PC (link) --Gaussian smoothing process Process and save with OpenCV while shooting with camera (link)

"It's an old laptop, can Python withstand OpenCV in terms of performance?" I tried to run it with half confidence, but it worked without problems (^^) v

Recommended Posts

Try converting webcam / camcorder videos in real time with OpenCV
Try converting videos in real time with OpenCV
Try face detection in real time using a webcam
Draw optical flow in real time with OpenCV (Shi-Tomasi method, Lucas-Kanade method)
Get standard output in real time with Python subprocess
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
Try edge detection with OpenCV
Get YouTube Live chat field in real time with API
Write charts in real time with Matplotlib on Jupyter notebook
Try OpenCV with Google Colaboratory
I tried to describe the traffic in real time with WebSocket
Try face recognition with python + OpenCV
Try blurring the image with opencv2
Use OpenCV with Python 3 in Window
How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV
Try logging in to qiita with Python
Try working with binary data in Python
Try using the camera with Python's OpenCV
Try converting to tidy data with pandas
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Visualize accelerometer information from the microcomputer board in real time with mbed + Python