聰明的人臉識別2——Keras 利用Retinaface+Facenet搭建人臉識別平臺
- 學習前言
- 什么是Retinface和Facenet
- 1、Retinface
- 2、Facenet
- 整體實作代碼
- 實作流程
- 一、資料庫的初始化
- 二、檢測圖片的處理
- 1、人臉的截取與對齊
- 2、利用Facenet對矯正后的人臉進行編碼
- 3、將實時圖片中的人臉特征與資料庫中的進行比對
- 4、圖片繪制
- 使用Retinaface+Facenet進行人臉識別:
學習前言
我又死了我又死了我又死了!

什么是Retinface和Facenet
1、Retinface
Retinaface是來自insightFace的又一力作,基于one-stage的人臉檢測網路,
同時開源了代碼與資料集,在widerface上有非常好的表現,

2、Facenet
谷歌人臉識別演算法,發表于 CVPR 2015,利用相同人臉在不同角度等姿態的照片下有高內聚性,不同人臉有低耦合性,提出使用 cnn + triplet mining 方法,在 LFW 資料集上準確度達到 99.63%,
通過 CNN 將人臉映射到歐式空間的特征向量上,實質上:不同圖片人臉特征的距離較大;通過相同個體的人臉的距離,總是小于不同個體的人臉這一先驗知識訓練網路,
測驗時只需要計算人臉特征EMBEDDING,然后計算距離使用閾值即可判定兩張人臉照片是否屬于相同的個體,

簡單來講,在使用階段,facenet即是:
1、輸入一張人臉圖片
2、通過深度卷積網路提取特征
3、L2標準化
4、得到一個長度為128特征向量,
整體實作代碼
https://github.com/bubbliiiing/facenet-retinaface-keras
Retinaface原理和Facenet原理可以參考我的另外兩篇博客,
睿智的目標檢測40——Keras搭建Retinaface人臉檢測與關鍵點定位平臺
神經網路學習小記錄54——Keras 搭建自己的Facenet人臉識別平臺
實作流程
整體的代碼擺放如下:

一、資料庫的初始化

face_dataset里面裝的是想要識別的人臉,在圖片中看到的obama_1.jpg指的就是obama的第一張人臉圖片,可以配置多張圖片都指向obama,如obama_2.jpg、obama_3.jpg,需要注意的是,face_dataset里面每張圖片都只能包含一張人臉,即目標人臉,
資料庫中每一張圖片對應一個人的人臉,圖片名字中“_”靠左的部分就是這個人的名字,
資料庫初始化指的是人臉資料庫的初始化,
想要實作人臉識別,首先要知道自己需要識別哪些人臉,在這一步中,我們會講識到的人臉進行編碼并放入資料庫中,
資料庫的初始化具體執行的程序就是:
1、遍歷資料庫中所有的圖片,
2、利用Retinaface檢測每個圖片中的人臉位置,
3、將人臉截取下來,
4、將獲取到的人臉進行對齊,
5、利用Facenet將人臉進行編碼,
6、將所有人臉編碼的結果放在一個串列中,
7、保存成npy的形式,
第6步得到的串列就是已知的所有人臉的特征串列,在之后獲得的實時圖片中的人臉都需要與已知人臉進行比對,這樣我們才能知道誰是誰,
實作代碼如下:
#-------------------------------------#
# 呼叫攝像頭檢測
#-------------------------------------#
from retinaface import Retinaface
from PIL import Image
import os
import cv2
import time
import numpy as np
retinaface = Retinaface()
list_dir = os.listdir("face_dataset")
image_paths = []
names = []
for name in list_dir:
image_paths.append("face_dataset/"+name)
names.append(name.split("_")[0])
retinaface.encode_face_dataset(image_paths,names)
二、檢測圖片的處理
1、人臉的截取與對齊

利用Retinaface我們可以獲得一張圖片中人臉的位置,但是我們截取下來的人臉是這樣的:

我們可以很明顯的看出來人臉是歪著的,我們如果人臉可以正過來,那么將對人臉的特征提取非常有好處,

