相关链接。
1、安装paddlehub
https://paddlehub.readthedocs.io/zh_CN/develop/install.html
2、安装错误请查看
https://blog.csdn.net/ywsydwsbn/article/details/105835538
话不多少先上图
贴代码
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import paddlehub as hub
from PIL import Image
import cv2
# 测试图片路径和输出路径
img_name = "test6"
test_img_path = ["image/" + img_name + ".jpg"]
output_path = 'humanseg_output'
# 2、加载模型
humanseg = hub.Module(name='deeplabv3p_xception65_humanseg')
input_dict = {"image": test_img_path}
# 3、处理图片
results = humanseg.segmentation(data=input_dict,
visualization=True,
output_dir='./' + output_path)
# 预测结果图片地址
out_img_path = output_path + '/' + img_name + '.png'
# 人脸识别
mask_detector = hub.Module(name="pyramidbox_lite_mobile_mask")
imgR = out_img_path
res = mask_detector.face_detection(paths=[imgR],
batch_size= 1,
visualization=True,
output_dir='detection_output',
shrink= 0.5,
)
#这个是坐标系
print(res[0]['data'][0])
imgArr = res[0]['data'][0]
l,t,r,b = imgArr['left'], imgArr['top'], imgArr['right'], imgArr['bottom']
img = Image.open("humanseg_output/" + img_name + ".png")
# 宽高等比增加
w_ratio = (r - l)/3
h_ratio = (b - t)/5
# 扩大裁剪范围。
cropped = img.crop((l-w_ratio, t-h_ratio, r+w_ratio, b+h_ratio)) # (left, upper, right, lower)
cropped.save("humanseg_output/" + img_name + "_head.png")
相关链接: