# -*- coding: utf-8 -*-
"""
@author: Dell Created on Fri Jan 3 13:00:41 2020
opencv-python安装:pip install --default-timeout=1000 opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/
安装opencv-python需要安装numpy
open computer vision:开源计算机视觉
"""
import cv2
import numpy as np
img = cv2.imread("1.jpg")
#转成黑白图片
img_gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
#人脸数据,级联分类器,给定人脸特征数据,返回可以识别人脸的对象
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
#scaleFactor=1.3:缩放比例,minNeighbors=5,邻居数量;minSize=(20,20), maxSize=(100,100),规定人脸的最小最大尺寸
face_zone = detector.detectMultiScale(img_gray, scaleFactor=1.2, minNeighbors=2, minSize=(20,20), maxSize=(200,200))
print(face_zone)#[[105 30 224 224]]#前两个数代表人脸左上角的坐标,有两个数代表人脸的高度和宽度
for x,y,w,h in face_zone:
#使用黑白图片检测,绘制到彩色图片
#cv2读取的颜色通道为BGR,thickness=2,为框线的粗细
cv2.rectangle(img, pt1=(x,y), pt2=(x+w,y+h), color=[0,0,255], thickness=2)
color = np.random.randint(0,256,size=3).tolist()#numpy.ndarray,转换为list
cv2.circle(img, center=(x+w//2,y+h//2), radius=w//2, color=color)
cv2.imshow("face-detector",img)
cv2.waitKey(0)
cv2.destroyAllWindows()