作业记录 WEEK4

Assignment

  • 将训练好的活体检测模型使用到人脸识别系统中。

Steps

基本思路:以dlib为基础做的人脸识别系统为baseline.获取其人脸检测后的结果或人脸对齐后的结果,输入到活体检测模型中,给出结果。

考虑到输入为一段视频,可以看做只有color,并不包含ir以及depth图像,所以之前训练的fusion模型不适合,选择重新train一个只有color的活体检测模型。

CUDA_VISIBLE_DEVICES=0 python train_CyclicLR.py --model=model_A --image_mode=color --image_size=64 --cycle_num=1 --cycle_inter=10

训练完成后,把生成的对应model文件拷贝到正确的路径上:

cp ./models/model_A_color_64/checkpoint/global_min_acer_model.pth ~/work/liv_ob_detection//week4

FaceRecognize.py

主要实现从一段视频中导入图片序列,并进行活体检测功能。

            ## 活体检测
            is_attack_str="live"
            if not face_spoofing.classify(face_aling):
                print(" not humman\n ")   
                is_attack_str="attack"  
                # 框为红色
                frame=cv2.rectangle(frame,(det.left(),det.top()),(det.right(),det.bottom()),(0,0,255),2)

活体检测初始化:

    face_spoofing = FaceAnti() 

FaceAnti.py

主要功能:构建活体检测模型。

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import sys
import torch
import cv2
from imgaug import augmenters as iaa
import numpy as np
import torch.nn.functional as F
from collections import OrderedDict
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
pwd = os.path.abspath('./')
RESIZE_SIZE = 112

def TTA_36_cropps(image, target_shape=(32, 32, 3)):
    image = cv2.resize(image, (RESIZE_SIZE, RESIZE_SIZE))

    width, height, d = image.shape
    target_w, target_h, d = target_shape

    start_x = ( width - target_w) // 2
    start_y = ( height - target_h) // 2

    starts = [[start_x, start_y],

              [start_x - target_w, start_y],
              [start_x, start_y - target_w],
              [start_x + target_w, start_y],
              [start_x, start_y + target_w],

              [start_x + target_w, start_y + target_w],
              [start_x - target_w, start_y - target_w],
              [start_x - target_w, start_y + target_w],
              [start_x + target_w, start_y - target_w],
              ]

    images = []

    for start_index in starts:
        image_ = image.copy()
        x, y = start_index

        if x < 0:
            x = 0
        if y < 0:
            y = 0

        if x + target_w >= RESIZE_SIZE:
            x = RESIZE_SIZE - target_w-1
        if y + target_h >= RESIZE_SIZE:
            y = RESIZE_SIZE - target_h-1

        zeros = image_[x:x + target_w, y: y+target_h, :]

        image_ = zeros.copy()

        zeros = np.fliplr(zeros)
        image_flip_lr = zeros.copy()

        zeros = np.flipud(zeros)
        image_flip_lr_up = zeros.copy()

        zeros = np.fliplr(zeros)
        image_flip_up = zeros.copy()

        images.append(image_.reshape([1,target_shape[0],target_shape[1],target_shape[2]]))
        images.append(image_flip_lr.reshape([1,target_shape[0],target_shape[1],target_shape[2]]))
        images.append(image_flip_up.reshape([1,target_shape[0],target_shape[1],target_shape[2]]))
        images.append(image_flip_lr_up.reshape([1,target_shape[0],target_shape[1],target_shape[2]]))

    return images

定义FaceAnti类,初始化包含:加载模型,修改参数格式,修改输入图片格式。

class FaceAnti:
    def __init__(self):
        from FaceBagNet_model_A import Net
        self.net = Net(num_class=2, is_first_bn=True)
        model_path = "./global_min_acer_model.pth"
        if torch.cuda.is_available():
            state_dict = torch.load(model_path, map_location='cuda')
        else:
            state_dict = torch.load(model_path, map_location ='cpu')
        new_state_dict = OrderedDict()
        
        for k, v in state_dict.items():
            name = k[7:] # 'module.' 应该被去掉了
            new_state_dict[name] = v
        
        self.net.load_state_dict(new_state_dict)
        if torch.cuda.is_available():
            self.net = self.net.cuda()
    
    def classify(self,color):
        return self.detect(color)
    
    def detect(self,color):
        color = cv2.resize(color,(RESIZE_SIZE,RESIZE_SIZE))

        def color_augmentor(image, target_shape=(64,64,3), is_infer=False):
            if is_infer:
                augment_img = iaa.Sequential([
                    iaa.Fliplr(0),
                ])
            image = augment_img.augment_img(image)
            image = TTA_36_cropps(image, target_shape)
            return image

        color = color_augmentor(color, target_shape=(64,64,3), is_infer=True)

        n = len(color)
        color = np.concatenate(color, axis=0)

        image = color
        image = np.transpose(image,(0,3,1,2))
        image = image.astype(np.float32)
        image = image / 255.0
        input_image = torch.FloatTensor(image)
        if (len(input_image.size())==4) and torch.cuda.is_available():
            input_image = input_image.unsqueeze(0).cuda()
        elif (len(input_image.size()==4)) and not torch.cuda.is_available():
            input_image = input_image.unsqueeze(0)

        b, n, c, w, h = input_image.size()
        input_image = input_image.view(b*n, c, w, h)
        if torch.cuda.is_available():
            input_image = input_image.cuda()

            with torch.no_grad():
                logit,_,_ = self.net(input_image)
                logit = logit.view(b,n,2)
                logit = torch.mean(logit, dim=1, keepdim=False)
                prob = F.softmax(logit,1)

            print('probabilistic: ', prob)
            print('predict: ', np.argmax(prob.detach().cpu().numpy()))
            return np.argmax(prob.detach().cpu().numpy())


if __name__=="__main__":
    FA = FaceAnti()
    img = cv2.imread("person_not_exist.jpg",1)
    FA.detect(img)

测试运行

测试一下FaceAnti.py能不能用,主要是检查一下对应的参数格式是否正确(注意当前目录要改变):

cd
cd work/liv_ob_detection/week4
python FaceAnti.py

在这里插入图片描述
运行结果判断这个输入的图像是活体。
顺便截图一下测试运行使用的单张图片:person_not_exist.png
在这里插入图片描述
运行face_recognize.py文件,结果如下:
在这里插入图片描述
说明导入的视频检测结果也是活体。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值