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