| 29 | INSIGHTFACE_DIR = os.path.join(folder_paths.models_dir, "insightface") |
| 30 | |
| 31 | def draw_kps(image_pil, kps, color_list=[(255,0,0), (0,255,0), (0,0,255), (255,255,0), (255,0,255)]): |
| 32 | stickwidth = 4 |
| 33 | limbSeq = np.array([[0, 2], [1, 2], [3, 2], [4, 2]]) |
| 34 | kps = np.array(kps) |
| 35 | |
| 36 | h, w, _ = image_pil.shape |
| 37 | out_img = np.zeros([h, w, 3]) |
| 38 | |
| 39 | for i in range(len(limbSeq)): |
| 40 | index = limbSeq[i] |
| 41 | color = color_list[index[0]] |
| 42 | |
| 43 | x = kps[index][:, 0] |
| 44 | y = kps[index][:, 1] |
| 45 | length = ((x[0] - x[1]) ** 2 + (y[0] - y[1]) ** 2) ** 0.5 |
| 46 | angle = math.degrees(math.atan2(y[0] - y[1], x[0] - x[1])) |
| 47 | polygon = cv2.ellipse2Poly((int(np.mean(x)), int(np.mean(y))), (int(length / 2), stickwidth), int(angle), 0, 360, 1) |
| 48 | out_img = cv2.fillConvexPoly(out_img.copy(), polygon, color) |
| 49 | out_img = (out_img * 0.6).astype(np.uint8) |
| 50 | |
| 51 | for idx_kp, kp in enumerate(kps): |
| 52 | color = color_list[idx_kp] |
| 53 | x, y = kp |
| 54 | out_img = cv2.circle(out_img.copy(), (int(x), int(y)), 10, color, -1) |
| 55 | |
| 56 | out_img_pil = PIL.Image.fromarray(out_img.astype(np.uint8)) |
| 57 | return out_img_pil |
| 58 | |
| 59 | class InstantID(torch.nn.Module): |
| 60 | def __init__(self, instantid_model, cross_attention_dim=1280, output_cross_attention_dim=1024, clip_embeddings_dim=512, clip_extra_context_tokens=16): |