■环境
Python 3.6.0
Pycharm 2017.1.3
■库、库的版本
OpenCV 3.4.1 (cp36)
■haarcascades下载
https://github.com/opencv...import cv2
# 引入人像识别训练库“haarcascade_frontalface_default.xml
face_patterns = cv2.CascadeClassifier(r'D...haarcascade_frontalface_alt.xml')
# 读取图片
image = cv2.imread('C:/Users/x230/Desktop/DSCF9093.JPG')
# 获取识别到的人脸...faces = face_patterns.detectMultiScale(image, scaleFactor=1.1, minNeighbors=4, minSize=(40, 40))
# 将识别到的人脸框出来...for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 生成一张新的图片保存识别结果