Spaces:
Runtime error
Runtime error
Add virtual try-on code
Browse files- app.py +56 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
import torch
|
| 5 |
+
import mediapipe as mp
|
| 6 |
+
from transformers import SamModel, SamProcessor
|
| 7 |
+
from diffusers.utils import load_image
|
| 8 |
+
from torchvision import transforms
|
| 9 |
+
|
| 10 |
+
# Load model once
|
| 11 |
+
model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50")
|
| 12 |
+
processor = SamProcessor.from_pretrained("Zigeng/SlimSAM-uniform-50")
|
| 13 |
+
|
| 14 |
+
def get_shoulder_coordinates(image: Image.Image):
|
| 15 |
+
mp_pose = mp.solutions.pose
|
| 16 |
+
pose = mp_pose.Pose()
|
| 17 |
+
|
| 18 |
+
image_rgb = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 19 |
+
results = pose.process(image_rgb)
|
| 20 |
+
if results.pose_landmarks:
|
| 21 |
+
height, width, _ = image_rgb.shape
|
| 22 |
+
landmarks = results.pose_landmarks.landmark
|
| 23 |
+
left = (int(landmarks[11].x * width), int(landmarks[11].y * height))
|
| 24 |
+
right = (int(landmarks[12].x * width), int(landmarks[12].y * height))
|
| 25 |
+
return left, right
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
def try_on(person_img, tshirt_img):
|
| 29 |
+
coordinates = get_shoulder_coordinates(person_img)
|
| 30 |
+
if coordinates is None:
|
| 31 |
+
return "No pose detected", None
|
| 32 |
+
|
| 33 |
+
left_shoulder, right_shoulder = coordinates
|
| 34 |
+
input_points = [[[left_shoulder[0], left_shoulder[1]], [right_shoulder[0], right_shoulder[1]]]]
|
| 35 |
+
|
| 36 |
+
inputs = processor(person_img, input_points=input_points, return_tensors="pt")
|
| 37 |
+
outputs = model(**inputs)
|
| 38 |
+
|
| 39 |
+
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(),
|
| 40 |
+
inputs["original_sizes"].cpu(),
|
| 41 |
+
inputs["reshaped_input_sizes"].cpu())
|
| 42 |
+
|
| 43 |
+
mask_tensor = masks[0][0][2].to(dtype=torch.uint8)
|
| 44 |
+
mask = transforms.ToPILImage()(mask_tensor * 255)
|
| 45 |
+
|
| 46 |
+
tshirt_img = tshirt_img.resize(person_img.size, Image.LANCZOS)
|
| 47 |
+
result = Image.composite(tshirt_img, person_img, mask)
|
| 48 |
+
return result
|
| 49 |
+
|
| 50 |
+
demo = gr.Interface(fn=try_on,
|
| 51 |
+
inputs=["image", "image"],
|
| 52 |
+
outputs="image",
|
| 53 |
+
title="Virtual Try-On using SlimSAM",
|
| 54 |
+
description="Upload a person image and a t-shirt image.")
|
| 55 |
+
|
| 56 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==2.3.3
|
| 2 |
+
gunicorn==21.2.0
|
| 3 |
+
Pillow==10.1.0
|
| 4 |
+
opencv-python==4.9.0.80
|
| 5 |
+
torch==2.2.0
|
| 6 |
+
torchvision==0.17.0
|
| 7 |
+
mediapipe==0.10.21
|
| 8 |
+
transformers==4.40.1
|
| 9 |
+
diffusers==0.27.2
|
| 10 |
+
safetensors==0.4.2
|