参考了很多资料,自己改的,但是感觉人多的时候就不太准确,会持续优化。。。。。(感觉用目标检测yolov4比较好)
import cv2
# 图片人脸识别
face_patterns = cv2.CascadeClassifier('D:\python course/tuxiangshibie/venv\Lib\site-packages\cv2\data/haarcascade_frontalface_default.xml')
sample_image = cv2.imread('D:\python course/tuxiangshibie\data/20200317.jpeg') # 原图
faces = face_patterns.detectMultiScale(sample_image, scaleFactor=1.1, minNeighbors=5, minSize=(100, 100))
for (x, y, w, h) in faces: # 为每个人脸画一个框
cv2.rectangle(sample_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imwrite('D:\python course/tuxiangshibie\data/20200317_detected.png', sample_image) # 标注人脸的图像