步骤
1、读取包含人脸的图片
2.使用haar模型识别人脸
3.将识别结果用矩形框画出来
github:opencv haar
当前下载的在 haarcascades文件夹内
import cv2
def cv_show(neme, img):
cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('./images/faces1.jpg')
face_detector = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detections = face_detector.detectMultiScale(img_gray)
for (x, y, w, h) in detections:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 5)
cv_show('neme', img)