人脸识别相关代码
这个是网上别人做的视频的学习地址:
https://www.bilibili.com/video/BV1zc41197BA/?p=8&vd_source=e9167c654bb4523338a765358a8ac2af
手敲了一遍:
总结
我只能说自己训练的人脸效果实在是不好,还是没搞懂为啥别人的视频效果确实不错,或许是训练集问题
人脸识别
其中haarcascade_frontalface_default.xml,这个下载opencv以后就会有的文件,然后将其添加到项目中
import cv2 as cvdef detect():gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# 使用绝对路径加载级联分类器文件face_detector = cv.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')faces = face_detector.detectMultiScale(gray)for (x, y, w, h) in faces:cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)cv.imshow('result', img)cv.waitKey(0)img = cv.imread('1.jpeg')if __name__ == '__main__':detect()
检测视频中的人脸
import cv2 as cvdef detect(img):gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# 使用绝对路径加载级联分类器文件face_detector = cv.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')faces = face_detector.detectMultiScale(gray)for (x, y, w, h) in faces:cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)cv.imshow('result', img)cv.waitKey(0)def main():cap = cv.VideoCapture('video.mp4')while True:flag, frame = cap.read()if not flag:breakdetect(frame)if ord('q') == cv.waitKey(10):breakcv.destroyAllWindows()cap.release()if __name__ == '__main__':main()
训练数据
import osimport cv2 as cvimport numpy as npfrom PIL import Imagedef getImageAndLabels(path):# 使用绝对路径加载级联分类器文件face_detector = cv.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')faceSamples = []ids = []imagePaths = [os.path.join(path, f) for f in os.listdir(path)]for imagePaths in imagePaths:PIL_img = Image.open(imagePaths).convert('L')# 图像转为数组img_numpy = np.array(PIL_img, 'uint8')faces = face_detector.detectMultiScale(img_numpy)# 获取每张图像的idid = int(os.path.split(imagePaths)[1].split('.')[0])for x, y, w, h in faces:faceSamples.append(img_numpy[y:y + h, x:x + w])ids.append(id)return faceSamples, idsif __name__ == '__main__':path = './data/jm/'faces, ids = getImageAndLabels(path)recognizer = cv.face.LBPHFaceRecognizer_create()recognizer.train(faces, np.array(ids))# 保存文件recognizer.write('trainer.yml')
置信区间
import cv2 as cvdef detect(img):gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)# 使用绝对路径加载级联分类器文件face_detector = cv.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')faces = face_detector.detectMultiScale(gray)recognizer = cv.face.LBPHFaceRecognizer_create()recognizer.read('trainer.yml')for (x, y, w, h) in faces:roi_gray = gray[y:y + h, x:x + w]label, confidence = recognizer.predict(roi_gray)# 打印预测标签和置信度print(f"Predicted Label: {label}, Confidence: {confidence}")# 绘制矩形框cv.rectangle(img, (x, y), (x + w, y + h), (100, 255, 0), 2)# 计算文本位置text_x = x + int(w / 2) - 50text_y = y - 20# 绘制标签和置信度信息label_text = f"Label: {label}, Confidence: {confidence:.2f}"cv.putText(img, label_text, (text_x, text_y), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)cv.imshow('result', img)cv.waitKey(0)cv.destroyAllWindows()if __name__ == '__main__':path = './data/test/'detect(cv.imread(path + '9.jpg'))