使用PaddleHub,实现AI一键视频人像美颜。完成视频人像瘦脸、大眼、美白、红唇等美颜功能。

项目地址:AI Studio

对视频进行人像美白和大眼后,效果如下图:

实现代码如下: 建议在 AI Studio 中直接 fork 该 项目 并运行,以获得较好的体验和运行结果。

# 参数设置

videos_path = 'girl.mp4' # 原始视频路径

frames_save_path = 'frame' # 图片帧保存路径

time_interval = 1 # 图片帧保存间隔

target_frames_save_path = 'beauty' # 美化后图片帧保存路径

target_videos_path = 'beauty.mp4' # 生成的视频保存路径

fps = 13 # 生成的视频的帧率

do_thin_face = False # 是否瘦脸

do_enlarge_eyes = True # 是否放大双眼

enlarge_eyes_radius = 15 # 眼睛放大范围

enlarge_eyes_strength = 15 # 眼睛放大程度

do_rouge = False # 是否画红唇

rouge_ruby = True # 是否采用深色口红

do_whitening = True # 是否美白

import cv2

import paddlehub as hub

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

import numpy as np

import math

import os

from PIL import Image

# 将视频转换为图片帧

def video2frame(videos_path,frames_save_path,time_interval):

vidcap = cv2.VideoCapture(videos_path)

success, image = vidcap.read()

count = 0

try:

while success:

success, image = vidcap.read()

count += 1

if count % time_interval == 0:

if count < 10:

num = '00' + str(count)

elif count < 100:

num = '0' + str(count)

else:

num = str(count)

cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "/frame{}.jpg".format(num))

except Exception as e:

print(str(e))

print('视频已转换为图片')

video2frame(videos_path, frames_save_path, time_interval + 1)

# 获取文件名

photos_name = []

for a,b,c in os.walk(frames_save_path):

photos_name.append(c)

photos_name = photos_name[0]

photos_name.sort()

# 读取图片

img = []

for name in photos_name:

img.append(cv2.imread(frames_save_path + '/' + name))

module = hub.Module(name="face_landmark_localization")

result = module.keypoint_detection(images = img)

# 局部平移算法

def local_traslation_warp(image, start_point, end_point, radius):

radius_square = math.pow(radius, 2)

image_cp = image.copy()

dist_se = math.pow(np.linalg.norm(end_point - start_point), 2)

height, width, channel = image.shape

for i in range(width):

for j in range(height):

# 计算该点是否在形变圆的范围之内

# 优化,第一步,直接判断是会在(start_point[0], start_point[1])的矩阵框中

if math.fabs(i - start_point[0]) > radius and math

Copyright © 2088 1986世界杯_意大利世界杯 - zlrxcw.com All Rights Reserved.
友情链接