1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| import net import torch import os from face_alignment import align import numpy as np import pandas as pd
adaface_models = {'ir_101': "pretrained/adaface_ir101_webface12m.ckpt",}
def load_pretrained_model(architecture='ir_101'): assert architecture in adaface_models.keys() model = net.build_model(architecture) statedict = torch.load( adaface_models[architecture], map_location=torch.device('cpu'))['state_dict'] model_statedict = {key[6:]: val for key, val in statedict.items() if key.startswith('model.')} model.load_state_dict(model_statedict) model.eval() return model
def to_input(pil_rgb_image): tensor = None try: np_img = np.array(pil_rgb_image) brg_img = ((np_img[:, :, ::-1] / 255.) - 0.5) / 0.5 tensor = torch.tensor([brg_img.transpose(2, 0,1)]).float() except Exception : return tensor return tensor
def read_vector_pkl(db_path, adaface_model_name): """ @Time : 2023/06/16 12:10:47 @Author : liruilonger@gmail.com @Version : 1.0 @Desc : 读取特征向量文件 Args: Returns: df """ import pickle file_name = f"representations_adaface_{adaface_model_name}.pkl" file_name = file_name.replace("-", "_").lower() with open(f"{db_path}/{file_name}", "rb") as f: representations = pickle.load(f) df = pd.DataFrame(representations, columns=["identity", f"{adaface_model_name}_representation"]) return df
def build_vector_pkl(db_path, adaface_model_name='adaface_model'): """ @Time : 2023/06/16 11:40:23 @Author : liruilonger@gmail.com @Version : 1.0 @Desc : 构建特征向量文件 Args:
Returns: void """ import time from os import path from tqdm import tqdm import pickle
if os.path.isdir(db_path) is not True: raise ValueError("Passed db_path does not exist!")
file_name = f"representations_adaface_{adaface_model_name}.pkl" file_name = file_name.replace("-", "_").lower() if path.exists(db_path + "/" + file_name): pass else: employees = [] for r, _, f in os.walk(db_path): for file in f: if ( (".jpg" in file.lower()) or (".jpeg" in file.lower()) or (".png" in file.lower()) ): exact_path = r + "/" + file employees.append(exact_path)
if len(employees) == 0: raise ValueError( "没有任何图像在 ", db_path, " 文件夹! 验证此路径中是否存在.jpg或.png文件。", ) representations = []
pbar = tqdm( range(0, len(employees)), desc="生成向量特征文件中:😍😊🔬🔬🔬⚒️⚒️⚒️🎢🎢🎢🎢🎢", ) for index in pbar: employee = employees[index]
img_representation = get_represent(employee) instance = [] instance.append(employee) instance.append(img_representation) representations.append(instance) with open(f"{db_path}/{file_name}", "wb") as f: pickle.dump(representations, f)
def get_represent(path): """ @Time : 2023/06/16 11:54:11 @Author : liruilonger@gmail.com @Version : 1.0 @Desc : 获取脸部特征向量 Args: Returns: void """ feature = None aligned_rgb_img = align.get_aligned_face(path) bgr_tensor_input = to_input(aligned_rgb_img) if bgr_tensor_input is not None: feature, _ = model(bgr_tensor_input) else: print(f"无法提取脸部特征向量{path}") return feature
def findCosineDistance(source_representation, test_representation): """ @Time : 2023/06/16 12:19:27 @Author : liruilonger@gmail.com @Version : 1.0 @Desc : 计算两个向量之间的余弦相似度得分 Args: Returns: void """ import torch.nn.functional as F return F.cosine_similarity(source_representation, test_representation)
def demo1(): model_name = "test" build_vector_pkl(test_image_path,model_name) df = read_vector_pkl(test_image_path, model_name) for index, instance in df.iterrows(): source_representation = instance[f"{model_name}_representation"] print(source_representation) features.append(source_representation) similarity_scores = torch.cat(features) @ torch.cat(features).T print(similarity_scores)
def find(test_image_path,threshold=0.5): """ @Time : 2023/06/16 14:02:52 @Author : liruilonger@gmail.com @Version : 1.0 @Desc : 根据图片在人脸库比对找人 Args: Returns: void """ test_representation = get_represent(test_image_path) if test_representation is not None: reset = {} for index, instance in df.iterrows(): source_representation = instance[f"{model_name}_representation"] ten = findCosineDistance(source_representation,test_representation) reset[ten.item()]= instance["identity"] cosine_similarity = max(reset.keys()) print(f"💝💝💝💝💝💝💝💝💝💝 {cosine_similarity} 💝💝💝💝💝{threshold}") return cosine_similarity > threshold ,reset[cosine_similarity] else: return False,0
def marge(m1,m2): from PIL import Image import uuid image1 = Image.open(m1) image2 = Image.open(m2) width1, height1 = image1.size width2, height2 = image2.size new_image = Image.new("RGB", (width1 + width2, max(height1, height2))) new_image.paste(image1, (0, 0)) new_image.paste(image2, (width1, 0)) new_image.save(str(uuid.uuid4()).replace('-', '')+"new_image.jpg")
if __name__ == '__main__': import imutils from imutils import paths import cv2 import uuid model = load_pretrained_model('ir_101') test_image_path = 'face_alignment/ser' features = set() model_name = "test_img"
build_vector_pkl("face_alignment/test",model_name) df = read_vector_pkl("face_alignment/test", model_name) for path in paths.list_images(test_image_path): b, r = find(path,0.25) if b: if r not in features: features.add(r) marge(r,path) else: img = cv2.imread(path) cv2.imwrite('__not' + str(uuid.uuid4()).replace('-', '')+".jpg", img)
|