[PYTHON] Try converting 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

This time, I will convert the video in real time.

program

The following is done in the loop of the while statement.

  1. Load one frame of the original color video
  2. Convert color frames to grayscale frames
  3. Display the frame in the window
  4. Write the frame to a file

movie.py


# -*- coding: utf-8 -*-
import cv2


#Constant definition
ESC_KEY = 27     #Esc key
INTERVAL= 33     #Waiting time
FRAME_RATE = 30  # fps

ORG_WINDOW_NAME = "org"
GRAY_WINDOW_NAME = "gray"

ORG_FILE_NAME = "org_768x576.avi"
GRAY_FILE_NAME = "gray_768x576.avi"

#Read original video file
org = cv2.VideoCapture(ORG_FILE_NAME)

#Preparing the saved video file
end_flag, c_frame = org.read()
height, width, channels = c_frame.shape
rec = cv2.VideoWriter(GRAY_FILE_NAME, \
                      cv2.VideoWriter_fourcc(*'XVID'), \
                      FRAME_RATE, \
                      (width, height), \
                      False)
#Window preparation
cv2.namedWindow(ORG_WINDOW_NAME)
cv2.namedWindow(GRAY_WINDOW_NAME)

#Conversion processing loop
while end_flag == True:
    #Grayscale conversion
    g_frame = cv2.cvtColor(c_frame, cv2.COLOR_BGR2GRAY)

    #Frame display
    cv2.imshow(ORG_WINDOW_NAME, c_frame)
    cv2.imshow(GRAY_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 = org.read()

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

Click here for a sample video. (Download) Rename "768x576.avi" to "org_768x576.avi" and place it in the same directory as the script and execute it.

Execution result

The color video and the grayscale video were displayed at the same time, and the grayscale video was also saved in the file.

window.png

How to specify the codec

OpenCV3.1 does not have cv2.cv.CV_FOURCC, which existed until OpenCV2.4. There is also a method to deal with it with your own method as shown below, so I will introduce it.

CV_FOURCC.py


# 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 you only need to operate with the codec decided in the program, you can also define it like this in the constant definition.

opencv3.1.py


XVID = 0x44495658

Here is a summary of frequently used codecs.

Codec String constant
MPEG-1 PIM1 0x314D4950
MPEG-4 MPG4 0x3447504D
MPEG-4.2 MP42 0x3234504D
MPEG-4.3 DIV3 0x33564944
DivX DIVX 0x58564944
DivX 5.0 DX50 0x30355844
Xvid XVID 0x44495658
H.263 U263 0x33363255
H.263I I263 0x33363249
H.264 H264 0x34363248
FLV1 FLV1 0x31564C46
Motion JPEG MJPG 0x47504A4D
Uncompressed zero(0) 0x00000000

Click here for a list of codecs (link)

to be continued

Next, let's convert webcam / camcorder videos in real time with OpenCV. Try converting webcam / camcorder videos in real time with OpenCV

Recommended Posts

Try converting videos in real time with OpenCV
Try converting webcam / camcorder videos in real time with OpenCV
Draw optical flow in real time with OpenCV (Shi-Tomasi method, Lucas-Kanade method)
Get standard output in real time with Python subprocess
Try face detection in real time using a webcam
Try edge detection with OpenCV
Try OpenCV with Google Colaboratory
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
Get YouTube Live chat field in real time with API
Try face recognition with python + OpenCV
Try blurring the image with opencv2
Use OpenCV with Python 3 in Window
I tried to describe the traffic in real time with WebSocket
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
Convenient time series aggregation with TimeGrouper in pandas
Track objects in your video with OpenCV Tracker
Read the output of subprocess.Popen in real time
Try working with Mongo in Python on Mac
Try to detect fish with python + OpenCV2.4 (unfinished)
Change the time zone with Docker in Oracle Database
How to do zero-padding in one line with OpenCV
Try implementing associative memory with Hopfield network in Python
Try projective transformation of images using OpenCV with Python
Try MNIST with VAT (Virtual Adversarial Training) in Keras
Create miscellaneous Photoshop videos with Python + OpenCV ④ Deal with issues
Write letters in the card illustration with OpenCV python
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Continued) Try other distance functions with kmeans in Scikit-learn