這頂海賊王的帽子,我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!

喜歡的朋友們,點個好看吧!

特別感謝分享的小夥伴!

很是感謝你們的支持!

相關文章
相關標籤/搜索