Beispiel #1
0
def spoofing_face_patch_generate(spoofing_face, save_dir, patch_size=96, display=False):
    # nitial
    img_transform = ts.Compose([ts.RandomCrop(patch_size)])
    img_saved_list = get_file_list(save_dir)
    img_patch_num = len(img_saved_list)
    img_Image = Image.fromarray(cv2.cvtColor(spoofing_face, cv2.COLOR_BGR2RGB))
    data_len = 8
    seed_arr = np.arange(data_len)

    for i in range(data_len):
        img_transform = RandomCrop(size=patch_size, seed=seed_arr[i])
        try:
            img_patch = img_transform(img_Image)
            if display:
                img_patch_opencv = cv2.cvtColor(np.array(img_patch), cv2.COLOR_RGB2BGR)
                cv2.imshow("patch", img_patch_opencv)
                cv2.waitKey(0)

            img_patch_num += 1
            img_patch_name = "patch" + str(img_patch_num) + ".bmp"
            patch_save_path = save_dir + "/" + img_patch_name

            img_patch_opencv = cv2.cvtColor(np.array(img_patch), cv2.COLOR_RGB2BGR)
            cv2.imwrite(patch_save_path, img_patch_opencv)
        except Exception as e:
            print(e)
Beispiel #2
0
    def __init__(self,
                 living_dir,
                 spoofing_dir,
                 balance=True,
                 data_transform=None):

        self.living_path_list = get_file_list(living_dir)
        self.spoofing_path_list = get_file_list(spoofing_dir)

        # 间隔取样,控制数量
        if balance:
            self.spoofing_path_list = sorted(self.spoofing_path_list)
            balance_factor = int(
                np.floor(
                    len(self.spoofing_path_list) / len(self.living_path_list)))
            if balance_factor == 0:
                balance_factor = 1
            self.spoofing_path_list = self.spoofing_path_list[
                0:len(self.spoofing_path_list):balance_factor]
        self.img_path_list = self.spoofing_path_list + self.living_path_list
        self.data_transform = data_transform
def depth_cnn_test(pre_path, test_dir, label, isface):
    '''

    :param :
    :param pre_path: road to pretrain model
    :param test_dir: test img dir
    :param isface: img is face img or not, if not need to detect face
    :return:
    '''
    face_detector = FaceDection(model_name='cv2')
    model = net_baesd_depth()

    state_dict = torch.load(pre_path, map_location='cpu')
    model.load_state_dict(state_dict)
    model.eval()

    time_begin = datetime.datetime.now()
    file_list = get_file_list(test_dir)
    count = 1
    true_num = 1
    for file in file_list:
        img = cv2.imread(file)
        if img is None:
            continue

        result = depth_cnn_single(model=model,
                                  face_detector=face_detector,
                                  img=img,
                                  isface=isface)

        if result is None:
            continue
        if result == label:
            count += 1
            true_num += 1
        else:
            print(file)
            count += 1
    print(count, true_num, true_num / count)

    time_end = datetime.datetime.now()
    time_all = time_end - time_begin
    print("time_all", time_all.total_seconds())