OpenCV实现图片人脸检测

本文使用python实现

import cv2 as cv
import numpy as np


def face_detect_demo(src):
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    face_detector = cv.CascadeClassifier("D:/Python/Python38/Lib/site-packages/cv2/data/haarcascade_frontalface_alt_tree.xml")
    '''
    cv2.cv2.CascadeClassifier.CascadeClassifier def __init__(self,
             *args: Any,
             **kwargs: Any) -> None
    '''
    faces = face_detector.detectMultiScale(gray, 1.02, 5)
    '''
    def detectMultiScale(self,
                     image: Any,                # 输入待检测的图像,灰度
                     scaleFactor: Any = None,   # 尺度系数
                     minNeighbors: Any = None,  # 需要的邻域数
                     flags: Any = None,
                     minSize: Any = None,       # 最小检测窗口
                     maxSize: Any = None) -> None   # 最大检测窗口
    '''
    for x, y, w, h in faces:
        cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 2)   # 绘制矩形
        '''
        def rectangle(img: Any,             # 图像
              pt1: Any,                     # pt1 矩形的一个顶点
              pt2: Any,                     # pt2 矩形对角线上的另一个顶点
              color: Any,                   # 线条颜色 (RGB) 或亮度(灰度图像 )
              thickness: Any = None,        # 组成矩形的线条的粗细程度,取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形
              lineType: Any = None,         # 线条的类型
              shift: Any = None) -> None
        '''
    cv.imshow("face_detect_demo", src)


src = cv.imread("C:/Users/admin/Desktop/opencv-python/23.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

face_detect_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

OpenCV实现图片人脸检测_第1张图片

你可能感兴趣的:(python)