利用PaddleHub戴绿帽[人间的青草地需要浇水~]

简介: 利用PaddleHub戴绿帽[人间的青草地需要浇水~]

利用PaddleHub戴绿帽[人间的青草地需要浇水~]

被抖音洗脑的人间的青草地需要浇水~

驱使了我想要做本项目,项目已放在AIstudio


话不多说,开干!

# 安装Paddlehub
!pip install paddlehub==2.1.0


! hub install ultra_light_fast_generic_face_detector_1mb_640==1.1.2


通过paddlehub获取位置信息

import paddlehub as hub
module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640", version="1.1.2")
res = module.face_detection(
    paths = ["data/data96296/1.jpg"],
    visualization=True,
    output_dir='face_detection_output')
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
[2021-06-22 16:33:08,556] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object


环境准备

import cv2
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
filename = 'face_detection_output/1.jpg'
## [Load an image from a file]
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return list(data) if isinstance(data, collections.MappingView) else data
<matplotlib.image.AxesImage at 0x7fe05c0632d0>

20210622175220917.png


res
# 返回
# res (list[dict]): 识别结果的列表,列表中每一个元素为 dict,关键字有'path', 'data',相应的取值为:
# path (str): 原输入图片的路径;
# data (numpy.ndarray): 图像分割得到的结果,shape 为H * W,元素的取值为0-19,表示每个像素的分类结果,映射顺序与下面的调色板相同。
[{'data': [{'left': 221.29409790039062,
    'right': 488.7735290527344,
    'top': 78.7082290649414,
    'bottom': 471.13763427734375,
    'confidence': 0.9999988079071045}],
  'path': 'data/data96296/1.jpg',
  'save_path': 'face_detection_output/1.jpg'}]
# 处理为整数,小数不符合后续位置参数的要求
import math
data = res[0]['data']
left = data[0]['left']
left = math.ceil(left)
right = data[0]['right']
right = math.ceil(right)
top = data[0]['top']
top = math.ceil(top)
bottom = data[0]['bottom']
bottom = math.ceil(bottom)
print("left:",left)
print("right:",right)
print("top:",top)
print("bottom:",bottom)
left: 222
right: 489
top: 79
bottom: 472

有上面的数据可以知道:

框的左上角顶点位置为[left,top]

框的右上角顶点位置为[right,top]

得到这些信息后,再带上提前做好的帽子,就思路清晰了

将帽子尺寸改为[right-left+25,2.2*top] 25和2.2可以自己进行调整,合适为止。

# 储存帽子数据
hat_w , hat_h=  right-left+40, 2.2*top
hat_w = math.ceil(hat_w)
hat_h = math.ceil(hat_h)
print(hat_w)
print(hat_h)
307
174


图片合成

from PIL import Image
mother_img = "data/data96296/1.jpg"
son_img = "data/data96296/lmz.png"
save_img = "result/result.png"
# 获取图片,方便后面的代码调用
M_Img = Image.open(mother_img)
S_Img = Image.open(son_img)
# 子图缩小的倍数1代表不变,2就代表原来的一半(没啥用,本来想通过这个控制图片)
factor = 1
# 给图片指定色彩显示格式
S_Img = S_Img.convert("RGBA")
# 获取图片的尺寸
M_Img_w, M_Img_h = M_Img.size  # 获取被放图片的大小(母图)
print("母图尺寸:",M_Img.size)
S_Img_w, S_Img_h = S_Img.size  # 获取小图的大小(子图)
print("子图尺寸:",S_Img.size)
size_w = int(S_Img_w / factor)
size_h = int(S_Img_h / factor)
# 防止子图尺寸大于母图
if S_Img_w > size_w:
    S_Img_w = size_w
if S_Img_h > size_h:
    S_Img_h = size_h
# 重新设置子图的尺寸
icon = S_Img.resize((hat_w, hat_h), Image.ANTIALIAS)
# 子图的左上角坐标位置
w = left-20
h = 0
# 粘贴子图到母图的指定坐标(当前居中)
M_Img.paste(icon, (w, h), mask=None)
# 保存图片
M_Img.save(save_img)
母图尺寸: (693, 539)
子图尺寸: (224, 236)


查看效果

filename = 'result/result.png'
## [Load an image from a file]
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)


<matplotlib.image.AxesImage at 0x7fe050429050>

202106221752053.png


总结

  1. 利用PaddleHub获取人体的头部位置
  2. 合理计算出帽子所需要放置的位置
  3. 图片合成
  4. 很明显(我这个版本很失败,还没有找到好的办法去把图片的黑色去掉,郁闷(明明帽子图片是PNG且代码也写了RGBA))
相关文章
|
Python
哆啦A梦和小猪佩奇(Python实现)
哆啦A梦和小猪佩奇(Python实现)
205 0
|
消息中间件
【中秋特辑】嫦娥妹妹,你别着急~
今天为大家带来一个小故事,是关于嫦娥和吴刚的爱情故事,想知道吴刚是怎样将情愫传递给嫦娥的吗?让我们一起一探究竟吧~
209 0
【中秋特辑】嫦娥妹妹,你别着急~
|
计算机视觉
我用PaddleHub给人像戴上了口罩
我用PaddleHub给人像戴上了口罩
69 0
我用PaddleHub给人像戴上了口罩
|
消息中间件
【中秋特辑】嫦娥妹妹,你别着急~
今天为大家带来一个小故事,是关于嫦娥和吴刚的爱情故事,想知道吴刚是怎样将情愫传递给嫦娥的吗?让我们一起一探究竟吧~
【中秋特辑】嫦娥妹妹,你别着急~
|
Java
HDU - 2018杭电ACM集训队单人排位赛 - 1 - Problem C. 狙击敌人
HDU - 2018杭电ACM集训队单人排位赛 - 1 - Problem C. 狙击敌人
117 0
马斯克晒SpaceX星际飞船概念图,运载能力达250吨,要飞往另一个恒星系统
在SpaceX的战略规划中,最终是希望用BFR替代所有猎鹰和猎鹰重型火箭,让其成为主要运载工具。
1037 0
|
机器学习/深度学习 算法 测试技术
sklearn调包侠之无敌小抄
scikit-learn(以下简称为sklearn)是用Python开发的机器学习库,其中包含大量机器学习算法、数据集,是数据挖掘方便的工具。
1658 0
伤心者
凄凄岁暮风,翳翳经日雪。 哀生之须臾,羡蛤之无穷。 遇旷烁古今之作,心神为之吸引。 遂引书而坐,俯首细看。 文名“伤心者”,意为悲苦凄凉之人。 作者名“何夕”,引自“人生不相见,动如参与商。
998 0