利用opencv自带的模型进行人脸识别
import cv2
faceCascade = cv2.CascadeClassifier('D:/img/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.15,
minNeighbors=5,
minSize=(5, 5)
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + w), (0, 255, 0), 2)
cv2.imshow('result', img)
cv2.waitKey(3)
cap.release()
cv2.destroyAllWindows()