下面這張圖看著就正多了,
常見的對齊方法有很多,在本篇博客里我們使用雙眼坐標進行旋正,
利用雙眼坐標進行旋正需要用到兩個引數,如圖所示分別是:
1、眼睛連線相對于水平線的傾斜角,
2、圖片的中心,

利用這兩個引數我們可以知道需要圖片需要旋轉的角度是多少,圖片旋轉的中心是什么,
代碼實作如下,其中landmark是五個人臉特征點的位置:
#-------------------------------------#
# 人臉對齊
#-------------------------------------#
def Alignment_1(img,landmark):
if landmark.shape[0]==68:
x = landmark[36,0] - landmark[45,0]
y = landmark[36,1] - landmark[45,1]
elif landmark.shape[0]==5:
x = landmark[0,0] - landmark[1,0]
y = landmark[0,1] - landmark[1,1]
# 眼睛連線相對于水平線的傾斜角
if x==0:
angle = 0
else:
# 計算它的弧度制
angle = math.atan(y/x)*180/math.pi
center = (img.shape[1]//2, img.shape[0]//2)
RotationMatrix = cv2.getRotationMatrix2D(center, angle, 1)
# 仿射函式
new_img = cv2.warpAffine(img,RotationMatrix,(img.shape[1],img.shape[0]))
RotationMatrix = np.array(RotationMatrix)
new_landmark = []
for i in range(landmark.shape[0]):
pts = []
pts.append(RotationMatrix[0,0]*landmark[i,0]+RotationMatrix[0,1]*landmark[i,1]+RotationMatrix[0,2])
pts.append(RotationMatrix[1,0]*landmark[i,0]+RotationMatrix[1,1]*landmark[i,1]+RotationMatrix[1,2])
new_landmark.append(pts)
new_landmark = np.array(new_landmark)
return new_img, new_landmark
#---------------------------------------------------#
# 檢測圖片
#---------------------------------------------------#
def detect_image(self, image):
image = np.array(image, np.float32)
old_image = np.array(image.copy(), np.uint8)
#---------------------------------------------------#
# Retinaface檢測部分-開始
#---------------------------------------------------#
# 資料的預處理
im_height, im_width, _ = np.shape(image)
scale = [np.shape(image)[1], np.shape(image)[0], np.shape(image)[1], np.shape(image)[0]]
scale_for_landmarks = [np.shape(image)[1], np.shape(image)[0], np.shape(image)[1], np.shape(image)[0],
np.shape(image)[1], np.shape(image)[0], np.shape(image)[1], np.shape(image)[0],
np.shape(image)[1], np.shape(image)[0]]
if self.letterbox_image:
image = letterbox_image(image,[self.retinaface_input_shape[1], self.retinaface_input_shape[0]])
anchors = self.anchors
else:
anchors = Anchors(self.cfg, image_size=(im_height, im_width)).get_anchors()
#---------------------------------------------------#
# 圖片預處理,歸一化
#---------------------------------------------------#
photo = np.expand_dims(preprocess_input(image),0)
#---------------------------------------------------#
# 將處理完的圖片傳入Retinaface網路當中進行預測
#---------------------------------------------------#
preds = self.retinaface.predict(photo)
#---------------------------------------------------#
# Retinaface網路的解碼,最終我們會獲得預測框
# 將預測結果進行解碼和非極大抑制
#---------------------------------------------------#
results = self.bbox_util.detection_out(preds,anchors,confidence_threshold=self.confidence)
#---------------------------------------------------#
# 如果沒有預測框則回傳原圖
#---------------------------------------------------#
if len(results)<=0:
return old_image
results = np.array(results)
if self.letterbox_image:
results = retinaface_correct_boxes(results, np.array((self.retinaface_input_shape[0], self.retinaface_input_shape[1])), np.array([im_height, im_width]))
#---------------------------------------------------#
# 4人臉框置信度
# :4是框的坐標
# 5:是人臉關鍵點的坐標
#---------------------------------------------------#
results[:,:4] = results[:,:4]*scale
results[:,5:] = results[:,5:]*scale_for_landmarks
#---------------------------------------------------#
# Retinaface檢測部分-結束
#---------------------------------------------------#
2、利用Facenet對矯正后的人臉進行編碼

Facenet是一個人臉特征獲取的模型,將第1步獲得的對齊人臉傳入Facenet模型就可以得到每個人臉的特征向量,
將所有特征向量保存在一個串列中,在第3步進行比對,
#-----------------------------------------------#
# Facenet編碼部分-開始
#-----------------------------------------------#
face_encodings = []
for result in results:
#----------------------#
# 影像截取,人臉矯正
#----------------------#
crop_img = np.array(old_image)[int(result[1]):int(result[3]), int(result[0]):int(result[2])]
landmark = np.reshape(result[5:],(5,2)) - np.array([int(result[0]),int(result[1])])
crop_img,_ = Alignment_1(crop_img,landmark)
#----------------------#
# 人臉編碼
#----------------------#
# 不失真的resize,然后進行歸一化
crop_img = np.array(letterbox_image(np.uint8(crop_img),(self.facenet_input_shape[1],self.facenet_input_shape[0])))/255
crop_img = np.expand_dims(crop_img,0)
# 利用影像算取長度為128的特征向量
face_encoding = self.facenet.predict(crop_img)[0]
face_encodings.append(face_encoding)
#-----------------------------------------------#
# Facenet編碼部分-結束
#-----------------------------------------------#
3、將實時圖片中的人臉特征與資料庫中的進行比對

這個比對程序需要回圈實作,具體對實時圖片中的每一個人臉進行回圈:
1、獲取實時圖片中的每一個人臉特征,
2、將每一個人臉特征和資料庫中所有的人臉進行比較,計算距離,如果距離小于門限值,則認為其具有一定的相似度,
3、獲得每一張人臉在資料庫中最相似的人臉的序號,
4、判斷這個序號對應的人臉距離是否小于門限,是則認為人臉識別成功,他就是這個人,
實作代碼如下:
#-----------------------------------------------#
# 人臉特征比對-開始
#-----------------------------------------------#
face_names = []
for face_encoding in face_encodings:
# 取出一張臉并與資料庫中所有的人臉進行對比,計算得分
matches, face_distances = compare_faces(self.known_face_encodings, face_encoding, tolerance = self.facenet_threhold)
name = "Unknown"
# 找到已知最貼近當前人臉的人臉序號
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = self.known_face_names[best_match_index]
face_names.append(name)
#-----------------------------------------------#
# 人臉特征比對-結束
#-----------------------------------------------#
4、圖片繪制
這一部分只是檢測結果繪制在圖片上,由繪制代碼組成,
for i, b in enumerate(results):
text = "{:.4f}".format(b[4])
b = list(map(int, b))
cv2.rectangle(old_image, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 2)
cx = b[0]
cy = b[1] + 12
cv2.putText(old_image, text, (cx, cy),
cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
# landms
cv2.circle(old_image, (b[5], b[6]), 1, (0, 0, 255), 4)
cv2.circle(old_image, (b[7], b[8]), 1, (0, 255, 255), 4)
cv2.circle(old_image, (b[9], b[10]), 1, (255, 0, 255), 4)
cv2.circle(old_image, (b[11], b[12]), 1, (0, 255, 0), 4)
cv2.circle(old_image, (b[13], b[14]), 1, (255, 0, 0), 4)
name = face_names[i]
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(old_image, name, (b[0] , b[3] - 15), font, 0.75, (255, 255, 255), 2)
使用Retinaface+Facenet進行人臉識別:
在GITHUB上下載好庫后將庫解壓,

下載對應的權重,README中會有下載連接,百度網盤下載或者GITHUB下載均可,

在retinaface.py里面,根據自身需求修改retinaface_model_path、retinaface_backbone、facenet_model_path、facenet_backbone四個引數,

運行encoding.py進行人臉資料集編碼,

運行predict.py進行人臉圖片的預測,

運行video.py進行攝像頭的呼叫與預測,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282085.html
標籤:AI
