【py小实验】PaddleHub一键抠图并人脸识别裁剪

相关链接。

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")

相关链接:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值