Spaces:
Running
on
Zero
Running
on
Zero
Update pipeline_stable_diffusion_xl_instantid_img2img.py
Browse files
pipeline_stable_diffusion_xl_instantid_img2img.py
CHANGED
@@ -32,6 +32,40 @@ def reshape_tensor(x: torch.Tensor, heads: int) -> torch.Tensor:
|
|
32 |
bs, length, width = x.shape
|
33 |
return x.view(bs, length, heads, -1).transpose(1, 2)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
class PerceiverAttention(nn.Module):
|
37 |
def __init__(self, dim: int, dim_head: int = 64, heads: int = 8):
|
|
|
32 |
bs, length, width = x.shape
|
33 |
return x.view(bs, length, heads, -1).transpose(1, 2)
|
34 |
|
35 |
+
def draw_kps(image_pil, kps, color_list=[(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255)]):
|
36 |
+
import numpy as np
|
37 |
+
import cv2
|
38 |
+
from PIL import Image
|
39 |
+
|
40 |
+
stickwidth = 4
|
41 |
+
limbSeq = np.array([[0, 2], [1, 2], [3, 2], [4, 2]])
|
42 |
+
kps = np.array(kps)
|
43 |
+
|
44 |
+
w, h = image_pil.size
|
45 |
+
out_img = np.zeros([h, w, 3])
|
46 |
+
|
47 |
+
for i in range(len(limbSeq)):
|
48 |
+
index = limbSeq[i]
|
49 |
+
color = color_list[index[0]]
|
50 |
+
|
51 |
+
x = kps[index][:, 0]
|
52 |
+
y = kps[index][:, 1]
|
53 |
+
length = ((x[0] - x[1]) ** 2 + (y[0] - y[1]) ** 2) ** 0.5
|
54 |
+
angle = np.degrees(np.arctan2(y[0] - y[1], x[0] - x[1]))
|
55 |
+
polygon = cv2.ellipse2Poly(
|
56 |
+
(int(np.mean(x)), int(np.mean(y))), (int(length / 2), stickwidth), int(angle), 0, 360, 1
|
57 |
+
)
|
58 |
+
out_img = cv2.fillConvexPoly(out_img.copy(), polygon, color)
|
59 |
+
out_img = (out_img * 0.6).astype(np.uint8)
|
60 |
+
|
61 |
+
for idx_kp, kp in enumerate(kps):
|
62 |
+
color = color_list[idx_kp]
|
63 |
+
x, y = kp
|
64 |
+
out_img = cv2.circle(out_img.copy(), (int(x), int(y)), 10, color, -1)
|
65 |
+
|
66 |
+
out_img_pil = Image.fromarray(out_img.astype(np.uint8))
|
67 |
+
return out_img_pil
|
68 |
+
|
69 |
|
70 |
class PerceiverAttention(nn.Module):
|
71 |
def __init__(self, dim: int, dim_head: int = 64, heads: int = 8):
|