詳解基于Facecognition+Opencv快速搭建人臉識別及跟蹤應(yīng)用
人臉識別技術(shù)已經(jīng)相當(dāng)成熟,面對滿大街的人臉識別應(yīng)用,像單位門禁、刷臉打卡、App解鎖、刷臉支付、口罩檢測........
作為一個圖像處理的愛好者,怎能放過人臉識別這一環(huán)呢!調(diào)研開搞,發(fā)現(xiàn)了超實用的Facecognition!現(xiàn)在和大家分享下~~
Facecognition人臉識別原理大體可分為:
1、通過hog算子定位人臉,也可以用cnn模型,但本文沒試過;
2、Dlib有專門的函數(shù)和模型,實現(xiàn)人臉68個特征點的定位。通過圖像的幾何變換(仿射、旋轉(zhuǎn)、縮放),使各個特征點對齊(將眼睛、嘴等部位移到相同位置);
3、訓(xùn)練一個神經(jīng)網(wǎng)絡(luò),將輸入的臉部圖像生成為128維的預(yù)測值。訓(xùn)練的大致過程為:將同一人的兩張不同照片和另一人的照片一起喂入神經(jīng)網(wǎng)絡(luò),不斷迭代訓(xùn)練,使同一人的兩張照片編碼后的預(yù)測值接近,不同人的照片預(yù)測值拉遠(yuǎn);
4、將陌生人臉預(yù)測為128維的向量,與人臉庫中的數(shù)據(jù)進(jìn)行比對,找出閾值范圍內(nèi)歐氏距離最小的人臉,完成識別。
1 開發(fā)環(huán)境
PyCharm: PyCharm Community Edition 2020.3.2 x64
Python:Python 3.8.7
Opencv:opencv-python 4.5.1.48
Facecognition:1.3.0
Dlb:dlb 0.5.0
2 環(huán)境搭建
本文不做PyCharm和Python安裝,這個自己搞不定,就別玩了~
pip install opencv-python pip install face-recognition pip install face-recognition-models pip install dlb
3 打造自己的人臉庫
通過opencv、facecogniton定位人臉并保存人臉頭像,生成人臉數(shù)據(jù)集,代碼如下:
import face_recognition import cv2 import os def builddataset(): Video_face = cv2.VideoCapture(0) num=0 while True: flag, frame = Video_face.read(); if flag: cv2.imshow('frame', frame) cv2.waitKey(2) else: break face_locations = face_recognition.face_locations(frame) if face_locations: x_face = frame[face_locations[0][0]-50:face_locations[0][2]+50, face_locations[0][3]-50:face_locations[0][1]+50]; #x_face = cv2.resize(x_face, dsize=(200, 200)); bo_photo = cv2.imwrite("%s\%d.jpg" % ("traindataset/ylb", num), x_face); print("保存成功:%d" % num) num=num+1 else: print("****未檢查到頭像****") Video_face.release() if __name__ == '__main__': builddataset(); pass
4、模型訓(xùn)練與保存
通過數(shù)據(jù)集進(jìn)行訓(xùn)練,得到人臉識別碼,以numpy數(shù)據(jù)形式保存(人臉識別碼)模型
def __init__(self, trainpath,labelname,modelpath, predictpath): self.trainpath = trainpath self.labelname = labelname self.modelpath = modelpath self.predictpath = predictpath # no doc def train(self, trainpath, modelpath): encodings = [] dirs = os.listdir(trainpath) for k,dir in enumerate(dirs): filelist = os.listdir(trainpath+'/'+dir) for i in range(0, len(filelist)): imgname = trainpath + '/'+dir+'/%d.jpg' % (i) picture_of_me = face_recognition.load_image_file(imgname) face_locations = face_recognition.face_locations(picture_of_me) if face_locations: print(face_locations) my_face_encoding = face_recognition.face_encodings(picture_of_me, face_locations)[0] encodings.append(my_face_encoding) if encodings: numpy.save(modelpath, encodings) print(len(encodings)) print("model train is sucess") else: print("model train is failed")
5、人臉識別及跟蹤
通過opencv啟動攝像頭并獲取視頻,加載訓(xùn)練好模型完成識別及跟蹤,為避免視頻卡頓設(shè)置了隔幀處理。
def predicvideo(self,names,model): Video_face = cv2.VideoCapture(0) num=0 recongnition=[] unknown_face_locations=[] while True: flag, frame = Video_face.read(); frame = cv2.flip(frame, 1) # 鏡像操作 num=num+1 if flag: self.predictpeople(num, recongnition,unknown_face_locations,frame, names, encodings) else: break Video_face.release() def predictpeople(self, condition,recongnition,unknown_face_locations,unknown_picture,labels,encodings): if condition%5==0: face_locations = face_recognition.face_locations(unknown_picture) unknown_face_encoding = face_recognition.face_encodings(unknown_picture,face_locations) unknown_face_locations.clear() recongnition.clear() for index, value in enumerate(unknown_face_encoding): unknown_face_locations.append(face_locations[index]) results = face_recognition.compare_faces(encodings, value, 0.4) splitresult = numpy.array_split(results, len(labels)) trueNum=[] a1 = '' for item in splitresult: number = numpy.sum(item) trueNum.append(number) if numpy.max(trueNum) > 0: id = numpy.argsort(trueNum)[-1] a1 = labels[id] cv2.rectangle(unknown_picture, pt1=(unknown_face_locations[index][1], unknown_face_locations[index][0]), pt2=(unknown_face_locations[index][3], unknown_face_locations[index][2]), color=[0, 0, 255], thickness=2); cv2.putText(unknown_picture, a1, (unknown_face_locations[index][1], unknown_face_locations[index][0]), cv2.FONT_ITALIC, 1, [0, 0, 255], 2); else: a1 = "unkown" cv2.rectangle(unknown_picture, pt1=(unknown_face_locations[index][1], unknown_face_locations[index][0]), pt2=(unknown_face_locations[index][3], unknown_face_locations[index][2]), color=[0, 0, 255], thickness=2); cv2.putText(unknown_picture, a1, (unknown_face_locations[index][1], unknown_face_locations[index][0]), cv2.FONT_ITALIC, 1, [0, 0, 255], 2); recongnition.append(a1) else: self.drawRect(unknown_picture,recongnition,unknown_face_locations) cv2.imshow('face', unknown_picture) cv2.waitKey(1)
6、結(jié)語
通過opencv啟動攝像頭并獲取實時視頻,為避免過度卡頓采取隔幀處理;利用Facecognition實現(xiàn)模型的訓(xùn)練、保存、識別,二者結(jié)合實現(xiàn)了實時視頻人臉的多人識別及跟蹤,希望對大家有所幫助~!
到此這篇關(guān)于詳解基于Facecognition+Opencv快速搭建人臉識別及跟蹤應(yīng)用的文章就介紹到這了,更多相關(guān)Facecognition+Opencv人臉識別 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python之從文件讀取數(shù)據(jù)到list的實例講解
下面小編就為大家分享一篇python之從文件讀取數(shù)據(jù)到list的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04python 列出面板數(shù)據(jù)所有變量名的示例代碼
在Python中,處理面板數(shù)據(jù)(Panel Data)通常使用pandas庫,特別是當(dāng)數(shù)據(jù)以DataFrame或Panel,這篇文章主要介紹了python 列出面板數(shù)據(jù)所有變量名,需要的朋友可以參考下2024-06-06詳解如何使用Pandas刪除DataFrame中的非數(shù)字類型數(shù)據(jù)
在數(shù)據(jù)處理和分析過程中,經(jīng)常會遇到需要清洗數(shù)據(jù)的情況,本文將詳細(xì)介紹如何使用Pandas刪除DataFrame中的非數(shù)字類型數(shù)據(jù),感興趣的小伙伴可以了解下2024-03-03Linux RedHat下安裝Python2.7開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Linux RedHat下安裝Python2.7、pip、ipython環(huán)境、eclipse和PyDev環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05