Spaces:
Runtime error
Runtime error
Commit Β·
b29f425
1
Parent(s): 01868e3
update: remove necklacetryon
Browse files
app.py
CHANGED
|
@@ -1,17 +1,12 @@
|
|
| 1 |
-
import torch
|
| 2 |
import cv2
|
| 3 |
import torch
|
| 4 |
import gc
|
| 5 |
-
import math
|
| 6 |
-
import cvzone
|
| 7 |
import os
|
| 8 |
import spaces
|
| 9 |
import gradio as gr
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
from PIL.ImageOps import grayscale
|
| 13 |
-
from cvzone.PoseModule import PoseDetector
|
| 14 |
-
from cvzone.FaceMeshModule import FaceMeshDetector
|
| 15 |
from diffusers import StableDiffusionInpaintPipeline
|
| 16 |
|
| 17 |
|
|
@@ -26,86 +21,13 @@ class NecklaceTryOn:
|
|
| 26 |
)
|
| 27 |
self.pipeline = self.pipeline.to("cuda")
|
| 28 |
|
| 29 |
-
self.detector = PoseDetector()
|
| 30 |
-
self.meshDetector = FaceMeshDetector(staticMode=True, maxFaces=1)
|
| 31 |
-
|
| 32 |
def clear_func(self):
|
| 33 |
torch.cuda.empty_cache()
|
| 34 |
gc.collect()
|
| 35 |
|
| 36 |
@spaces.GPU
|
| 37 |
-
def
|
| 38 |
-
"""Apply necklace on the image and return modified image and mask."""
|
| 39 |
-
image = np.array(image)
|
| 40 |
-
copy_image = image.copy()
|
| 41 |
-
jewellery = np.array(jewellery)
|
| 42 |
-
|
| 43 |
-
image = self.detector.findPose(image)
|
| 44 |
-
lmList, _ = self.detector.findPosition(image, bboxWithHands=False, draw=False)
|
| 45 |
-
|
| 46 |
-
img, faces = self.meshDetector.findFaceMesh(image, draw=False)
|
| 47 |
-
leftLandmarkIndex = 172
|
| 48 |
-
rightLandmarkIndex = 397
|
| 49 |
-
|
| 50 |
-
leftLandmark, rightLandmark = faces[0][leftLandmarkIndex], faces[0][rightLandmarkIndex]
|
| 51 |
-
landmarksDistance = int(
|
| 52 |
-
((leftLandmark[0] - rightLandmark[0]) ** 2 + (leftLandmark[1] - rightLandmark[1]) ** 2) ** 0.5)
|
| 53 |
-
|
| 54 |
-
avg_x1 = int(leftLandmark[0] - landmarksDistance * 0.12)
|
| 55 |
-
avg_x2 = int(rightLandmark[0] + landmarksDistance * 0.12)
|
| 56 |
-
|
| 57 |
-
avg_y1 = int(leftLandmark[1] + landmarksDistance * 0.5)
|
| 58 |
-
avg_y2 = int(rightLandmark[1] + landmarksDistance * 0.5)
|
| 59 |
-
|
| 60 |
-
if avg_y2 < avg_y1:
|
| 61 |
-
angle = math.ceil(
|
| 62 |
-
self.detector.findAngle(
|
| 63 |
-
p1=(avg_x2, avg_y2), p2=(avg_x1, avg_y1), p3=(avg_x2, avg_y1)
|
| 64 |
-
)[0]
|
| 65 |
-
)
|
| 66 |
-
else:
|
| 67 |
-
angle = math.ceil(
|
| 68 |
-
self.detector.findAngle(
|
| 69 |
-
p1=(avg_x2, avg_y2), p2=(avg_x1, avg_y1), p3=(avg_x2, avg_y1)
|
| 70 |
-
)[0]
|
| 71 |
-
)
|
| 72 |
-
angle = angle * -1
|
| 73 |
-
|
| 74 |
-
xdist = avg_x2 - avg_x1
|
| 75 |
-
origImgRatio = xdist / jewellery.shape[1]
|
| 76 |
-
ydist = jewellery.shape[0] * origImgRatio
|
| 77 |
-
|
| 78 |
-
image_gray = cv2.cvtColor(jewellery, cv2.COLOR_BGRA2GRAY)
|
| 79 |
-
for offset_orig in range(image_gray.shape[1]):
|
| 80 |
-
pixel_value = image_gray[0, :][offset_orig]
|
| 81 |
-
if (pixel_value != 255) & (pixel_value != 0):
|
| 82 |
-
break
|
| 83 |
-
else:
|
| 84 |
-
continue
|
| 85 |
-
offset = int(0.8 * xdist * (offset_orig / jewellery.shape[1]))
|
| 86 |
-
jewellery = cv2.resize(
|
| 87 |
-
jewellery, (int(xdist), int(ydist)), interpolation=cv2.INTER_AREA
|
| 88 |
-
)
|
| 89 |
-
jewellery = cvzone.rotateImage(jewellery, angle)
|
| 90 |
-
y_coordinate = avg_y1 - offset
|
| 91 |
-
result = cvzone.overlayPNG(copy_image, jewellery, (avg_x1, y_coordinate))
|
| 92 |
-
|
| 93 |
-
blackedNecklace = np.zeros(shape=copy_image.shape)
|
| 94 |
-
cvzone.overlayPNG(blackedNecklace, jewellery, (avg_x1, y_coordinate))
|
| 95 |
-
blackedNecklace = cv2.cvtColor(blackedNecklace.astype(np.uint8), cv2.COLOR_BGR2GRAY)
|
| 96 |
-
binaryMask = blackedNecklace * ((blackedNecklace > 5) * 255)
|
| 97 |
-
binaryMask[binaryMask >= 255] = 255
|
| 98 |
-
binaryMask[binaryMask < 255] = 0
|
| 99 |
-
|
| 100 |
-
gc.collect()
|
| 101 |
-
|
| 102 |
-
return Image.fromarray(result.astype(np.uint8)), Image.fromarray(binaryMask.astype(np.uint8)).convert("RGB")
|
| 103 |
-
|
| 104 |
-
@spaces.GPU
|
| 105 |
-
def clothing_try_on_n_necklace_try_on(self, image, jewellery):
|
| 106 |
"""Main method for clothing and necklace try-on."""
|
| 107 |
-
result_image, mask = self.apply_necklace(image, jewellery)
|
| 108 |
-
|
| 109 |
jewellery_mask = Image.fromarray(
|
| 110 |
np.bitwise_and(np.array(mask), np.array(result_image))
|
| 111 |
)
|
|
@@ -155,16 +77,12 @@ class NecklaceTryOn:
|
|
| 155 |
with gr.Blocks() as interface:
|
| 156 |
with gr.Row():
|
| 157 |
inputImage = gr.Image(label="Input Image", type="pil", image_mode="RGB", interactive=True)
|
| 158 |
-
|
| 159 |
outputOne = gr.Image(label="Output", interactive=False)
|
| 160 |
|
| 161 |
-
with gr.Row():
|
| 162 |
-
gr.Examples(examples=self.choker_images, inputs=[selectedNecklace], label="Select Necklace")
|
| 163 |
-
gr.Examples(examples=self.person_images, inputs=[inputImage], label="Select Model")
|
| 164 |
-
|
| 165 |
submit = gr.Button("Apply")
|
| 166 |
|
| 167 |
-
submit.click(fn=self.clothing_try_on_n_necklace_try_on, inputs=[inputImage,
|
| 168 |
outputs=[outputOne])
|
| 169 |
|
| 170 |
interface.launch(debug=True)
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import torch
|
| 3 |
import gc
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import spaces
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
from PIL.ImageOps import grayscale
|
|
|
|
|
|
|
| 10 |
from diffusers import StableDiffusionInpaintPipeline
|
| 11 |
|
| 12 |
|
|
|
|
| 21 |
)
|
| 22 |
self.pipeline = self.pipeline.to("cuda")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
def clear_func(self):
|
| 25 |
torch.cuda.empty_cache()
|
| 26 |
gc.collect()
|
| 27 |
|
| 28 |
@spaces.GPU
|
| 29 |
+
def clothing_try_on_n_necklace_try_on(self, result_image, mask):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""Main method for clothing and necklace try-on."""
|
|
|
|
|
|
|
| 31 |
jewellery_mask = Image.fromarray(
|
| 32 |
np.bitwise_and(np.array(mask), np.array(result_image))
|
| 33 |
)
|
|
|
|
| 77 |
with gr.Blocks() as interface:
|
| 78 |
with gr.Row():
|
| 79 |
inputImage = gr.Image(label="Input Image", type="pil", image_mode="RGB", interactive=True)
|
| 80 |
+
mask_image = gr.Image(label="Mask Image", type="pil", image_mode="RGB", interactive=True)
|
| 81 |
outputOne = gr.Image(label="Output", interactive=False)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
submit = gr.Button("Apply")
|
| 84 |
|
| 85 |
+
submit.click(fn=self.clothing_try_on_n_necklace_try_on, inputs=[inputImage, mask_image],
|
| 86 |
outputs=[outputOne])
|
| 87 |
|
| 88 |
interface.launch(debug=True)
|