Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
# -----------------------------
|
| 6 |
# Local "try-on" function
|
| 7 |
# -----------------------------
|
| 8 |
def tryon_local(person_img, garment_img, seed, randomize_seed):
|
| 9 |
-
"""
|
| 10 |
-
Simulates try-on by resizing the garment to fit the person and overlaying.
|
| 11 |
-
"""
|
| 12 |
if person_img is None or garment_img is None:
|
| 13 |
return None, None, "Empty image"
|
| 14 |
|
| 15 |
-
# Resize garment to width of person image
|
| 16 |
h_person, w_person = person_img.shape[:2]
|
| 17 |
h_garment, w_garment = garment_img.shape[:2]
|
| 18 |
|
|
@@ -21,12 +18,10 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
|
|
| 21 |
new_h = int(h_garment * scale)
|
| 22 |
garment_resized = cv2.resize(garment_img, (new_w, new_h))
|
| 23 |
|
| 24 |
-
# Simple overlay: top of garment onto top of person
|
| 25 |
overlay = person_img.copy()
|
| 26 |
y_offset = 0
|
| 27 |
x_offset = max(0, (w_person - new_w) // 2)
|
| 28 |
|
| 29 |
-
# Handle transparency if 4-channel
|
| 30 |
if garment_resized.shape[2] == 4:
|
| 31 |
alpha = garment_resized[:, :, 3] / 255.0
|
| 32 |
for c in range(3):
|
|
@@ -39,24 +34,27 @@ def tryon_local(person_img, garment_img, seed, randomize_seed):
|
|
| 39 |
return overlay, seed, info
|
| 40 |
|
| 41 |
# -----------------------------
|
| 42 |
-
# Hunyuan3D API
|
| 43 |
# -----------------------------
|
| 44 |
-
from gradio_client import Client
|
| 45 |
hunyuan_client = Client("tencent/Hunyuan3D-2.1")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
| 48 |
-
|
| 49 |
-
tryon_img, seed_used, tryon_info = tryon_local(person_img, garment_img, seed, randomize_seed)
|
| 50 |
if tryon_img is None:
|
| 51 |
return None, "Try-on failed"
|
| 52 |
|
| 53 |
-
# Step 2: Send 2D try-on result to Hunyuan3D
|
| 54 |
try:
|
| 55 |
result_3d = hunyuan_client.predict(
|
| 56 |
-
tryon_img,
|
| 57 |
-
api_name=
|
| 58 |
)
|
| 59 |
-
return result_3d,
|
| 60 |
except Exception as e:
|
| 61 |
return None, f"Hunyuan3D API error: {e}"
|
| 62 |
|
|
@@ -64,14 +62,8 @@ def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
|
| 64 |
# Gradio UI
|
| 65 |
# -----------------------------
|
| 66 |
css = """
|
| 67 |
-
#col-left, #col-mid, #col-right {
|
| 68 |
-
|
| 69 |
-
max-width: 430px;
|
| 70 |
-
}
|
| 71 |
-
#col-showcase {
|
| 72 |
-
margin: 0 auto;
|
| 73 |
-
max-width: 1100px;
|
| 74 |
-
}
|
| 75 |
#button { color: blue; }
|
| 76 |
"""
|
| 77 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from gradio_client import Client
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
# Local "try-on" function
|
| 8 |
# -----------------------------
|
| 9 |
def tryon_local(person_img, garment_img, seed, randomize_seed):
|
|
|
|
|
|
|
|
|
|
| 10 |
if person_img is None or garment_img is None:
|
| 11 |
return None, None, "Empty image"
|
| 12 |
|
|
|
|
| 13 |
h_person, w_person = person_img.shape[:2]
|
| 14 |
h_garment, w_garment = garment_img.shape[:2]
|
| 15 |
|
|
|
|
| 18 |
new_h = int(h_garment * scale)
|
| 19 |
garment_resized = cv2.resize(garment_img, (new_w, new_h))
|
| 20 |
|
|
|
|
| 21 |
overlay = person_img.copy()
|
| 22 |
y_offset = 0
|
| 23 |
x_offset = max(0, (w_person - new_w) // 2)
|
| 24 |
|
|
|
|
| 25 |
if garment_resized.shape[2] == 4:
|
| 26 |
alpha = garment_resized[:, :, 3] / 255.0
|
| 27 |
for c in range(3):
|
|
|
|
| 34 |
return overlay, seed, info
|
| 35 |
|
| 36 |
# -----------------------------
|
| 37 |
+
# Hunyuan3D API client
|
| 38 |
# -----------------------------
|
|
|
|
| 39 |
hunyuan_client = Client("tencent/Hunyuan3D-2.1")
|
| 40 |
|
| 41 |
+
# Auto-detect available API function
|
| 42 |
+
available_functions = hunyuan_client.get_functions()
|
| 43 |
+
if len(available_functions) == 0:
|
| 44 |
+
raise RuntimeError("No functions available in Hunyuan3D Space")
|
| 45 |
+
api_name = available_functions[0]["api_name"] # use first available function
|
| 46 |
+
|
| 47 |
def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
| 48 |
+
tryon_img, seed_used, info = tryon_local(person_img, garment_img, seed, randomize_seed)
|
|
|
|
| 49 |
if tryon_img is None:
|
| 50 |
return None, "Try-on failed"
|
| 51 |
|
|
|
|
| 52 |
try:
|
| 53 |
result_3d = hunyuan_client.predict(
|
| 54 |
+
tryon_img,
|
| 55 |
+
api_name=api_name
|
| 56 |
)
|
| 57 |
+
return result_3d, info
|
| 58 |
except Exception as e:
|
| 59 |
return None, f"Hunyuan3D API error: {e}"
|
| 60 |
|
|
|
|
| 62 |
# Gradio UI
|
| 63 |
# -----------------------------
|
| 64 |
css = """
|
| 65 |
+
#col-left, #col-mid, #col-right {margin: 0 auto; max-width: 430px;}
|
| 66 |
+
#col-showcase {margin: 0 auto; max-width: 1100px;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
#button { color: blue; }
|
| 68 |
"""
|
| 69 |
|