Dome 多人人臉識別 face_recognition
注意
face_recognition 依賴 face_recognition_models
中文字體文件需要自己下載
1.多人人臉識別
# 多人 人臉識別
import os
import numpy as np
import face_recognition
from PIL import Image, ImageDraw, ImageFont
PATH = 'face_imgs'
TMP_IMG = 'tt4.jpeg'
# 制作所有可用圖像特征碼列表
dirs = os.listdir(PATH)
names = [i.split('.')[0] for i in dirs]
face_codes = []
for img_dir in dirs:
current_image = face_recognition.load_image_file(f'{PATH}/{img_dir}')
face_codes.append(face_recognition.face_encodings(current_image)[0])
# 讀取目標圖片並識別人臉
image = face_recognition.load_image_file(TMP_IMG)
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
# 定位所有找到的臉的位置
face_locations = face_recognition.face_locations(image)
# 循環找到的所有人臉
results = []
for face_location in face_locations:
# 打印每張臉的位置信息
top, right, bottom, left = face_location
# 摳人臉圖
face_image = image[top:bottom, left:right]
# 求特征碼
# o_face_code.append(face_recognition.face_encodings(np.array(face_image))[0])
result = face_recognition.compare_faces(face_codes,
face_recognition.face_encodings(np.array(face_image))[0],
tolerance=0.4)
results.append(result)
# 畫矩形
d.rectangle((left, top, right, bottom), None, 'red', width=2)
# 畫文字_中文
name = ''
for na in np.unique(np.array(names)[result]):
name += f'{na} '
path_to_ttf = r'font/simfang.ttf'
font = ImageFont.truetype(path_to_ttf, size=14) # 設置字體
d.text(xy=(left, bottom), text=name, fill='red', font=font, stroke_width=1)
pil_image.show()
2.人臉檢測
from PIL import Image, ImageDraw
import face_recognition
# 讀取圖片並識別人臉
image = face_recognition.load_image_file("t2.png")
face_locations = face_recognition.face_locations(image)
print(face_locations)
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
# 遍歷每個人臉,並標注
faceNum = len(face_locations)
for i in range(0, faceNum):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
rect = (left, top, right, bottom)
d.rectangle(rect, None, outline='red', width=2)
pil_image.show()
3.人臉檢測加摳圖
from PIL import Image
import face_recognition
#加載圖像文件
image = face_recognition.load_image_file("t2.png")
#定位所有找到的臉的位置
face_locations = face_recognition.face_locations(image)
# 循環找到的所有人臉
for face_location in face_locations:
# 打印每張臉的位置信息
top, right, bottom, left = face_location
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()
4.關鍵點檢查
from PIL import Image, ImageDraw
import face_recognition
# 將jpg文件加載到numpy 數組中
image = face_recognition.load_image_file("t2.png")
#查找圖像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for face_landmarks in face_landmarks_list:
#打印此圖像中每個面部特征的位置
facial_features = [
'chin',
'left_eyebrow',
'right_eyebrow',
'nose_bridge',
'nose_tip',
'left_eye',
'right_eye',
'top_lip',
'bottom_lip'
]
for facial_feature in facial_features:
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
#在圖像中畫出每個人臉特征!
for facial_feature in facial_features:
d.line(face_landmarks[facial_feature], width=2, fill='red')
pil_image.show()
5.關鍵點檢查加毀容
** 據說是美顏來着,怎么成這樣我也......emmmmmm**
import face_recognition
from PIL import Image, ImageDraw
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("tt3.jpg")
# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
for face_landmarks in face_landmarks_list:
# Create a PIL imageDraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
# 畫個濃眉
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), width=6)
pil_image.show()
原圖