Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos

0. Introduction

This is a continuation from the previous time (Part 2 Still image Photoshop creation). Apply the overlay to the face of the still image that was done last time to the video. I referred to the following site. [Series] "CV programming using python and OpenCV" 9th: OpenCV-python ② Video input / output and mouse and keyboard callback with highGUI

1. Video input / output

First of all, let's input and output the video. Simply load the video and output it as is. Below is the code

export_movie.py


#coding=utf-8

import cv2

def export_movie():


    #Specify the video to be input and the output path.
    target = "target/test_input.mp4"
    result = "result/test_output.m4v" 

    #Loading videos and getting video information
    movie = cv2.VideoCapture(target) 
    fps    = movie.get(cv2.CAP_PROP_FPS)
    height = movie.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width  = movie.get(cv2.CAP_PROP_FRAME_WIDTH)

    #Specify MP4V as the format
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
    
    #Open the output file
    out = cv2.VideoWriter(result, int(fourcc), fps, (int(width), int(height)))
   
    #Read the first frame
    if movie.isOpened() == True:
        ret,frame = movie.read()
    else:
        ret = False

    #Continue to export frames while successfully reading frames
    while ret:
        
        #Write the read frame
        out.write(frame)

        #Load the next frame
        ret,frame = movie.read()


if __name__ == '__main__':
    export_movie()

Super easy

2. Result

Although the capacity has nearly tripled, I was able to play the output video. It seems that the codec must be introduced properly in this area. There is no sound.

3. Miscellaneous Photoshop video creation

Since I / O of the video was successful, I will finally create a miscellaneous Photoshop video. However, you only need to perform face recognition and overlay before outputting the video. Frames read from video files can be treated in the same way as image files. Also, the end of the overlayOnPart function has changed so that the return image does not include the alpha channel (transparency). Below is the code.

overlay_movie.py


#coding=utf-8

import cv2
import datetime
import numpy as np
from PIL import Image

def overlay_movie():

    #Specify the video to be input and the output path.
    target = "target/test_input.mp4"
    result = "result/test_output.m4v"  #.I get an error if I don't use m4v

    #Loading videos and getting video information
    movie = cv2.VideoCapture(target) 
    fps    = movie.get(cv2.CAP_PROP_FPS)
    height = movie.get(cv2.CAP_PROP_FRAME_HEIGHT)
    width  = movie.get(cv2.CAP_PROP_FRAME_WIDTH)

    #Specify MP4V as the format
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
    
    #Open the output file
    out = cv2.VideoWriter(result, int(fourcc), fps, (int(width), int(height)))

    #Acquire the features of the cascade classifier
    cascade_path = "haarcascades/haarcascade_frontalface_alt.xml"
    cascade = cv2.CascadeClassifier(cascade_path)

    #Loading overlay image
    ol_imgae_path = "target/warai_otoko.png "    
    ol_image = cv2.imread(ol_imgae_path,cv2.IMREAD_UNCHANGED)
        
    #Read the first frame
    if movie.isOpened() == True:
        ret,frame = movie.read()
    else:
        ret = False

    #Continue to export frames while successfully reading frames
    while ret:
        
        #Convert to grayscale
        frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        #Perform face recognition
        facerecog = cascade.detectMultiScale(frame_gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))

        if len(facerecog) > 0:
            #Add an image to the recognized face
            for rect in facerecog:

                #Resize the image to fit the recognition range
                resized_ol_image = resize_image(ol_image, rect[2], rect[3])
                    
                #Overlay image creation
                frame = overlayOnPart(frame, resized_ol_image, rect[0],rect[1])

        #Write the read frame
        out.write(frame)

        #Load the next frame
        ret,frame = movie.read()

        #Output progress every 100 frames to check progress
        if movie.get(cv2.CAP_PROP_POS_FRAMES)%100 == 0:
            date = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
            print(date + 'Current number of frames:'+str(int(movie.get(cv2.CAP_PROP_POS_FRAMES))))

        #Because it is long, it ends in the middle frame
#        if movie.get(cv2.CAP_PROP_POS_FRAMES) > 1000:
#            break

    print("Done")
    

def resize_image(image, height, width):
    
    #Get the original size
    org_height, org_width = image.shape[:2]
    
    #Shrink to fit the larger size
    if float(height)/org_height > float(width)/org_width:
        ratio = float(height)/org_height
    else:
        ratio = float(width)/org_width
    
    resized = cv2.resize(image,(int(org_height*ratio),int(org_width*ratio)))
    
    return resized    

