(insightface, image, extract_kps=False)
| 169 | return (model,) |
| 170 | |
| 171 | def extractFeatures(insightface, image, extract_kps=False): |
| 172 | face_img = tensor_to_image(image) |
| 173 | out = [] |
| 174 | |
| 175 | insightface.det_model.input_size = (640,640) # reset the detection size |
| 176 | |
| 177 | for i in range(face_img.shape[0]): |
| 178 | for size in [(size, size) for size in range(640, 128, -64)]: |
| 179 | insightface.det_model.input_size = size # TODO: hacky but seems to be working |
| 180 | face = insightface.get(face_img[i]) |
| 181 | if face: |
| 182 | face = sorted(face, key=lambda x:(x['bbox'][2]-x['bbox'][0])*(x['bbox'][3]-x['bbox'][1]))[-1] |
| 183 | |
| 184 | if extract_kps: |
| 185 | out.append(draw_kps(face_img[i], face['kps'])) |
| 186 | else: |
| 187 | out.append(torch.from_numpy(face['embedding']).unsqueeze(0)) |
| 188 | |
| 189 | if 640 not in size: |
| 190 | print(f"\033[33mINFO: InsightFace detection resolution lowered to {size}.\033[0m") |
| 191 | break |
| 192 | |
| 193 | if out: |
| 194 | if extract_kps: |
| 195 | out = torch.stack(T.ToTensor()(out), dim=0).permute([0,2,3,1]) |
| 196 | else: |
| 197 | out = torch.stack(out, dim=0) |
| 198 | else: |
| 199 | out = None |
| 200 | |
| 201 | return out |
| 202 | |
| 203 | class InstantIDFaceAnalysis: |
| 204 | @classmethod |
no test coverage detected