Spaces:
Runtime error
Runtime error
Commit
Β·
62a1fe7
1
Parent(s):
3327b8b
Initial commit
Browse files- .gitattributes +16 -0
- README.md +6 -5
- app.py +193 -0
- choker/necklace_1.png +3 -0
- choker/necklace_11.png +3 -0
- choker/necklace_12.png +3 -0
- choker/necklace_20.png +3 -0
- choker/necklace_27.png +3 -0
- choker/necklace_29.png +3 -0
- choker/necklace_48.png +3 -0
- choker/necklace_51.png +3 -0
- choker/necklace_56.png +3 -0
- choker/necklace_8.png +3 -0
- long_haram/necklace_1.png +0 -0
- long_haram/necklace_10.png +0 -0
- long_haram/necklace_2.png +0 -0
- long_haram/necklace_3.png +0 -0
- long_haram/necklace_4.png +0 -0
- long_haram/necklace_5.png +0 -0
- long_haram/necklace_6.png +0 -0
- long_haram/necklace_7.png +0 -0
- long_haram/necklace_8.png +0 -0
- long_haram/necklace_9.png +3 -0
- requirements.txt +14 -0
- short_necklace/necklace_10.png +0 -0
- short_necklace/necklace_13.png +0 -0
- short_necklace/necklace_14.png +3 -0
- short_necklace/necklace_2.png +0 -0
- short_necklace/necklace_3.png +0 -0
- short_necklace/necklace_4.png +3 -0
- short_necklace/necklace_5.png +3 -0
- short_necklace/necklace_6.png +3 -0
- short_necklace/necklace_7.png +0 -0
- short_necklace/necklace_9.png +0 -0
- without_necklace/person_1.png +0 -0
- without_necklace/person_2.png +0 -0
- without_necklace/person_3.png +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,19 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
choker/necklace_1.png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
choker/necklace_11.png filter=lfs diff=lfs merge=lfs -text
|
38 |
+
choker/necklace_12.png filter=lfs diff=lfs merge=lfs -text
|
39 |
+
choker/necklace_20.png filter=lfs diff=lfs merge=lfs -text
|
40 |
+
choker/necklace_27.png filter=lfs diff=lfs merge=lfs -text
|
41 |
+
choker/necklace_29.png filter=lfs diff=lfs merge=lfs -text
|
42 |
+
choker/necklace_48.png filter=lfs diff=lfs merge=lfs -text
|
43 |
+
choker/necklace_51.png filter=lfs diff=lfs merge=lfs -text
|
44 |
+
choker/necklace_56.png filter=lfs diff=lfs merge=lfs -text
|
45 |
+
choker/necklace_8.png filter=lfs diff=lfs merge=lfs -text
|
46 |
+
long_haram/necklace_9.png filter=lfs diff=lfs merge=lfs -text
|
47 |
+
short_necklace/necklace_14.png filter=lfs diff=lfs merge=lfs -text
|
48 |
+
short_necklace/necklace_4.png filter=lfs diff=lfs merge=lfs -text
|
49 |
+
short_necklace/necklace_5.png filter=lfs diff=lfs merge=lfs -text
|
50 |
+
short_necklace/necklace_6.png filter=lfs diff=lfs merge=lfs -text
|
51 |
+
without_necklace/person_4.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Necklace And Clothing Try On
|
3 |
+
emoji: π
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.29.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: mit
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### FINAL COMBINED
|
2 |
+
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
from PIL import Image
|
8 |
+
from PIL.ImageOps import grayscale
|
9 |
+
import cv2
|
10 |
+
import torch
|
11 |
+
import gc
|
12 |
+
import math
|
13 |
+
import cvzone
|
14 |
+
from cvzone.PoseModule import PoseDetector
|
15 |
+
import spaces
|
16 |
+
|
17 |
+
# images
|
18 |
+
choker_images = [Image.open(os.path.join("choker", x)) for x in os.listdir("choker")]
|
19 |
+
short_necklaces = [Image.open(os.path.join("short_necklace", x)) for x in os.listdir("short_necklace")]
|
20 |
+
long_necklaces = [Image.open(os.path.join("long_haram", x)) for x in os.listdir("long_haram")]
|
21 |
+
person_images = [Image.open(os.path.join("without_necklace", x)) for x in os.listdir("without_necklace")]
|
22 |
+
|
23 |
+
# initialising the stable diffusion model
|
24 |
+
model_id = "stabilityai/stable-diffusion-2-inpainting"
|
25 |
+
pipeline = StableDiffusionInpaintPipeline.from_pretrained(
|
26 |
+
model_id, torch_dtype=torch.float16
|
27 |
+
)
|
28 |
+
pipeline = pipeline.to("cuda")
|
29 |
+
|
30 |
+
# functions
|
31 |
+
def clearFunc():
|
32 |
+
torch.cuda.empty_cache()
|
33 |
+
gc.collect()
|
34 |
+
|
35 |
+
def necklaceTryOnPipeline(image, jewellery):
|
36 |
+
global binaryMask
|
37 |
+
|
38 |
+
image = np.array(image)
|
39 |
+
copy_image = image.copy()
|
40 |
+
jewellery = np.array(jewellery)
|
41 |
+
|
42 |
+
detector = PoseDetector()
|
43 |
+
|
44 |
+
image = detector.findPose(image)
|
45 |
+
lmList, bBoxInfo = detector.findPosition(image, bboxWithHands=False, draw=False)
|
46 |
+
|
47 |
+
pt12, pt11, pt10, pt9 = (
|
48 |
+
lmList[12][:2],
|
49 |
+
lmList[11][:2],
|
50 |
+
lmList[10][:2],
|
51 |
+
lmList[9][:2],
|
52 |
+
)
|
53 |
+
|
54 |
+
avg_x1 = int(pt12[0] + (pt10[0] - pt12[0]) / 2)
|
55 |
+
avg_y1 = int(pt12[1] - (pt12[1] - pt10[1]) / 2)
|
56 |
+
|
57 |
+
avg_x2 = int(pt11[0] - (pt11[0] - pt9[0]) / 2)
|
58 |
+
avg_y2 = int(pt11[1] - (pt11[1] - pt9[1]) / 2)
|
59 |
+
|
60 |
+
image_gray = cv2.cvtColor(jewellery, cv2.COLOR_BGRA2GRAY)
|
61 |
+
|
62 |
+
if avg_y2 < avg_y1:
|
63 |
+
angle = math.ceil(
|
64 |
+
detector.findAngle(
|
65 |
+
p1=(avg_x2, avg_y2), p2=(avg_x1, avg_y1), p3=(avg_x2, avg_y1)
|
66 |
+
)[0]
|
67 |
+
)
|
68 |
+
else:
|
69 |
+
angle = math.ceil(
|
70 |
+
detector.findAngle(
|
71 |
+
p1=(avg_x2, avg_y2), p2=(avg_x1, avg_y1), p3=(avg_x2, avg_y1)
|
72 |
+
)[0]
|
73 |
+
)
|
74 |
+
angle = angle * -1
|
75 |
+
|
76 |
+
xdist = avg_x2 - avg_x1
|
77 |
+
origImgRatio = xdist / jewellery.shape[1]
|
78 |
+
ydist = jewellery.shape[0] * origImgRatio
|
79 |
+
|
80 |
+
for offset_orig in range(image_gray.shape[1]):
|
81 |
+
pixel_value = image_gray[0, :][offset_orig]
|
82 |
+
if (pixel_value != 255) & (pixel_value != 0):
|
83 |
+
break
|
84 |
+
else:
|
85 |
+
continue
|
86 |
+
|
87 |
+
offset = int(0.8 * xdist * (offset_orig / jewellery.shape[1]))
|
88 |
+
jewellery = cv2.resize(
|
89 |
+
jewellery, (int(xdist), int(ydist)), interpolation=cv2.INTER_CUBIC
|
90 |
+
)
|
91 |
+
jewellery = cvzone.rotateImage(jewellery, angle)
|
92 |
+
y_coordinate = avg_y1 - offset
|
93 |
+
available_space = copy_image.shape[0] - y_coordinate
|
94 |
+
extra = jewellery.shape[0] - available_space
|
95 |
+
if extra > 0:
|
96 |
+
jewellery = jewellery[extra + 10 :, :]
|
97 |
+
return necklaceTryOnPipeline(
|
98 |
+
Image.fromarray(copy_image), Image.fromarray(jewellery)
|
99 |
+
)
|
100 |
+
else:
|
101 |
+
result = cvzone.overlayPNG(copy_image, jewellery, (avg_x1, y_coordinate))
|
102 |
+
# masking
|
103 |
+
blackedNecklace = np.zeros(shape = copy_image.shape)
|
104 |
+
# overlay
|
105 |
+
cvzone.overlayPNG(blackedNecklace, jewellery, (avg_x1, y_coordinate))
|
106 |
+
blackedNecklace = cv2.cvtColor(blackedNecklace.astype(np.uint8), cv2.COLOR_BGR2GRAY)
|
107 |
+
binaryMask = blackedNecklace * ((blackedNecklace > 5) * 255)
|
108 |
+
binaryMask[binaryMask >= 255] = 255
|
109 |
+
binaryMask[binaryMask < 255] = 0
|
110 |
+
return Image.fromarray(result.astype(np.uint8)), Image.fromarray(binaryMask.astype(np.uint8))
|
111 |
+
|
112 |
+
# SD Model
|
113 |
+
@spaces.GPU
|
114 |
+
def sd_inpaint(image, mask):
|
115 |
+
# image = Image.fromarray(image)
|
116 |
+
# mask = Image.fromarray(mask)
|
117 |
+
|
118 |
+
jewellery_mask = Image.fromarray(
|
119 |
+
np.bitwise_and(np.array(mask), np.array(image))
|
120 |
+
)
|
121 |
+
arr_orig = np.array(grayscale(mask))
|
122 |
+
|
123 |
+
image = cv2.inpaint(np.array(image), arr_orig, 15, cv2.INPAINT_TELEA)
|
124 |
+
image = Image.fromarray(image)
|
125 |
+
|
126 |
+
arr = arr_orig.copy()
|
127 |
+
mask_y = np.where(arr == arr[arr != 0][0])[0][0]
|
128 |
+
arr[mask_y:, :] = 255
|
129 |
+
|
130 |
+
new = Image.fromarray(arr)
|
131 |
+
|
132 |
+
mask = new.copy()
|
133 |
+
|
134 |
+
orig_size = image.size
|
135 |
+
|
136 |
+
image = image.resize((512, 512))
|
137 |
+
mask = mask.resize((512, 512))
|
138 |
+
|
139 |
+
results = []
|
140 |
+
for colour in ["Red", "Blue", "Green"]:
|
141 |
+
prompt = f"{colour}, South Indian Saree, properly worn, natural setting, elegant, natural look, neckline without jewellery, simple"
|
142 |
+
negative_prompt = "necklaces, jewellery, jewelry, necklace, neckpiece, garland, chain, neck wear, jewelled neck, jeweled neck, necklace on neck, jewellery on neck, accessories, watermark, text, changed background, wider body, narrower body, bad proportions, extra limbs, mutated hands, changed sizes, altered proportions, unnatural body proportions, blury, ugly"
|
143 |
+
|
144 |
+
output = pipeline(
|
145 |
+
prompt=prompt,
|
146 |
+
negative_prompt=negative_prompt,
|
147 |
+
image=image,
|
148 |
+
mask_image=mask,
|
149 |
+
strength=0.95,
|
150 |
+
guidance_score=9,
|
151 |
+
# generator = torch.Generator("cuda").manual_seed(42)
|
152 |
+
).images[0]
|
153 |
+
|
154 |
+
output = output.resize(orig_size)
|
155 |
+
temp_generated = np.bitwise_and(
|
156 |
+
np.array(output),
|
157 |
+
np.bitwise_not(np.array(Image.fromarray(arr_orig).convert("RGB"))),
|
158 |
+
)
|
159 |
+
results.append(temp_generated)
|
160 |
+
|
161 |
+
results = [
|
162 |
+
Image.fromarray(np.bitwise_or(x, np.array(jewellery_mask))) for x in results
|
163 |
+
]
|
164 |
+
clearFunc()
|
165 |
+
return results[0], results[1], results[2]
|
166 |
+
|
167 |
+
# interface
|
168 |
+
|
169 |
+
with gr.Blocks() as interface:
|
170 |
+
with gr.Row():
|
171 |
+
inputImage = gr.Image(label = "Input Image", type = "pil", image_mode = "RGB", interactive = True)
|
172 |
+
selectedNecklace = gr.Image(label = "Selected Necklace", type = "pil", image_mode = "RGBA", visible = False)
|
173 |
+
hiddenMask = gr.Image(visible = False, type = "pil")
|
174 |
+
with gr.Row():
|
175 |
+
gr.Examples(examples = person_images, inputs=[inputImage], label="Models")
|
176 |
+
with gr.Row():
|
177 |
+
gr.Examples(examples = choker_images, inputs = [selectedNecklace], label = "Chokers")
|
178 |
+
with gr.Row():
|
179 |
+
gr.Examples(examples = short_necklaces, inputs = [selectedNecklace], label = "Short Necklaces")
|
180 |
+
with gr.Row():
|
181 |
+
gr.Examples(examples = long_necklaces, inputs = [selectedNecklace], label = "Long Necklaces")
|
182 |
+
with gr.Row():
|
183 |
+
outputOne = gr.Image(label = "Output 1", interactive = False)
|
184 |
+
outputTwo = gr.Image(label = "Output 2", interactive = False)
|
185 |
+
outputThree = gr.Image(label = "Output 3", interactive = False)
|
186 |
+
with gr.Row():
|
187 |
+
submit = gr.Button("Enter")
|
188 |
+
|
189 |
+
selectedNecklace.change(fn = necklaceTryOnPipeline, inputs = [inputImage, selectedNecklace], outputs = [inputImage, hiddenMask])
|
190 |
+
submit.click(fn = sd_inpaint, inputs = [inputImage, hiddenMask], outputs = [outputOne, outputTwo, outputThree])
|
191 |
+
|
192 |
+
|
193 |
+
interface.launch(debug = True)
|
choker/necklace_1.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_11.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_12.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_20.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_27.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_29.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_48.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_51.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_56.png
ADDED
![]() |
Git LFS Details
|
choker/necklace_8.png
ADDED
![]() |
Git LFS Details
|
long_haram/necklace_1.png
ADDED
![]() |
long_haram/necklace_10.png
ADDED
![]() |
long_haram/necklace_2.png
ADDED
![]() |
long_haram/necklace_3.png
ADDED
![]() |
long_haram/necklace_4.png
ADDED
![]() |
long_haram/necklace_5.png
ADDED
![]() |
long_haram/necklace_6.png
ADDED
![]() |
long_haram/necklace_7.png
ADDED
![]() |
long_haram/necklace_8.png
ADDED
![]() |
long_haram/necklace_9.png
ADDED
![]() |
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
torchvision
|
5 |
+
pillow
|
6 |
+
ftfy
|
7 |
+
numpy
|
8 |
+
gradio
|
9 |
+
accelerate
|
10 |
+
segment_anything
|
11 |
+
opencv-python
|
12 |
+
numpy
|
13 |
+
mediapipe
|
14 |
+
cvzone
|
short_necklace/necklace_10.png
ADDED
![]() |
short_necklace/necklace_13.png
ADDED
![]() |
short_necklace/necklace_14.png
ADDED
![]() |
Git LFS Details
|
short_necklace/necklace_2.png
ADDED
![]() |
short_necklace/necklace_3.png
ADDED
![]() |
short_necklace/necklace_4.png
ADDED
![]() |
Git LFS Details
|
short_necklace/necklace_5.png
ADDED
![]() |
Git LFS Details
|
short_necklace/necklace_6.png
ADDED
![]() |
Git LFS Details
|
short_necklace/necklace_7.png
ADDED
![]() |
short_necklace/necklace_9.png
ADDED
![]() |
without_necklace/person_1.png
ADDED
![]() |
without_necklace/person_2.png
ADDED
![]() |
without_necklace/person_3.png
ADDED
![]() |