Advertisement

RGB简单人脸活体检测(Liveness Detection)

阅读量:

本文介绍了活体检测在人脸识别中的应用及其挑战。活体检测是识别对象是否为真人时的关键技术之一,在对抗性攻击中被广泛利用。目前主要的攻击方式包括照片类、视频类和面具类三种类型。其中面具类攻击最为棘手,因为其与真人的相似度较高。
针对活体检测方案的优化研究显示:
二维平面RGB相机:基于深度学习的方法效果显著;
红外相机:结合热成像特性可有效识别视频类和照片类的人脸;
三维深度相机:对照片和视频类样本均有较好的效果但对复杂面具样本表现较弱。
此外文章提供了一个完整的解决方案:
使用Silent-Face-Anti-Spoofing库进行摄像头测试
提供了详细的代码示例用于实时摄像头运行
建议使用模型融合方法进一步提升检测效果
文中还附有安装指导和相关模型下载链接供读者参考实践。

主要参考:
https://github.com/minivision-ai/Silent-Face-Anti-Spoofing"(该库的主要功能)
https://github.com/computervisioneng/face-attendance-system"(实际应用场景)

##概念: 活体检测主要指在人脸识别流程中对被识别对象的脸部进行额外确认的过程。当前针对人脸识别系统的攻击手段大致可分为三类:照片攻击、视频攻击以及面具攻击。其中面具攻击方案因其接近真实面部特征而尤为棘手。
目前主流的活体检测方案可以划分为三类:基于二维RGB视觉系统的活体检测方案、基于红外热成像技术设计的活体检测方案以及基于三维深度相机的数据分析方法。
其中二维RGB视觉系统的活体检测方案具有较高的效率;而基于红外热成像技术设计的活体检测方案不仅能够结合传统二维图像分析的优势,并且还能够充分利用红外热成像的独特特性来辅助识别工作。
此外,在实际应用中发现三维深度相机在处理照片与视频数据时展现出显著的优势;但面对复杂的面具形态时仍需进一步优化算法性能。

代码

通过网络平台下载Silent-Face-Anti-Spoofing项目的代码包,并将其解压至当前工作目录;在配置文件中修改相关参数以启用摄像头功能;将模型文件放置于./resources/anti_spoof_models目录中,并命名为活体检测融合模型;直接运行测试用例

复制代码
    python test.py --image_name your_image_name
增加camrea.py 摄像头代码,另外test.py稍微修改下
在这里插入图片描述

camrea.py(直接运行这个文件,就可以摄像头检测)

复制代码
    import cv2
    from test import test
    
    
    # Start video capture
    cap = cv2.VideoCapture(0) # By default webcam is index 0
    
    # Load gesture recognition class
    # gest = GestureRecognition(mode='eval')
    
    counter = 0
    while cap.isOpened():
    ret, img = cap.read()
    if not ret:
        break
    
    label,value,image_bbox = test(
                image_name=img,
                model_dir=r'E:\Silent-Face-Anti-Spoofing-master\Silent-Face-Anti-Spoofing-master\resources\anti_spoof_models',
                device_id=0
                )
    
    if label == 1:
        print("Image is Real Face. Score: {:.2f}.".format( value))
        result_text = "RealFace Score: {:.2f}".format(value)
        color = (255, 0, 0)
    else:
        print("Image is Fake Face. Score: {:.2f}.".format( value))
        result_text = "FakeFace Score: {:.2f}".format(value)
        color = (0, 0, 255)
    
    # print("Prediction cost {:.2f} s".format(test_speed))
    cv2.rectangle(
        img,
        (image_bbox[0], image_bbox[1]),
        (image_bbox[0] + image_bbox[2], image_bbox[1] + image_bbox[3]),
        color, 2)
    cv2.putText(
        img,
        result_text,
        (image_bbox[0], image_bbox[1] - 5),
       ## cv2.FONT_HERSHEY_COMPLEX, 0.5*img.shape[0]/1024, color)
        cv2.FONT_HERSHEY_COMPLEX, 1, color)
    
    cv2.imshow('frame', img)
    
    key = cv2.waitKey(1)
    if key==27:
        break
    
    
    cap.release()

