詳解基于Facecognition+Opencv快速搭建人臉識(shí)別及跟蹤應(yīng)用
人臉識(shí)別技術(shù)已經(jīng)相當(dāng)成熟,面對(duì)滿大街的人臉識(shí)別應(yīng)用,像單位門(mén)禁、刷臉打卡、App解鎖、刷臉支付、口罩檢測(cè)........
作為一個(gè)圖像處理的愛(ài)好者,怎能放過(guò)人臉識(shí)別這一環(huán)呢!調(diào)研開(kāi)搞,發(fā)現(xiàn)了超實(shí)用的Facecognition!現(xiàn)在和大家分享下~~
Facecognition人臉識(shí)別原理大體可分為:
1、通過(guò)hog算子定位人臉,也可以用cnn模型,但本文沒(méi)試過(guò);
2、Dlib有專門(mén)的函數(shù)和模型,實(shí)現(xiàn)人臉68個(gè)特征點(diǎn)的定位。通過(guò)圖像的幾何變換(仿射、旋轉(zhuǎn)、縮放),使各個(gè)特征點(diǎn)對(duì)齊(將眼睛、嘴等部位移到相同位置);
3、訓(xùn)練一個(gè)神經(jīng)網(wǎng)絡(luò),將輸入的臉部圖像生成為128維的預(yù)測(cè)值。訓(xùn)練的大致過(guò)程為:將同一人的兩張不同照片和另一人的照片一起喂入神經(jīng)網(wǎng)絡(luò),不斷迭代訓(xùn)練,使同一人的兩張照片編碼后的預(yù)測(cè)值接近,不同人的照片預(yù)測(cè)值拉遠(yuǎn);
4、將陌生人臉預(yù)測(cè)為128維的向量,與人臉庫(kù)中的數(shù)據(jù)進(jìn)行比對(duì),找出閾值范圍內(nèi)歐氏距離最小的人臉,完成識(shí)別。
1 開(kāi)發(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安裝,這個(gè)自己搞不定,就別玩了~
pip install opencv-python pip install face-recognition pip install face-recognition-models pip install dlb
3 打造自己的人臉庫(kù)
通過(guò)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)練與保存
通過(guò)數(shù)據(jù)集進(jìn)行訓(xùn)練,得到人臉識(shí)別碼,以numpy數(shù)據(jù)形式保存(人臉識(shí)別碼)模型
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、人臉識(shí)別及跟蹤
通過(guò)opencv啟動(dòng)攝像頭并獲取視頻,加載訓(xùn)練好模型完成識(shí)別及跟蹤,為避免視頻卡頓設(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é)語(yǔ)
通過(guò)opencv啟動(dòng)攝像頭并獲取實(shí)時(shí)視頻,為避免過(guò)度卡頓采取隔幀處理;利用Facecognition實(shí)現(xiàn)模型的訓(xùn)練、保存、識(shí)別,二者結(jié)合實(shí)現(xiàn)了實(shí)時(shí)視頻人臉的多人識(shí)別及跟蹤,希望對(duì)大家有所幫助~!
到此這篇關(guān)于詳解基于Facecognition+Opencv快速搭建人臉識(shí)別及跟蹤應(yīng)用的文章就介紹到這了,更多相關(guān)Facecognition+Opencv人臉識(shí)別 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python基于opencv實(shí)現(xiàn)人臉識(shí)別
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(opencv版)
- OpenCV+face++實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別解鎖功能
- OpenCV實(shí)現(xiàn)人臉識(shí)別簡(jiǎn)單程序
- OpenCV + MFC實(shí)現(xiàn)簡(jiǎn)單人臉識(shí)別
- opencv實(shí)現(xiàn)簡(jiǎn)單人臉識(shí)別
- OpenCV Java實(shí)現(xiàn)人臉識(shí)別和裁剪功能
- Opencv EigenFace人臉識(shí)別算法詳解
- Opencv LBPH人臉識(shí)別算法詳解
相關(guān)文章
python之從文件讀取數(shù)據(jù)到list的實(shí)例講解
下面小編就為大家分享一篇python之從文件讀取數(shù)據(jù)到list的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python中的浮點(diǎn)數(shù)原理與運(yùn)算分析
這篇文章主要介紹了Python中的浮點(diǎn)數(shù)原理與運(yùn)算分析,結(jié)合實(shí)例形式分析了Python浮點(diǎn)數(shù)操作的常見(jiàn)錯(cuò)誤,并簡(jiǎn)單解釋了浮點(diǎn)數(shù)運(yùn)算的原理與比較運(yùn)算實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10python 列出面板數(shù)據(jù)所有變量名的示例代碼
在Python中,處理面板數(shù)據(jù)(Panel Data)通常使用pandas庫(kù),特別是當(dāng)數(shù)據(jù)以DataFrame或Panel,這篇文章主要介紹了python 列出面板數(shù)據(jù)所有變量名,需要的朋友可以參考下2024-06-06基于python實(shí)現(xiàn)MUI區(qū)域滾動(dòng)
這篇文章主要介紹的是python實(shí)現(xiàn)MUI區(qū)域滾動(dòng),MUI提供了區(qū)域滾動(dòng)的組件,使用時(shí)遵循DOM結(jié)構(gòu)就可以,下面來(lái)看看文章具體的實(shí)現(xiàn)內(nèi)容,需要的朋友可以參考一下2021-11-11詳解如何使用Pandas刪除DataFrame中的非數(shù)字類型數(shù)據(jù)
在數(shù)據(jù)處理和分析過(guò)程中,經(jīng)常會(huì)遇到需要清洗數(shù)據(jù)的情況,本文將詳細(xì)介紹如何使用Pandas刪除DataFrame中的非數(shù)字類型數(shù)據(jù),感興趣的小伙伴可以了解下2024-03-03Linux RedHat下安裝Python2.7開(kāi)發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Linux RedHat下安裝Python2.7、pip、ipython環(huán)境、eclipse和PyDev環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05