def __init__(self, config, registered_ids, objects_frame=None, registered_images_embedding=None, batch_size=20): params = { 'model': config["arcface_model"]["path"], 'image_size': '112,112', 'gpu': 0, } self.model = face_model.FaceModel(params) self.image_size = [int(i) for i in params['image_size'].split(',')] if registered_images_embedding is not None: self.registered_images_embedding = registered_images_embedding else: n_images = objects_frame.shape[0] registered_images_embedding_list = [] for row_idx, batch_start, batch_end in chunk_with_index( range(n_images), batch_size): objects_embedding = self.model.get_faces_feature( objects_frame[batch_start:batch_end]) registered_images_embedding_list.append(objects_embedding) self.registered_images_embedding = np.concatenate( registered_images_embedding_list) self.registered_ids = registered_ids self.threshold = config["arcface_model"]["confidence_threshold"] self.unknown = 'Unknown'
def __init__(self, gpu_id=0): # 初始化模型的一些参数 ModelParam.gpu = gpu_id self.model = face_model.FaceModel(ModelParam) self.database = None self.index2name = [] self.feature_matrix = None
def main(args): args.det = 0 args.model = "C:\\Users\\Paul\\insightface\\models\\model-r50-am-lfw\\,0" model = face_model.FaceModel(args) img = cv2.imread(args.image) img = cv2.resize(img, (112, 112)) img = model.get_input(img) f1 = model.get_feature(img) print(f1)
def test_all(): im_path = '/Users/wangyiran/Documents/face_data_set' file = open('/Users/wangyiran/Documents/bad_case.txt', 'a') # 人脸检测 folders = os.listdir(im_path) cnt = 0 name_list = [] for folder in folders: # 跳过mac隐藏文件 if folder.startswith('.'): continue random.seed(time.time() * 100000 % 10000) sublist = [] imgs = os.listdir(os.path.join(im_path, folder)) random.shuffle(imgs) for i in range(0, len(imgs), 2): # 跳过mac隐藏文件 if imgs[i].startswith("."): continue try: model = face_model.FaceModel(args) pic_path1 = os.path.join(im_path, folder, imgs[i]) img1 = cv2.imread(pic_path1) img1 = model.get_input(img1) f1 = model.get_feature(img1) pic_path2 = os.path.join(im_path, folder, imgs[i+1]) img2 = cv2.imread(pic_path2) img2 = model.get_input(img2) f2 = model.get_feature(img2) dist = np.sum(np.square(f1-f2)) if dist < args.threshold: print('they are the same one') else: # pdb.set_trace() bad_case_folder = '/Users/wangyiran/Documents/face_data_set_result/' + str(cnt) os.mkdir(bad_case_folder) shutil.copy(pic_path1, bad_case_folder) shutil.copy(pic_path2, bad_case_folder) cnt += 1 file.writelines(pic_path1 + ',' + pic_path2+ ',' + str(dist) + ',' + str(args.threshold) + '\n') print('they are the different one') print(dist) except: continue file.close()
def test(): model = face_model.FaceModel(args) for i in range(1): cnt = 0 out_txt = open('/media/dhao/系统/05-weiwei/FR/dataset/test_result/combine_test/1.38/result' +str(i+1) + '.txt', 'a') file = open('/media/dhao/系统/05-weiwei/FR/politician_code/same_pairs.txt', 'r') lines = file.readlines() for j in range(len(lines)): name1 = lines[j].strip().split(',')[0] name2 = lines[j].strip().split(',')[1] try: img1 = cv2.imread(name1) img1 = model.get_input(img1) f1 = model.get_feature(img1) img2 = cv2.imread(name2) img2 = model.get_input(img2) f2 = model.get_feature(img2) dist = np.sqrt(np.sum(np.square(f1 - f2))) if dist < args.threshold: # print('they are the same one') out_txt.writelines(name1 + ',' + name2 + ',' + str(dist) + ',' + str(args.threshold) + '*' *3 + str(1) + '\n') else: out_txt.writelines(name1 + ',' + name2 + ',' + str(dist) + ',' + str(args.threshold) + '*' * 3 + str(0) + '\n') bad_case_folder = '/media/dhao/系统/05-weiwei/FR/dataset/test_result/combine_test/1.38/all' + str(i) + '/' + str(cnt) + '/' if not os.path.exists(bad_case_folder): os.mkdirs(bad_case_folder) if os.path.isdir(bad_case_folder): shutil.copy(name1, bad_case_folder) shutil.copy(name2, bad_case_folder) cnt += 1 else: sys.exit(0) except: continue file.close() out_txt.close()
def face_refine(): im_path = '/Users/wangyiran/Documents/face_data_set' result_path = '/Users/wangyiran/Documents/face_data_set_result' # 人脸检测 folders = os.listdir(im_path) cnt = 0 name_list = [] for folder in folders: # 跳过mac隐藏文件 if folder.startswith('.'): continue random.seed(time.time() * 100000 % 10000) # 在结果集中建立文件夹 result_dir = os.path.join(result_path, folder) if not os.path.exists(result_dir): os.mkdir(result_dir) sublist = [] imgs = os.listdir(os.path.join(im_path, folder)) random.shuffle(imgs) for i in range(0, len(imgs), 1): # 跳过mac隐藏文件 if imgs[i].startswith("."): continue try: model = face_model.FaceModel(args) pic_path1 = os.path.join(im_path, folder, imgs[i]) img1 = cv2.imread(pic_path1) img1 = model.get_input(img1) # f1 = model.get_feature(img1) if type(img1) == np.ndarray: if not os.path.exists(os.path.join(result_path, folder)): os.mkdir(os.path.join(result_path, folder)) cv2.imwrite(os.path.join(result_path, folder, imgs[i]), np.transpose(img1, (1, 2, 0))[:, :, ::-1]) except: print('错误:文件名:' + folder + '/' + str(imgs[i])) continue
def test_Faces(args): model = face_model.FaceModel(args) imgs = os.listdir(args.image_path) flag = False # 设置阈值,这两个阈值用来判定该人是否是库里的人 a = zip([args.threshold1], [args.threshold2]) for k, v in a: for img in imgs: if img.startswith('.'): continue pic = cv2.imread(os.path.join(args.image_path, img)) pic = model.get_input(pic) if pic is None: continue else: f1 = model.get_feature(pic) for i in range(faces.shape[0]): cnt = 0 for j in range(faces.shape[1]): dist = np.sqrt(np.sum(np.square(f1 - faces[i][j]))) # 如果与库中某人距离小于阈值1 if dist < k: cnt += 1 # 如果与库中某个人的相似度大于阈值2,则证明是该人 if cnt >= v: name = labels[i] testresult.writelines( os.path.join(args.image_path, img) + ' is ' + name + '\n') print( os.path.join(args.image_path, img) + ' is ' + name) flag = True if flag is False: print("image:" + os.path.join(args.image_path, img) + ' is not in the gallery, refused!' + '\n') testresult.close()
def compute_sim(): model = face_model.FaceModel(args) out_txt = open('/media/dhao/系统/05-weiwei/FR/politician_code/result_dist.txt', 'a') file = open('/media/dhao/系统/05-weiwei/FR/politician_code/same_pairs.txt', 'r') lines = file.readlines() for j in range(len(lines)): name1 = lines[j].strip().split(',')[0] name2 = lines[j].strip().split(',')[1] try: img1 = cv2.imread(name1) img1 = model.get_input(img1) f1 = model.get_feature(img1) img2 = cv2.imread(name2) img2 = model.get_input(img2) f2 = model.get_feature(img2) dist = np.sqrt(np.sum(np.square(f1 - f2))) out_txt.writelines(name1 + ',' + name2 + ',' + ',' + str(dist) + '\n') except: continue file.close() out_txt.close()
image_set = get_dataset(args.data_dir) image_list, label_list = get_image_paths_and_labels(image_set) path_exp = os.path.expanduser(args.data_dir) classes = [ path for path in os.listdir(path_exp) if os.path.isdir(os.path.join(path_exp, path)) ] label_strings = [ name for name in classes if os.path.isdir(os.path.join(path_exp, name)) ] # pdb.set_trace() # 初始化人脸检测模型 model = face_model.FaceModel(args) # embedding_array 是一个 类别数*每个类别的图片数*每张图片特征向量长度 的numpy,这里每个类别有50张,可以更改。 embedding_array = np.zeros((len(classes), args.num_per_class, 512)) for i in range(0, len(classes)): for j in range(args.num_per_class): try: pic = cv2.imread(image_list[i * args.num_per_class + j]) print(image_list[i * args.num_per_class + j]) if pic is None: print(image_list[j]) else: pic = model.get_input(pic) feature = model.get_feature(pic) embedding_array[i, j, :] = feature except:
def __init__(self): self.model = face_model.FaceModel(args)