您好,登錄后才能下訂單哦!
使用Facecognition與Opencv怎么實現一個人臉識別功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
Facecognition人臉識別原理大體可分為:
1、通過hog算子定位人臉,也可以用cnn模型,但本文沒試過;
2、Dlib有專門的函數和模型,實現人臉68個特征點的定位。通過圖像的幾何變換(仿射、旋轉、縮放),使各個特征點對齊(將眼睛、嘴等部位移到相同位置);
3、訓練一個神經網絡,將輸入的臉部圖像生成為128維的預測值。訓練的大致過程為:將同一人的兩張不同照片和另一人的照片一起喂入神經網絡,不斷迭代訓練,使同一人的兩張照片編碼后的預測值接近,不同人的照片預測值拉遠;
4、將陌生人臉預測為128維的向量,與人臉庫中的數據進行比對,找出閾值范圍內歐氏距離最小的人臉,完成識別。
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
本文不做PyCharm和Python安裝,這個自己搞不定,就別玩了~
pip install opencv-python pip install face-recognition pip install face-recognition-models pip install dlb
通過opencv、facecogniton定位人臉并保存人臉頭像,生成人臉數據集,代碼如下:
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
通過數據集進行訓練,得到人臉識別碼,以numpy數據形式保存(人臉識別碼)模型
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")
通過opencv啟動攝像頭并獲取視頻,加載訓練好模型完成識別及跟蹤,為避免視頻卡頓設置了隔幀處理。
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)
關于使用Facecognition與Opencv怎么實現一個人臉識別功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。