一、前言
PaddleHub是飞桨生态下的预训练模型的管理工具,旨在让飞桨生态下的开发者更便捷地享受到大规模预训练模型的价值。用户可以通过PaddleHub便捷地获取飞桨生态下的预训练模型,结合Fine-tune API快速完成迁移学习到应用部署的全流程工作,让预训练模型能更好服务于用户特定场景的应用。
通过PaddleHub提供的人体骨骼关键点检测库,完成人体骨骼关键点检测,将人体骨骼关键点进行连接,就可以获取到人体的肢体骨骼,同时映射到皮影身上,就可以得到皮影人了;将视频中连续帧进行转换,就可以实现皮影动起来的效果,只需要提供mp4视频即可实现将视频转化为皮影戏。
二、项目工具
Python版本为3.7.0
PyCharm 2019.3.3 x64
PaddleHub
其他依赖库的版本分别为cv2 4.5.1.48、matplotlib 2.2.2、numpy 1.19.3、tensorflow
2.4.1。
三、项目实现
此项目,你可以在本地运行,也可以在线运行,我采用的是在本地运行。
在线实现参考以下链接:
AI 实现皮影戏,传承正在消失的艺术【一键应用版】
具体流程如下:
安装依赖包和模型
首先我们需要通过pip安装PaddlePaddle和PaddleHub,安装完后,我们就可以通过PaddleHub来安装人体骨骼关键点检测模型 human_pose_estimation_resnet50_mpii。
安装PaddlePaddle
python -m pip install paddlepaddle==2.0.2 -i https://mirror.baidu.com/pypi/simple
安装PaddleHub
pip install PaddleHub
导入人体骨骼关键节点检测模型
hub install human_pose_estimation_resnet50_mpii==1.1.1
选取一张含有人体姿态的作为输入,检测人体骨骼关键点信息。
import os
import cv2
import paddlehub as hub
import matplotlib.pyplot as plt
from matplotlib.image import imread
import numpy as np
def show_img(img_path, size=8):
'''
文件读取图片显示
'''
im = imread(img_path)
plt.figure(figsize=(size, size))
plt.axis("off")
plt.imshow(im)
def img_show_bgr(image, size=8):
'''
cv读取的图片显示
'''
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(size, size))
plt.imshow(image)
plt.axis("off")
plt.show()
pose_estimation = hub.Module(name="human_pose_estimation_resnet50_mpii")
result = pose_estimation.keypoint_detection(paths=['body.jpg'], visualization=True, output_dir="work/output_pose/")
print(result)
实现姿态映射
要实现皮影戏的效果,我们首先要解析人体各个骨骼关键点的位置信息,通过这些关键点的信息,计算人体的肢体长度,旋转角度,这样就可以将皮影的素材对应的缩放和旋转,并移动到指定位置,从而达到与人体动作同步的效果
import os
import cv2
import paddlehub as hub
import matplotlib.pyplot as plt
from matplotlib.image import imread
import numpy as np
def show_img(img_path, size=8):
'''
文件读取图片显示
'''
im = imread(img_path)
plt.figure(figsize=(size,size))
plt.axis("off")
plt.imshow(im)
def img_show_bgr(image,size=8):
'''
cv读取的图片显示
'''
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
plt.figure(figsize=(size,size))
plt.imshow(image)
plt.axis("off")
plt.show()
show_img('work/imgs/body01.jpg')
def get_true_angel(value):
'''
转转得到角度值
'''
return value/np.pi*180
def get_angle(x1, y1, x2, y2):
'''
计算旋转角度
'''
dx = abs(x1- x2)
dy = abs(y1- y2)
result_angele = 0
if x1 == x2:
if y1 > y2:
result_angele = 180
else:
if y1!=y2:
the_angle = int(get_true_angel(np.arctan(dx/dy)))
if x1 < x2:
if y1>y2:
result_angele = -(180 - the_angle)
elif y1<y2:
result_angele = -the_angle
elif y1==y2:
result_angele = -90
elif x1 > x2:
if y1>y2:
result_angele = 180 - the_angle
elif y1<y2:
result_angele = the_angle
elif y1==y2:
result_angele = 90
if result_angele<0:
result_angele = 360 + result_angele
return result_angele
def rotate_bound(image, angle, key_point_y):
'''
旋转图像,并取得关节点偏移量
'''
#获取图像的尺寸
(h,w) = image.shape[:2]
#旋转中心
(cx,cy) = (w/2,h/2)
# 关键点必须在中心的y轴上
(kx,ky) = cx, key_point_y
d = abs(ky - cy)
#设置旋转矩阵
M = cv2.getRotationMatrix2D((cx,cy), -angle, 1.0)
cos = np.abs(M[0,0])
sin = np.abs(M[0,1])
# 计算图像旋转后的新边界
nW = int((h*sin)+(w*cos))
nH = int((h*cos)+(w*sin))
# 计算旋转后的相对位移
move_x = nW/2 + np.sin(angle/180*np.pi)*d
move_y = nH/2 - np.cos(angle/180*np.pi)*d
# 调整旋转矩阵的移动距离(t_{x}, t_{y})
M[0,2] += (nW/2) - cx
M[1,2] += (nH/2) - cy
return cv2.warpAffine(image,M,(nW,nH)), int(move_x), int(move_y)
def get_distences(x1, y1, x2, y2):
return ((x1-x2)**2 + (y1-y2)**2)**0.5
def append_img_by_sk_points(img, append_img_path, key_point_y, first_point, second_point, append_img_reset_width=None,
append_img_max_height_rate=1, middle_flip=False, append_img_max_height=None):
'''
将需要添加的肢体图片进行缩放
'''
append_image = cv2.imdecode(np.fromfile(append_img_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
# 根据长度进行缩放
sk_height = int(get_distences(first_point[0], first_point[1], second_point[0], second_point[1])*append_img_max_height_rate)
# 缩放制约
if append_img_max_height:
sk_height = min(sk_height, append_img_max_height)
sk_width = int(sk_height/append_image.shape[0]*append_image.shape[1]) if append_img_reset_width is None else int(append_img_reset_width)
if sk_width <= 0:
sk_width = 1
if sk_height <= 0:
sk_height = 1
# 关键点映射
key_point_y_new = int(key_point_y/append_image.shape[0]*append_image.shape[1])
# 缩放图片
append_image = cv2.resize(append_image, (sk_width, sk_height))
img_height, img_width, _ = img.shape