windows环境 python + opencv 加载 onnx 人脸检测模型识别人脸测试

参考博客:

        1. OpenCV-Python 4.5.4 人脸识别应用:https://blog.csdn.net/qq_36563273/article/details/121510440( 代码就是在此博客代码基础上改的,主要添加了人脸画框的逻辑 )

1. windows环境:win11

2. 安装 miniconda2-4.7.10:

3. 新建 python3.9 的 conda 环境:

        1. 创建 conda 环境并瞎下载 opencv、numpy:

        conda create --name conda_env_python_3_9  python=3.9
        conda  activate conda_env_python_3_9
        pip install opencv-python==4.5.4.58 -i https://pypi.tuna.tsinghua.edu.cn/simple
        pip install numpy==1.22.4 -i https://pypi.tuna.tsinghua.edu.cn/simple

         2. 查看安装情况:

 4. test.py:

import cv2

# 定义输入和变量
# new_shape = (300, 300)    # 统一缩放为 300*300
new_shape = (960, 540)      # 统一缩放为 300*300
cos_thresh = 0.363          # cos阈值,距离越大越接近
L2_thresh = 1.128           # L2阈值,距离越小越接近

faceDetectorModel = None
faceRecognizerModel = None

# 初始化模型:
def initOnnxModel():
    global faceDetectorModel
    global faceRecognizerModel
    faceDetectorModel = cv2.FaceDetectorYN.create('face_model/yunet.onnx', '', new_shape)
    faceRecognizerModel = cv2.FaceRecognizerSF.create('face_model/face_recognizer_fast.onnx', '')

# 对图片进行人脸检测的方法
def detectHumanFace4Image_v1( imagePath ):
    faceImage = cv2.imread( imagePath )
    faceImage = cv2.resize(faceImage, new_shape)
    # detect输出的是一个二维元祖,其中第二维是一个二维数组: n*15,n为人脸数,
    # 15为人脸的xywh和5个关键点(右眼瞳孔、左眼、鼻尖、右嘴角、左嘴角)的xy坐标及置信度
    faceDatas = faceDetectorModel.detect(faceImage)[1]
    face_count = len( faceDatas )
    print( "检测到",face_count,"个人脸" )
    # print( faceDatas  )
    for i in range( 0,face_count ):
        face_key_info_array = faceDatas[ i ]
        x1 = int( face_key_info_array[ 0 ] )
        y1 = int( face_key_info_array[ 1 ] )
        width = int( face_key_info_array[ 2 ] )
        height = int( face_key_info_array[ 3 ] )
        x2 = x1 + width
        y2 = y1 + height
        # 在图片上画矩形框
        cv2.rectangle(faceImage, (x1,y1), (x2,y2), (0,255,0), 1, cv2.LINE_8)
    cv2.imshow("image", faceImage)
    cv2.waitKey(0)

# 对图片进行人脸检测的方法
def detectHumanFace4Image_v2( faceImage ):
    # detect输出的是一个二维元祖,其中第二维是一个二维数组: n*15,n为人脸数,
    # 15为人脸的xywh和5个关键点(右眼瞳孔、左眼、鼻尖、右嘴角、左嘴角)的xy坐标及置信度
    faceDatas = faceDetectorModel.detect(faceImage)[1]
    if( faceDatas is None ):
        return
    face_count = len( faceDatas )
    print( "检测到",face_count,"个人脸" )
    # print( faceDatas  )
    for i in range( 0,face_count ):
        face_key_info_array = faceDatas[ i ]
        x1 = int( face_key_info_array[ 0 ] )
        y1 = int( face_key_info_array[ 1 ] )
        width = int( face_key_info_array[ 2 ] )
        height = int( face_key_info_array[ 3 ] )
        x2 = x1 + width
        y2 = y1 + height
        # 在图片上画矩形框
        cv2.rectangle(faceImage, (x1,y1), (x2,y2), (0,255,0), 1, cv2.LINE_8)
    cv2.imshow("image", faceImage)
    # cv2.waitKey(0)
    cv2.waitKey(1)

# 对视频进行轮廓检测的方法
def contoursCheck4Video( videoPath ):
    # 读取视频
    cap = cv2.VideoCapture( videoPath )
    # 获取视频帧数
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print( "frame_count of ", videoPath ," = ",frame_count )
    # 遍历每一帧
    for i in range(frame_count):
        # 读取帧
        ret, frame = cap.read()
        if ret:
            # 转灰度图
            gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # 进行阈值处理
            ret, thresh_frame = cv2.threshold(gray_frame, 100, 255, cv2.THRESH_BINARY)
            # 轮廓检测
            contours, hierarchy = cv2.findContours(thresh_frame, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            # 绘制轮廓
            cv2.drawContours(frame, contours, -1, (0, 0, 255), 3)
            # 显示每一帧
            cv2.imshow('frame', frame)
            cv2.waitKey(1)
    # 释放资源
    cap.release()
    cv2.destroyAllWindows()

# 对视频进行人脸检测的方法
def humanFaceCheck4Video( videoPath ):
    # 读取视频
    cap = cv2.VideoCapture( videoPath )
    # 获取视频帧数
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print( "frame_count of ", videoPath ," = ",frame_count )
    # 遍历每一帧
    for i in range(frame_count):
        # 读取帧
        ret, frame = cap.read()
        if ret:
            detectHumanFace4Image_v2( frame )
            # cv2.imshow('frame', frame)
            # cv2.waitKey(1)
    # 释放资源
    cap.release()
    cv2.destroyAllWindows()

# 初始化人脸检测模型
initOnnxModel()
# detectHumanFace( img1 )
# detectHumanFace( img2 )
videoPath = 'D:\素材\视频\\疯狂的麦克斯_small.mp4'
# contoursCheck4Video( videoPath )
humanFaceCheck4Video( videoPath )

# aligned_face1 = faceRecognizer.alignCrop(img1, faces1[1][0])    # 对齐后的图片
# feature1 = faceRecognizer.feature(aligned_face1);               # 128维特征

# faces2 = faceDetector.detect(img2)
# print( faces2 )
# aligned_face2 = faceRecognizer.alignCrop(img2, faces2[1][0])
# feature2 = faceRecognizer.feature(aligned_face2);

# cv2.imwrite('face_img/aligned1.jpg',aligned_face1)
# cv2.imwrite('face_img/aligned2.jpg',aligned_face2)

# 人脸匹配值打分:
# cos_score = faceRecognizer.match(feature1, feature2, 0)
# L2_score = faceRecognizer.match(feature1, feature2, 1)

5. 代码目录结构:

 6. 运行展示效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值