基于opencv的人脸识别
LBPH人脸识别
import cv2 import numpy as np images=[] #刘诗诗 images.append(cv2.imread("./lss/1.png",0)) images.append(cv2.imread("./lss/2.png",0)) images.append(cv2.imread("./lss/3.png",0)) images.append(cv2.imread("./lss/4.png",0)) images.append(cv2.imread("./lss/5.png",0)) #刘亦菲 images.append(cv2.imread("./lyf/1.jpg",0)) images.append(cv2.imread("./lyf/2.jpg",0)) images.append(cv2.imread("./lyf/3.jpg",0)) images.append(cv2.imread("./lyf/4.jpg",0)) images.append(cv2.imread("./lyf/5.jpg",0)) #标签 labels=[0,0,0,0,0,1,1,1,1,1] #获取识别器 recognizer = cv2.face.LBPHFaceRecognizer_create() #训练 recognizer.train(images, np.array(labels)) #待识别照片 predict_image=cv2.imread("001.jpg",0) #识别 label,confidence= recognizer.predict(predict_image) print("label=",label) print("confidence=",confidence)
confidence为识别结果与模型之间的距离,0表示百分百准确,正常情况下小于50都可以认为可信,还是要根据项目的实际情况去跑大量测试来确定阈值。
EigenFaces人脸识别
import cv2 import numpy as np images=[] #刘诗诗 images.append(cv2.imread("./lss/1.jpg",0)) images.append(cv2.imread("./lss/2.jpg",0)) images.append(cv2.imread("./lss/3.jpg",0)) images.append(cv2.imread("./lss/4.jpg",0)) images.append(cv2.imread("./lss/5.jpg",0)) #刘亦菲 images.append(cv2.imread("./lyf/1.jpg",0)) images.append(cv2.imread("./lyf/2.jpg",0)) images.append(cv2.imread("./lyf/3.jpg",0)) images.append(cv2.imread("./lyf/4.jpg",0)) images.append(cv2.imread("./lyf/5.jpg",0)) #标签 labels=[0,0,0,0,0,1,1,1,1,1] #获取识别器 recognizer = cv2.face.EigenFaceRecognizer_create() #训练 recognizer.train(images, np.array(labels)) #待识别照片 predict_image=cv2.imread("10.jpg",0) #识别 label,confidence= recognizer.predict(predict_image) print("label=",label) print("confidence=",confidence)
confidence为识别结果与模型之间的距离,0表示百分百准确,正常情况下小于5000都可以认为可信。
Fisherfaces人脸识别
import cv2 import numpy as np images=[] #刘诗诗 images.append(cv2.imread("./lss/1.jpg",0)) images.append(cv2.imread("./lss/2.jpg",0)) images.append(cv2.imread("./lss/3.jpg",0)) images.append(cv2.imread("./lss/4.jpg",0)) images.append(cv2.imread("./lss/5.jpg",0)) #刘亦菲 images.append(cv2.imread("./lyf/1.jpg",0)) images.append(cv2.imread("./lyf/2.jpg",0)) images.append(cv2.imread("./lyf/3.jpg",0)) images.append(cv2.imread("./lyf/4.jpg",0)) images.append(cv2.imread("./lyf/5.jpg",0)) #标签 labels=[0,0,0,0,0,1,1,1,1,1] #获取识别器 recognizer = cv2.face.FisherFaceRecognizer_create() #训练 recognizer.train(images, np.array(labels)) #待识别照片 predict_image=cv2.imread("10.jpg",0) #识别 label,confidence= recognizer.predict(predict_image) print("label=",label) print("confidence=",confidence)
confidence为识别结果与模型之间的距离,0表示百分百准确,正常情况下小于5000都可以认为可信。