#Combine images using PIL
def overlayOnPart(src_image, overlay_image, posX, posY):

    #Get the size of the overlay image
    ol_height, ol_width = overlay_image.shape[:2]

    #Convert OpenCV image data to PIL
    #Convert from BGRA to RGBA
    src_image_RGBA = cv2.cvtColor(src_image, cv2.COLOR_BGR2RGB)
    overlay_image_RGBA = cv2.cvtColor(overlay_image, cv2.COLOR_BGRA2RGBA)
    
    #Convert to PIL
    src_image_PIL=Image.fromarray(src_image_RGBA)
    overlay_image_PIL=Image.fromarray(overlay_image_RGBA)

    #Change to RGBA mode for compositing
    src_image_PIL = src_image_PIL.convert('RGBA')
    overlay_image_PIL = overlay_image_PIL.convert('RGBA')

    #Prepare a transparent campus of the same size
    tmp = Image.new('RGBA', src_image_PIL.size, (255, 255,255, 0))
    #Overwrite the prepared campus
    tmp.paste(overlay_image_PIL, (posX, posY), overlay_image_PIL)
    #Combine and save the original and campus
    result = Image.alpha_composite(src_image_PIL, tmp)
    
    # COLOR_RGBA2BGRA to COLOR_Changed to RGBA2BGR. If it contains an alpha channel, it will not be output to the video properly.
    return  cv2.cvtColor(np.asarray(result), cv2.COLOR_RGBA2BGR)
               
if __name__ == '__main__':
    overlay_movie()

It's getting longer and longer.

4. Result


2016/03/11 21:57:04 Current number of frames: 100.0 2016/03/11 21:57:16 Current number of frames: 200.0 2016/03/11 21:57:28 Current number of frames: 300.0 ・ ・ ・ 2016/03/11 22:08:59 Current number of frames: 7100.0 Done


It was executed in the environment of Core i5-4670 3.40GHz memory 16GB for the video of frame size 718 x 480, 24fps, 5:00. It took quite a while, but I was able to output a miscellaneous Photoshop video. 動画認識結果.JPG

5. Finally

However, this is incomplete. If you have multiple faces, you can overwrite all of them, 複数人.JPG

It will overwrite the face that was mistakenly recognized at random. 誤認識.JPG

Next time, I will aim to solve this problem. Create miscellaneous Photoshop videos with Python + OpenCV ④ Deal with issues

Recommended Posts

Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Create miscellaneous Photoshop videos with Python + OpenCV ④ Deal with issues
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Create a Photoshop format file (.psd) with python
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Camera capture with Python + OpenCV
Create 3d gif with python3
[Python] Using OpenCV with Python (Basic)
Face detection with Python + OpenCV
Create a directory with python
Using OpenCV with Python @Mac
Create a simple video analysis tool with python wxpython + openCV
[Python] Using OpenCV with Python (Image Filtering)
Create Awaitable with Python / C API
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
Create folders from '01' to '12' with python
[Python] Using OpenCV with Python (Edge Detection)
Create a virtual environment with Python!
Create an Excel file with Python3
Easy Python + OpenCV programming with Canopy
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
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
Create a striped illusion with gamma correction for Python3 and openCV3
Make blur videos look like fixed-point cameras with Python and OpenCV
Face detection with Python + OpenCV (rotation invariant)
Create a Python function decorator with Class
Create wordcloud from your tweet with python3
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
Save video frame by frame with Python OpenCV
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Capturing images with Pupil, python and OpenCV
I tried non-photorealistic rendering with Python + opencv
Image processing with Python & OpenCV [Tone Curve]
Create Python + uWSGI + Nginx environment with Docker
Image acquisition from camera with Python + OpenCV
[python, openCV] base64 Face recognition with images
Create Excel file with Python + similarity matrix
Create a word frequency counter with Python 3.4
Create an OpenCV3 + python3 environment on OSX
[Python] Read images with OpenCV (for beginners)
[Python] Quickly create an API with Flask
Create an English word app with python
Until you can use opencv with python
Light image processing with Python x OpenCV
Smoothing edge-saved with python + OpenCV (BilateralFilter, NLMeansFilter)
Create your own virtual camera with Python + OpenCV and apply original effects
Hello World and face detection with OpenCV 4.3 + Python