一、安装依赖库以及模型
安装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
二、测试及拼接皮影素材
打开pycharm软件,需安装以下依赖库:cv2 4.5.1.48、matplotlib 3.0.3、numpy
1.16.2、tensorflow 2.4.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):
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=['imgs/01.jpg'], visualization=True, output_dir="output_pose/")
print(result)
②打开output_pose文件夹会发现多出一张图片为检测图片骨骼节点图,若安装成功的话,结果如下图所示:
拼接皮影素材
代码如下:
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('imgs/01.jpg')
def get_true_angel(value):
'''
转转得到角度值
'''
return value / np.pi * 180
def get_angle(x1, y1, x2, y2):
'''
计算旋转角度
'''
global the_angle
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