opencv实现人脸识别案例

1.首先去git上面拷贝一份人脸特征数据文件 

gitee地址:https://gitee.com/mirrors/opencv/tree/4.x/data/haarcascades

 2.然后准备人脸图片

3.完整代码

import cv2

if __name__ == '__main__':
    img1 = cv2.imread('data/img_5.png')
    gray = cv2.cvtColor(img1, code=cv2.COLOR_BGR2GRAY)  # 黑白,数据变少
    # 人脸特征详细说明, 1万多行,根据这些特征来判定人脸
    face_detector = cv2.CascadeClassifier('data/haarcascade_frontalface_alt.xml')
    faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)   # scaleFactor缩放是1.1 结果返回检测到的人脸是坐标和宽高(x、y、w、h)
    print(faces)
    for x, y, w, h in faces:
        # pt1、pt2分别是左上角和右下角
        cv2.rectangle(img1, pt1=(x, y), pt2=(x + w, y + h), color=[0, 0, 255], thickness=2)
    cv2.imshow('sbn', img1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

4.运行结果

 

猜你喜欢

转载自blog.csdn.net/yueyue763184/article/details/128266239
今日推荐