请对test.py中的该核心函数进行优化以使其更加简洁

复制代码
    def test(image_name, model_dir, device_id):
    model_test = AntiSpoofPredict(device_id)
    image_cropper = CropImage()
    print(image_name.shape)
    #image = cv2.imread(SAMPLE_IMAGE_PATH + image_name)
    #image = cv2.imread(image_name)
    #result = check_image(image)
    #if result is False:
    #    return
    image = image_name
    image_bbox = model_test.get_bbox(image)
    prediction = np.zeros((1, 3))
    test_speed = 0
    # sum the prediction from single model's result
    for model_name in os.listdir(model_dir):
        h_input, w_input, model_type, scale = parse_model_name(model_name)
        param = {
            "org_img": image,
            "bbox": image_bbox,
            "scale": scale,
            "out_w": w_input,
            "out_h": h_input,
            "crop": True,
        }
        if scale is None:
            param["crop"] = False
        img = image_cropper.crop(**param)
        start = time.time()
        prediction += model_test.predict(img, os.path.join(model_dir, model_name))
        test_speed += time.time()-start
    
    # draw result of prediction
    label = np.argmax(prediction)
    value = prediction[0][label]/2
    return label,value,image_bbox
    # if label == 1:
    #     print("Image '{}' is Real Face. Score: {:.2f}.".format(image_name, value))
    #     result_text = "RealFace Score: {:.2f}".format(value)
    #     color = (255, 0, 0)
    # else:
    #     print("Image '{}' is Fake Face. Score: {:.2f}.".format(image_name, value))
    #     result_text = "FakeFace Score: {:.2f}".format(value)
    #     color = (0, 0, 255)
    # print("Prediction cost {:.2f} s".format(test_speed))
    # cv2.rectangle(
    #     image,
    #     (image_bbox[0], image_bbox[1]),
    #     (image_bbox[0] + image_bbox[2], image_bbox[1] + image_bbox[3]),
    #     color, 2)
    # cv2.putText(
    #     image,
    #     result_text,
    #     (image_bbox[0], image_bbox[1] - 5),
    #     cv2.FONT_HERSHEY_COMPLEX, 0.5*image.shape[0]/1024, color)
    
    # format_ = os.path.splitext(image_name)[-1]
    # result_image_name = image_name.replace(format_, "_result" + format_)
    # cv2.imwrite(SAMPLE_IMAGE_PATH + result_image_name, image)

红外IR 活体检测

可参考该模型详细信息:https://modelscope.cn/models/damo/cv_manual_face-liveness_flir/summary

安装:

复制代码
    pip install modelscope
    
    pip install mmcv-full==1.6.0  mmdet==2.25.0  numpy==1.23.0  ##测试下来这几个对应版本运行不会报错
复制代码
    from modelscope.pipelines import pipeline
    from modelscope.utils.constant import Tasks
    
    
    
    
    face_liveness_ir = pipeline(Tasks.face_liveness, 'damo/cv_manual_face-liveness_flir')
    # img_path = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/face_liveness_ir.jpg'
    img_path = r"C:\Users\loong\Downloads\candidate-face\0029_IR_frontface.jpg"
    result = face_liveness_ir(img_path)
    print(f'face liveness output: {result}.')
复制代码
    ##或者简单直接运行
    p = pipeline('face-liveness', 'damo/cv_manual_face-liveness_flir')
    # p('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/retina_face_detection.jpg',)
    p(r"C:\Users\loong\Downloads\candidate-face\0025_IR_frontface.jpg")
在这里插入图片描述
在这里插入图片描述

全部评论 (0)

还没有任何评论哟~