Python版本: 3.7
使用到的库: opencv

# -*- encoding:utf-8 -*-
import cv2


# 使用默认分类器
classifier_path = r'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'
file_path = r'E:\123\000D05CD0D8847D597A26CD7464C138C.jpg'

# 核心代码即以下三行
face_patterns = cv2.CascadeClassifier(classifier_path)
sample_image = cv2.imread(file_path)
faces = face_patterns.detectMultiScale(sample_image,scaleFactor=1.1,minNeighbors=5,minSize=(50, 50))

# 画矩形框标识出人脸位置
if len(faces) > 0:
	for (x, y, w, h) in faces:
		cv2.rectangle(sample_image, (x, y), (x+w, y+h), (0, 255, 0), 2)
else:
	print('No face detected')
# 保存图片
cv2.imwrite('lian.jpg', sample_image)
print('success')

效果如下图:

20200408144924.png