项目背景来源:https://blog.csdn.net/weixin_45449540/article/details/109127164?utm_source=app&app_version=4.6.1
领略千年皮影戏魅力,传承正在消失的艺术。皮影戏的神奇,在于小小皮影在指尖上飞舞,时而刀光剑影、时而策马扬鞭、时而缠绵悱恻,千军万马是他,单打独斗也是他。皮影戏可谓是闻名中外,它是把光影声色做到极致的一门古老艺术。
先辈门通过手艺演绎着皮影戏,随着人工智能的浪潮,我们同样也可以通过AI方式来实现皮影戏的效果。通过PaddleHub提供的人体骨骼关键点检测预训练模型,我们就可以快速实现皮影戏的效果。
PaddleHub可以便捷地获取PaddlePaddle生态下的预训练模型,完成模型的管理和一键预测。配合使用Fine-tune API,可以基于大规模预训练模型快速完成迁移学习,让预训练模型能更好地服务于用户特定场景的应用。
一、安装依赖包和模型
! hub install human_pose_estimation_resnet50_mpii==1.1.
执行以下命令安装
python -m pip install paddlepaddle==2.0.2 -i https://mirror.baidu.com/pypi/simple
安装PaddleHub
pip install PaddleHub
二、python源码测试
import paddlehub as hub
# coding=utf-8
pose_estimation = hub.Module(name="human_pose_estimation_resnet50_mpii")
pose_estimation.keypoint_detection(paths=['photo/1.jpg'], visualization=True, output_dir="work/output_pose/")
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)
# 关键点坐标
(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):
retu