簡介
face_recognition使用世界上最簡單的人臉識别工具,在Python或指令行中識别和操作人臉。
使用dlib最先進的人臉識别技術建構而成,并具有深度學習功能。 該模型在
Labeled Faces in the Wild基準中的準确率為99.38%。
另外還提供了face_recognition指令行工具!
快速入門
本節我們基于ubuntu16.04,python3,使用如下圖檔:
image.png
face_recognition
import face_recognition
image = face_recognition.load_image_file("test0.jpg")
face_locations = face_recognition.face_locations(image,model="cnn")
print(face_locations)
執行結果:
$ python3 quick.py
[(203, 391, 447, 147)]
model選擇模型,預設為hog,該模式很多圖檔是無法識别的,為此一般用采用更精确但是速度更慢的cnn模型。
- 顯示圖檔:
quick2.py
import face_recognition
from PIL import Image
image = face_recognition.load_image_file("test0.jpg")
face_locations = face_recognition.face_locations(image,model="cnn")
top, right, bottom, left = face_locations[0]
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()
pil_image.save("quick2.jpg")
執行後會在目前目錄生成quick2.jpg,并在螢幕顯示美女頭像。
- 上口紅
quick3.py
import face_recognition
from PIL import Image, ImageDraw
image = face_recognition.load_image_file("test1.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)
print(face_landmarks_list)
for face_landmarks in face_landmarks_list:
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
# Gloss the lips
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=3)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=3)
pil_image.show()
pil_image.save("quick3.jpg")
上口紅之前:
上口紅之後:
個人總是覺得沒上口紅的更好看,偏偏有那麼多喜歡化成妖怪的女人。
- 框選
下面代碼把臉部框選出來,注意:face_locations傳回的圖檔和PIL使用的坐标不同,為此需要一定的轉換。
quick4.py
import face_recognition
from PIL import Image, ImageDraw
image = face_recognition.load_image_file("test1.jpg")
locations = face_recognition.face_locations(image)
print(locations)
pos = locations[0]
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
d.rectangle((pos[3], pos[0], pos[1], pos[2]))
pil_image.show()
pil_image.save("quick4.jpg")
本文代碼位址:
https://github.com/china-testing/python-api-tesing/tree/master/python3_libraries/face_recognition其他
- 旋轉
face_recognition隻能識别頭在上嘴在下的圖檔比較好,如果你的照片是橫向的,有可能要旋轉才能識别。
sleep.py
import face_recognition
from PIL import Image, ImageDraw
image = face_recognition.load_image_file("sleep.jpg")
locations = face_recognition.face_locations(image)
print(locations)
img = Image.open("sleep.jpg")
img = img.rotate(90,expand=1)
img.save("/tmp/tmp.jpg")
image = face_recognition.load_image_file("/tmp/tmp.jpg")
locations = face_recognition.face_locations(image)
print(locations)
pil_image = Image.fromarray(image)
pil_image.show()
[]
[(166, 424, 255, 335)]
當然此圖使用cnn模式不用旋轉也是可以識别的,但是我們實驗中發現一些圖檔,比如戴墨鏡的橫向圖檔,還是要旋轉才能識别。
注意旋轉方向是逆時針的。
參考資料
- 本文涉及的python測試開發庫 請在github上點贊,謝謝!
- 讨論 釘釘免費群21745728 qq群144081101 567351477
- 本文相關海量書籍下載下傳
- https://github.com/ageitgey/face_recognition