这顶海贼王的帽子,我Python给你带上了 | 【人脸识别应用】

微信公众号:AI算法与图像处理 关注可了解更多的资料及技巧。问题或建议,请公众号留言; 若是你以为对你有帮助,欢迎分享和转发哈 本文主要参考:html

zhuanlan.zhihu.com/p/32299758?…git

故事原由

海贼王里,有不少人都戴着帽子,有的帽子对他有着很大的含义,就像是路飞,把他的草帽视若生命,下面就说一说那些戴帽子的男人,固然没有戴绿色的,除了索隆的绿色头巾。github

路飞,对于路飞的草帽,你们都知道那是香克斯交给路飞的,他和路飞约定,等到有一天大海上相遇,路飞再还给香克斯,路飞的草帽,是梦想的开始,也仿佛是一种精神的传承,路飞把他的草帽视若生命,不容许别人践踏,即便丢掉生命。 固然还有,乔巴,罗,艾斯,萨博等等算法

因此你是否是也想来一个?微信

图片来源: baijiahao.baidu.com/s?id=157208…session

思路与实现

1.找到头(脸)——寻找放置位置app

2.将帽子缩放到必定比例——寻找合适的帽子ide

3.将帽子放在头(脸)上面this

准备工做

1.素材(一张头像,一个帽子) 2.导入库 face_recogniton——实现人脸检测 PIL——完成图像的基本操做 这边做者使用的环境是Ubuntu 16.04+Python2.7.12 库安装命令spa

pip install face_recognition pip install pillow #PIL库 详细代码和效果 from PIL import Image import face_recognition

img_path = raw_input("image path:")

image = face_recognition.load_image_file(img_path) face_locations = face_recognition.face_locations(image) print("Found {} face(s) in this photograph.".format(len(face_locations)))

human_img = Image.open(img_path) human_img = human_img.convert("RGBA")

hat_img = Image.open("./lv.jpg") hat_img = hat_img.convert("RGBA")

for face_location in face_locations: top, right, bottom, left = face_location top -= 10 print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

head_h = bottom-top#hight of head
head_l = right-left#length of head

hat_img = hat_img.resize( (head_l,head_h) )#convert size of hat
hat_region = hat_img
#hat_region = hat_region.rotate(6)

human_region = ( left, top-head_h, right, top )

human_img.paste(hat_region, human_region,mask=hat_img)
复制代码

human_img.show() human_img.save("hape.png")
效果展现!

对不起拿错帽子了

v

总结

1.应用的知识 1)人脸识别库和图像基础库的使用 2)简单的数学变换

2.存在的问题 1)帽子图片采用背景为纯黑和纯白均没法隐藏,这里须要采用背景为透明的图片,因为精力有限(太懒了),因此没有弄,连接附上 zhidao.baidu.com/question/27… 2)face_recognition库对于漫画人物有时候也会识别成“人”,全部识别仍是存在必定的问题

3.开阔思惟 这个操做能够拓宽到不少地方,例如,如今不少拍照app有给人脸加装饰的功能等,实际上就是将这里的“帽子”换成其余东西。

代码:github.com/SCUTPZW/AI_…

素材后台回复 “hat” 能够得到

但愿咱们都可以找到那顶拥有生命通常意义的‘帽子“,fighting!

喜欢的朋友们,点个好看吧!

特别感谢分享的小伙伴!

很是感谢你们的支持!

相关文章
相关标签/搜索