我们采用opencv自带的人脸模型
也可以自己创立模型可参考:
自创人脸模型
一、前期准备:
1.建立 一个文件夹dataset,用来存放人脸
2.建立 一个文件夹trainer,用来训练人脸,
OpenCV自带的人脸模型:
链接:https://pan.baidu.com/s/1y64L9pwmyBUpIOPtNJe3BA
提取码:p85p
二、编写代码:
1.编写录入人脸代码:
# -*- coding: utf-8 -*-
import cv2
import os
cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height
face_detector = cv2.CascadeClassifier('G:/haarcascade_frontalface_default.xml')
# For each person, enter one numeric face id
face_id = input('\n enter user id end press <return> ==> ')
print("\n [INFO] Initializing face capture. Look the camera and wait ...")
# Initialize individual sampling face count
count = 0
while(True):
ret, img = cam.read()
img = cv2.flip(img, 1) # flip video image vertically
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.5, 2)
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
count += 1
# Save the captured image into the datasets folder
cv2.imwrite("dataset/User."