溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用Python實現人臉識別

發(fā)布時間:2021-11-20 15:08:43 來源:億速云 閱讀:153 作者:iii 欄目:編程語言

這篇文章主要講解了“怎么用Python實現人臉識別”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用Python實現人臉識別”吧!

1.安裝

最好是使用 Linux 或 Mac 環(huán)境來安裝,Windows 下安裝會有很多問題。在安裝 face_recognition 之前你需要先安裝以下幾個庫,注意順序!

1.1 先安裝 cmake 和 boost

pip install cmake
pip install boost

1.2 安裝 dlib

pip install dlib

此處安裝可能要幾分鐘。如安裝出錯,建議使用 whl 文件來安裝

1.3 安裝 face_recognition

face_recongnition 一般要配合 opencv 一起使用

pip install face_recognition
pip install opencv-python

2. 人臉識別

比如這里總共有三張圖片,其中有兩張已知,第三張是需要識別的圖片

怎么用Python實現人臉識別

首先獲取人臉中的信息

kobe_image = face_recognition.load_image_file("kobe.jpg") # 已知科比照片
jordan_image = face_recognition.load_image_file("jordan.jpeg") # 已知喬丹照片
unknown_image = face_recognition.load_image_file("unkown.jpeg") # 未知照片
kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]
jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]

代碼中前三行分別是加載三張圖片文件并返回圖像的 numpy 數組,后三行返回圖像中每個面部的人臉編碼

然后將未知圖片中的人臉和已知圖片中的人臉進行對比,使用 compare_faces() 函數, 代碼如下:

known_faces = [
 kobe_face_encoding,
 jordan_face_encoding
]
results = face_recognition.compare_faces(known_faces, unknown_face_encoding) # 識別結果列表
print("這張未知照片是科比嗎? {}".format(results[0]))
print("這張未知照片是喬丹嗎? {}".format(results[1]))

運行結果如下:

怎么用Python實現人臉識別

不到二十行代碼,就能識別出人臉是誰,是不是 so easy!

3. 人臉標注

僅僅識別圖片中的人臉總是感覺差點什么,那么將識別出來的人臉進行姓名標注是不是更加有趣~

已知圖片的識別和前面代碼基本是一樣的,未知圖片代碼多了人臉位置的識別,并使用了face_locations() 函數。代碼如下:

face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)

函數傳入兩個參數,返回以上,右,下,左固定順序的臉部位置列表的作用是將已知臉部位置和未知面部編碼進行比較,得到歐式距離~~~具體是什么我也不知道,距離就相當于相識度。

函數說明:face_distance(face_encodings, face_to_compare)

face_encodings:已知的面部編碼

face_to_compare:要比較的面部編碼

本次圖片前面兩張沒有變化,第三張換成了科比和喬丹的合影,最終運行之后結果如下:

怎么用Python實現人臉識別

左邊是原圖,右邊是識別后自動標注出來的圖片。

import face_recognition
from PIL import Image, ImageDraw
import numpy as np
def draws():
 kobe_image = face_recognition.load_image_file("kobe.jpg")
 kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]
 jordan_image = face_recognition.load_image_file("jordan.jpeg")
 jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]
 known_face_encodings = [
 kobe_face_encoding,
 jordan_face_encoding
 ]
 known_face_names = [
 "Kobe",
 "Jordan"
 ]
 unknown_image = face_recognition.load_image_file("two_people.jpeg")
 face_locations = face_recognition.face_locations(unknown_image)
 face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
 pil_image = Image.fromarray(unknown_image)
 draw = ImageDraw.Draw(pil_image)
 for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
 matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
 name = "Unknown"
 face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
 best_match_index = np.argmin(face_distances)
 if matches[best_match_index]:
 name = known_face_names[best_match_index]
 draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))
 text_width, text_height = draw.textsize(name)
 draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
 draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))
 del draw
 pil_image.show()
 pil_image.save("image_with_boxes.jpg")

4. 給人臉美妝

這個功能需要結合 PIL 一起使用。用法都差不多,首先就是將圖片文件加載到 numpy 數組中,然后將人臉中的面部所有特征識別到一個列表中

image = face_recognition.load_image_file("bogute.jpeg")
face_landmarks_list = face_recognition.face_landmarks(image)

遍歷列表中的元素,修改眉毛

d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)

給人臉涂口紅

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=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)

增加眼線

d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), wid=6)

根據以上代碼做了,我用實力不行,打球又臟的 "大嘴" 博格特來做演示!

左邊是原圖,右邊是加了美妝后的效果

怎么用Python實現人臉識別

感謝各位的閱讀,以上就是“怎么用Python實現人臉識別”的內容了,經過本文的學習后,相信大家對怎么用Python實現人臉識別這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI