今日总结-opencv实现人脸对比
结果如上图所示
代码如下:
#导入cv模块 import cv2 as cv import os import numpy as np #加载训练数据集文件 recognizer=cv.face.LBPHFaceRecognizer_create() recognizer.read('trainer/trainer1.yml') #准备识别的照片 img=cv.imread('20.jpg') gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY) face_detect=cv.CascadeClassifier('D:/opencv/opencv-4.7.0-windows/opencv/sources/data/haarcascades_cuda/haarcascade_frontalface_alt2.xml') face=face_detect.detectMultiScale(gray) for x,y,w,h in face: cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2) #人脸识别 id,confidence=recognizer.predict(gray[y:y+h,x:x+w]) print('标签id:',id,'置信评分',confidence) cv.imshow('result',img) #等待 while True: if ord('q')==cv.waitKey(0): break #释放内存 cv.destoryAllWindows()