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
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
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.
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.
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.
However, this is incomplete. If you have multiple faces, you can overwrite all of them,
It will overwrite the face that was mistakenly recognized at random.
Next time, I will aim to solve this problem. Create miscellaneous Photoshop videos with Python + OpenCV ④ Deal with issues
Recommended Posts