Keras目標(biāo)檢測(cè)mtcnn?facenet搭建人臉識(shí)別平臺(tái)
什么是mtcnn和facenet
1、mtcnn
MTCNN,英文全稱(chēng)是Multi-task convolutional neural network,中文全稱(chēng)是多任務(wù)卷積神經(jīng)網(wǎng)絡(luò),該神經(jīng)網(wǎng)絡(luò)將人臉區(qū)域檢測(cè)與人臉關(guān)鍵點(diǎn)檢測(cè)放在了一起。
總體可分為P-Net、R-Net、和O-Net三層網(wǎng)絡(luò)結(jié)構(gòu)。
2、facenet
谷歌人臉檢測(cè)算法,發(fā)表于 CVPR 2015,利用相同人臉在不同角度等姿態(tài)的照片下有高內(nèi)聚性,不同人臉有低耦合性,提出使用 cnn + triplet mining 方法,在 LFW 數(shù)據(jù)集上準(zhǔn)確度達(dá)到 99.63%。
通過(guò) CNN 將人臉映射到歐式空間的特征向量上,實(shí)質(zhì)上:不同圖片人臉特征的距離較大;通過(guò)相同個(gè)體的人臉的距離,總是小于不同個(gè)體的人臉這一先驗(yàn)知識(shí)訓(xùn)練網(wǎng)絡(luò)。
測(cè)試時(shí)只需要計(jì)算人臉特征EMBEDDING,然后計(jì)算距離使用閾值即可判定兩張人臉照片是否屬于相同的個(gè)體。
簡(jiǎn)單來(lái)講,在使用階段,facenet即是:
1、輸入一張人臉圖片
2、通過(guò)深度學(xué)習(xí)網(wǎng)絡(luò)提取特征
3、L2標(biāo)準(zhǔn)化
4、得到128維特征向量。
mtcnn原理和facenet原理可以參考我的另外兩篇博客。
睿智的目標(biāo)檢測(cè)——Keras搭建mtcnn人臉檢測(cè)平臺(tái)
神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)——facenet詳解及其keras實(shí)現(xiàn)
實(shí)現(xiàn)流程
整體的代碼擺放如下:
一、數(shù)據(jù)庫(kù)的初始化
face_dataset里面裝的是想要識(shí)別的人臉,比如說(shuō)obama.jpg指的就是奧巴馬。
數(shù)據(jù)庫(kù)中每一張圖片對(duì)應(yīng)一個(gè)人的人臉,圖片名字就是這個(gè)人的名字。
數(shù)據(jù)庫(kù)初始化指的是人臉數(shù)據(jù)庫(kù)的初始化。
想要實(shí)現(xiàn)人臉識(shí)別,首先要知道自己需要識(shí)別哪些人臉。
這就是數(shù)據(jù)庫(kù)的一個(gè)功能,將想要檢測(cè)到的人臉?lè)湃霐?shù)據(jù)庫(kù)中,并進(jìn)行編碼。
數(shù)據(jù)庫(kù)的初始化具體執(zhí)行的過(guò)程就是:
1、遍歷數(shù)據(jù)庫(kù)中所有的圖片。
2、檢測(cè)每個(gè)圖片中的人臉位置。
3、利用mtcnn將人臉截取下載。
4、將獲取到的人臉進(jìn)行對(duì)齊。
5、利用facenet將人臉進(jìn)行編碼。
6、將所有人臉編碼的結(jié)果放在一個(gè)列表中。
第6步得到的列表就是已知的所有人臉的特征列表,在之后獲得的實(shí)時(shí)圖片中的人臉都需要與已知人臉進(jìn)行比對(duì),這樣我們才能知道誰(shuí)是誰(shuí)。實(shí)現(xiàn)代碼如下:
class face_rec(): def __init__(self): # 創(chuàng)建mtcnn對(duì)象 # 檢測(cè)圖片中的人臉 self.mtcnn_model = mtcnn() # 門(mén)限函數(shù) self.threshold = [0.5,0.8,0.9] # 載入facenet # 將檢測(cè)到的人臉轉(zhuǎn)化為128維的向量 self.facenet_model = InceptionResNetV1() # model.summary() model_path = './model_data/facenet_keras.h5' self.facenet_model.load_weights(model_path) #-----------------------------------------------# # 對(duì)數(shù)據(jù)庫(kù)中的人臉進(jìn)行編碼 # known_face_encodings中存儲(chǔ)的是編碼后的人臉 # known_face_names為人臉的名字 #-----------------------------------------------# face_list = os.listdir("face_dataset") self.known_face_encodings=[] self.known_face_names=[] for face in face_list: name = face.split(".")[0] img = cv2.imread("./face_dataset/"+face) img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # 檢測(cè)人臉 rectangles = self.mtcnn_model.detectFace(img, self.threshold) # 轉(zhuǎn)化成正方形 rectangles = utils.rect2square(np.array(rectangles)) # facenet要傳入一個(gè)160x160的圖片 rectangle = rectangles[0] # 記下他們的landmark landmark = (np.reshape(rectangle[5:15],(5,2)) - np.array([int(rectangle[0]),int(rectangle[1])]))/(rectangle[3]-rectangle[1])*160 crop_img = img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img = cv2.resize(crop_img,(160,160)) new_img,_ = utils.Alignment_1(crop_img,landmark) new_img = np.expand_dims(new_img,0) # 將檢測(cè)到的人臉傳入到facenet的模型中,實(shí)現(xiàn)128維特征向量的提取 face_encoding = utils.calc_128_vec(self.facenet_model,new_img) self.known_face_encodings.append(face_encoding) self.known_face_names.append(name)
二、實(shí)時(shí)圖片的處理
1、人臉的截取與對(duì)齊
利用mtcnn我們可以獲得一張圖片中人臉的位置,但是我們截取下來(lái)的人臉是這樣的:
我們可以很明顯的看出來(lái)人臉是歪著的,我們?nèi)绻四樋梢哉^(guò)來(lái),那么將對(duì)人臉的特征提取非常有好處。
下面這張圖看著就正多了。
常見(jiàn)的對(duì)齊方法有
1、通過(guò)雙眼坐標(biāo)進(jìn)行旋正
2、通過(guò)矩陣運(yùn)算求解仿射矩陣進(jìn)行旋正
這里簡(jiǎn)單講講通過(guò)雙眼坐標(biāo)進(jìn)行旋正。
利用雙眼坐標(biāo)進(jìn)行旋正需要用到兩個(gè)參數(shù),如圖所示分別是:
1、眼睛連線(xiàn)相對(duì)于水平線(xiàn)的傾斜角。
2、圖片的中心。
利用這兩個(gè)參數(shù)我們可以知道需要圖片需要旋轉(zhuǎn)的角度是多少,圖片旋轉(zhuǎn)的中心是什么。
代碼實(shí)現(xiàn)如下,其中l(wèi)andmark是五個(gè)人臉特征點(diǎn)的位置:
#-------------------------------------# # 人臉對(duì)齊 #-------------------------------------# 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] # 眼睛連線(xiàn)相對(duì)于水平線(xiàn)的傾斜角 if x==0: angle = 0 else: # 計(jì)算它的弧度制 angle = math.atan(y/x)*180/math.pi center = (img.shape[1]//2, img.shape[0]//2) RotationMatrix = cv2.getRotationMatrix2D(center, angle, 1) # 仿射函數(shù) 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
2、利用facenet對(duì)矯正后的人臉進(jìn)行編碼
facenet是一個(gè)人臉特征獲取的模型,將第1步獲得的對(duì)齊人臉傳入facenet模型就可以得到每個(gè)人臉的特征向量。
將所有特征向量保存在一個(gè)列表中,在第3步進(jìn)行比對(duì)。
height,width,_ = np.shape(draw) draw_rgb = cv2.cvtColor(draw,cv2.COLOR_BGR2RGB) # 檢測(cè)人臉 rectangles = self.mtcnn_model.detectFace(draw_rgb, self.threshold) print(np.shape(rectangles)) if len(rectangles)==0: return # 轉(zhuǎn)化成正方形 rectangles = utils.rect2square(np.array(rectangles,dtype=np.int32)) rectangles[:,0] = np.clip(rectangles[:,0],0,width) rectangles[:,1] = np.clip(rectangles[:,1],0,height) rectangles[:,2] = np.clip(rectangles[:,2],0,width) rectangles[:,3] = np.clip(rectangles[:,3],0,height) #-----------------------------------------------# # 對(duì)檢測(cè)到的人臉進(jìn)行編碼 #-----------------------------------------------# face_encodings = [] for rectangle in rectangles: # 獲取landmark在小圖中的坐標(biāo) landmark = (np.reshape(rectangle[5:15],(5,2)) - np.array([int(rectangle[0]),int(rectangle[1])]))/(rectangle[3]-rectangle[1])*160 # 截取圖像 crop_img = draw_rgb[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img = cv2.resize(crop_img,(160,160)) # 對(duì)齊 new_img,_ = utils.Alignment_1(crop_img,landmark) new_img = np.expand_dims(new_img,0) # 利用facenet_model計(jì)算128維特征向量 face_encoding = utils.calc_128_vec(self.facenet_model,new_img) face_encodings.append(face_encoding)
3、將實(shí)時(shí)圖片中的人臉特征與數(shù)據(jù)庫(kù)中的進(jìn)行比對(duì)
這個(gè)比對(duì)過(guò)程是這樣的:
1、獲取實(shí)時(shí)圖片中的每一張人臉特征。
2、將每一張人臉特征和數(shù)據(jù)庫(kù)中所有的人臉進(jìn)行比較,計(jì)算距離。如果距離小于門(mén)限值,則認(rèn)為其具有一定的相似度。
3、獲得每一張人臉在數(shù)據(jù)庫(kù)中最相似的人臉的序號(hào)。
4、判斷這個(gè)序號(hào)對(duì)應(yīng)的人臉距離是否小于門(mén)限,是則認(rèn)為人臉識(shí)別成功,他就是這個(gè)人。
實(shí)現(xiàn)代碼如下:
face_names = [] for face_encoding in face_encodings: # 取出一張臉并與數(shù)據(jù)庫(kù)中所有的人臉進(jìn)行對(duì)比,計(jì)算得分 matches = utils.compare_faces(self.known_face_encodings, face_encoding, tolerance = 0.9) name = "Unknown" # 找出距離最近的人臉 face_distances = utils.face_distance(self.known_face_encodings, face_encoding) # 取出這個(gè)最近人臉的評(píng)分 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、實(shí)時(shí)處理圖片整體代碼
class face_rec(): def recognize(self,draw): #-----------------------------------------------# # 人臉識(shí)別 # 先定位,再進(jìn)行數(shù)據(jù)庫(kù)匹配 #-----------------------------------------------# height,width,_ = np.shape(draw) draw_rgb = cv2.cvtColor(draw,cv2.COLOR_BGR2RGB) # 檢測(cè)人臉 rectangles = self.mtcnn_model.detectFace(draw_rgb, self.threshold) print(np.shape(rectangles)) if len(rectangles)==0: return # 轉(zhuǎn)化成正方形 rectangles = utils.rect2square(np.array(rectangles,dtype=np.int32)) rectangles[:,0] = np.clip(rectangles[:,0],0,width) rectangles[:,1] = np.clip(rectangles[:,1],0,height) rectangles[:,2] = np.clip(rectangles[:,2],0,width) rectangles[:,3] = np.clip(rectangles[:,3],0,height) #-----------------------------------------------# # 對(duì)檢測(cè)到的人臉進(jìn)行編碼 #-----------------------------------------------# face_encodings = [] for rectangle in rectangles: # 獲取landmark在小圖中的坐標(biāo) landmark = (np.reshape(rectangle[5:15],(5,2)) - np.array([int(rectangle[0]),int(rectangle[1])]))/(rectangle[3]-rectangle[1])*160 # 截取圖像 crop_img = draw_rgb[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img = cv2.resize(crop_img,(160,160)) # 對(duì)齊 new_img,_ = utils.Alignment_1(crop_img,landmark) new_img = np.expand_dims(new_img,0) # 利用facenet_model計(jì)算128維特征向量 face_encoding = utils.calc_128_vec(self.facenet_model,new_img) face_encodings.append(face_encoding) face_names = [] for face_encoding in face_encodings: # 取出一張臉并與數(shù)據(jù)庫(kù)中所有的人臉進(jìn)行對(duì)比,計(jì)算得分 matches = utils.compare_faces(self.known_face_encodings, face_encoding, tolerance = 0.9) name = "Unknown" # 找出距離最近的人臉 face_distances = utils.face_distance(self.known_face_encodings, face_encoding) # 取出這個(gè)最近人臉的評(píng)分 best_match_index = np.argmin(face_distances) if matches[best_match_index]: name = self.known_face_names[best_match_index] face_names.append(name) rectangles = rectangles[:,0:4] #-----------------------------------------------# # 畫(huà)框~!~ #-----------------------------------------------# for (left, top, right, bottom), name in zip(rectangles, face_names): cv2.rectangle(draw, (left, top), (right, bottom), (0, 0, 255), 2) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(draw, name, (left , bottom - 15), font, 0.75, (255, 255, 255), 2) return draw
全部代碼:
這里只放出主文件的全部代碼,需要的可以去github下載:
import cv2 import os import numpy as np from net.mtcnn import mtcnn import utils.utils as utils from net.inception import InceptionResNetV1 class face_rec(): def __init__(self): # 創(chuàng)建mtcnn對(duì)象 # 檢測(cè)圖片中的人臉 self.mtcnn_model = mtcnn() # 門(mén)限函數(shù) self.threshold = [0.5,0.8,0.9] # 載入facenet # 將檢測(cè)到的人臉轉(zhuǎn)化為128維的向量 self.facenet_model = InceptionResNetV1() # model.summary() model_path = './model_data/facenet_keras.h5' self.facenet_model.load_weights(model_path) #-----------------------------------------------# # 對(duì)數(shù)據(jù)庫(kù)中的人臉進(jìn)行編碼 # known_face_encodings中存儲(chǔ)的是編碼后的人臉 # known_face_names為人臉的名字 #-----------------------------------------------# face_list = os.listdir("face_dataset") self.known_face_encodings=[] self.known_face_names=[] for face in face_list: name = face.split(".")[0] img = cv2.imread("./face_dataset/"+face) img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # 檢測(cè)人臉 rectangles = self.mtcnn_model.detectFace(img, self.threshold) # 轉(zhuǎn)化成正方形 rectangles = utils.rect2square(np.array(rectangles)) # facenet要傳入一個(gè)160x160的圖片 rectangle = rectangles[0] # 記下他們的landmark landmark = (np.reshape(rectangle[5:15],(5,2)) - np.array([int(rectangle[0]),int(rectangle[1])]))/(rectangle[3]-rectangle[1])*160 crop_img = img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img = cv2.resize(crop_img,(160,160)) new_img,_ = utils.Alignment_1(crop_img,landmark) new_img = np.expand_dims(new_img,0) # 將檢測(cè)到的人臉傳入到facenet的模型中,實(shí)現(xiàn)128維特征向量的提取 face_encoding = utils.calc_128_vec(self.facenet_model,new_img) self.known_face_encodings.append(face_encoding) self.known_face_names.append(name) def recognize(self,draw): #-----------------------------------------------# # 人臉識(shí)別 # 先定位,再進(jìn)行數(shù)據(jù)庫(kù)匹配 #-----------------------------------------------# height,width,_ = np.shape(draw) draw_rgb = cv2.cvtColor(draw,cv2.COLOR_BGR2RGB) # 檢測(cè)人臉 rectangles = self.mtcnn_model.detectFace(draw_rgb, self.threshold) print(np.shape(rectangles)) if len(rectangles)==0: return # 轉(zhuǎn)化成正方形 rectangles = utils.rect2square(np.array(rectangles,dtype=np.int32)) rectangles[:,0] = np.clip(rectangles[:,0],0,width) rectangles[:,1] = np.clip(rectangles[:,1],0,height) rectangles[:,2] = np.clip(rectangles[:,2],0,width) rectangles[:,3] = np.clip(rectangles[:,3],0,height) #-----------------------------------------------# # 對(duì)檢測(cè)到的人臉進(jìn)行編碼 #-----------------------------------------------# face_encodings = [] for rectangle in rectangles: # 獲取landmark在小圖中的坐標(biāo) landmark = (np.reshape(rectangle[5:15],(5,2)) - np.array([int(rectangle[0]),int(rectangle[1])]))/(rectangle[3]-rectangle[1])*160 # 截取圖像 crop_img = draw_rgb[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img = cv2.resize(crop_img,(160,160)) # 對(duì)齊 new_img,_ = utils.Alignment_1(crop_img,landmark) new_img = np.expand_dims(new_img,0) # 利用facenet_model計(jì)算128維特征向量 face_encoding = utils.calc_128_vec(self.facenet_model,new_img) face_encodings.append(face_encoding) face_names = [] for face_encoding in face_encodings: # 取出一張臉并與數(shù)據(jù)庫(kù)中所有的人臉進(jìn)行對(duì)比,計(jì)算得分 matches = utils.compare_faces(self.known_face_encodings, face_encoding, tolerance = 0.9) name = "Unknown" # 找出距離最近的人臉 face_distances = utils.face_distance(self.known_face_encodings, face_encoding) # 取出這個(gè)最近人臉的評(píng)分 best_match_index = np.argmin(face_distances) if matches[best_match_index]: name = self.known_face_names[best_match_index] face_names.append(name) rectangles = rectangles[:,0:4] #-----------------------------------------------# # 畫(huà)框~!~ #-----------------------------------------------# for (left, top, right, bottom), name in zip(rectangles, face_names): cv2.rectangle(draw, (left, top), (right, bottom), (0, 0, 255), 2) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(draw, name, (left , bottom - 15), font, 0.75, (255, 255, 255), 2) return draw if __name__ == "__main__": dududu = face_rec() video_capture = cv2.VideoCapture(1) while True: ret, draw = video_capture.read() dududu.recognize(draw) cv2.imshow('Video', draw) if cv2.waitKey(20) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
以上就是Keras目標(biāo)檢測(cè)mtcnn facenet搭建人臉識(shí)別平臺(tái)的詳細(xì)內(nèi)容,更多關(guān)于Keras目標(biāo)檢測(cè)mtcnn facenet人臉識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python PyWebIO實(shí)現(xiàn)網(wǎng)頁(yè)版數(shù)據(jù)查詢(xún)器
PyWebIO提供了一系列命令式的交互函數(shù)來(lái)在瀏覽器上獲取用戶(hù)輸入和進(jìn)行輸出,將瀏覽器變成了一個(gè)“富文本終端”,可以用于構(gòu)建簡(jiǎn)單的Web應(yīng)用或基于瀏覽器的GUI應(yīng)用。本文將利用PyWebIO制作一個(gè)網(wǎng)頁(yè)版的數(shù)據(jù)查詢(xún)器,感興趣的可以學(xué)習(xí)一下2021-12-12使用Python開(kāi)發(fā)游戲運(yùn)行腳本實(shí)現(xiàn)模擬點(diǎn)擊
這篇文章主要介紹了使用Python開(kāi)發(fā)游戲運(yùn)行腳本實(shí)現(xiàn)模擬點(diǎn)擊,這樣我們要想實(shí)現(xiàn)手游腳本開(kāi)發(fā)的第一步,就是下載Android模擬器,然后在對(duì)安卓模擬器進(jìn)行鼠標(biāo)和鍵盤(pán)的模擬,以此來(lái)實(shí)現(xiàn)自動(dòng)化游戲腳本,需要的朋友可以參考下2021-11-11詳解Django關(guān)于StreamingHttpResponse與FileResponse文件下載的最優(yōu)方法
這篇文章主要介紹了詳解Django關(guān)于StreamingHttpResponse與FileResponse文件下載的最優(yōu)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python實(shí)現(xiàn)ElGamal加密算法的示例代碼
ElGamal加密算法是一個(gè)基于迪菲-赫爾曼密鑰交換的非對(duì)稱(chēng)加密算法。這篇文章通過(guò)示例代碼給大家介紹Python實(shí)現(xiàn)ElGamal加密算法的相關(guān)知識(shí),感興趣的朋友一起看看吧2020-06-06Python基于pyopencv人臉識(shí)別并繪制GUI界面
本文詳細(xì)講解了Python基于pyopencv人臉識(shí)別并繪制GUI界面,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12django中上傳圖片分頁(yè)三級(jí)聯(lián)動(dòng)效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了django中上傳圖片分頁(yè)三級(jí)聯(lián)動(dòng)效果的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-08-08torchxrayvision包安裝過(guò)程(附pytorch1.6cpu版安裝)
這篇文章主要介紹了torchxrayvision包安裝過(guò)程(附pytorch1.6cpu版安裝),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python實(shí)現(xiàn)微信自動(dòng)好友驗(yàn)證,自動(dòng)回復(fù),發(fā)送群聊鏈接方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)微信自動(dòng)好友驗(yàn)證,自動(dòng)回復(fù),發(fā)送群聊鏈接方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02django虛擬環(huán)境(virtualenv)的創(chuàng)建
在使用django開(kāi)發(fā)項(xiàng)目的時(shí)候,一個(gè)環(huán)境只能對(duì)應(yīng)一個(gè)項(xiàng)目,若不安裝虛擬環(huán)境、都裝在系統(tǒng)里面,每次項(xiàng)目加載都需要加載所有的安裝包,本文就介紹django虛擬環(huán)境的安裝,感興趣的可以了解一下2021-08-08