Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,121 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import random
|
| 6 |
+
import base64
|
| 7 |
+
import time
|
| 8 |
from gradio_client import Client
|
| 9 |
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# Connect to Hunyuan3D Space
|
| 12 |
+
# -----------------------------
|
| 13 |
+
mesh_client = Client("tencent/Hunyuan3D-2.1") # has real API
|
| 14 |
+
MESH_API_NAME = "/process"
|
| 15 |
+
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# Kolors VTON code (internal)
|
| 18 |
+
# -----------------------------
|
| 19 |
+
MAX_SEED = 999999
|
| 20 |
+
|
| 21 |
+
def tryon(person_img, garment_img, seed, randomize_seed):
|
| 22 |
+
if person_img is None or garment_img is None:
|
| 23 |
+
return None, seed, "Empty image"
|
| 24 |
+
if randomize_seed:
|
| 25 |
+
seed = random.randint(0, MAX_SEED)
|
| 26 |
+
|
| 27 |
+
# Encode images
|
| 28 |
+
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 29 |
+
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 30 |
+
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 31 |
+
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 32 |
+
|
| 33 |
+
# Send request to internal tryon server (Kolors VTON)
|
| 34 |
+
tryon_url = "http://" + os.environ['tryon_url'] + "Submit"
|
| 35 |
+
token = os.environ['token']
|
| 36 |
+
cookie = os.environ['Cookie']
|
| 37 |
+
referer = os.environ['referer']
|
| 38 |
+
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 39 |
+
data = {"clothImage": encoded_garment_img, "humanImage": encoded_person_img, "seed": seed}
|
| 40 |
+
|
| 41 |
+
# Post request
|
| 42 |
+
import requests
|
| 43 |
+
try:
|
| 44 |
+
response = requests.post(tryon_url, headers=headers, data=json.dumps(data), timeout=50)
|
| 45 |
+
if response.status_code != 200:
|
| 46 |
+
return None, seed, f"Tryon POST failed: {response.status_code}"
|
| 47 |
+
result_json = response.json()['result']
|
| 48 |
+
if result_json['status'] != "success":
|
| 49 |
+
return None, seed, f"Tryon error: {result_json['status']}"
|
| 50 |
+
task_uuid = result_json['result']
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return None, seed, f"Tryon exception: {str(e)}"
|
| 53 |
+
|
| 54 |
+
# Poll for result
|
| 55 |
+
result_img = None
|
| 56 |
+
for _ in range(12): # retry 12 times
|
| 57 |
+
try:
|
| 58 |
+
query_url = "http://" + os.environ['tryon_url'] + f"Query?taskId={task_uuid}"
|
| 59 |
+
response = requests.get(query_url, headers=headers, timeout=20)
|
| 60 |
+
if response.status_code == 200:
|
| 61 |
+
r = response.json()['result']
|
| 62 |
+
if r['status'] == "success":
|
| 63 |
+
img_bytes = base64.b64decode(r['result'])
|
| 64 |
+
result_np = np.frombuffer(img_bytes, np.uint8)
|
| 65 |
+
result_img = cv2.imdecode(result_np, cv2.IMREAD_COLOR)
|
| 66 |
+
result_img = cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB)
|
| 67 |
+
break
|
| 68 |
+
time.sleep(1)
|
| 69 |
+
except:
|
| 70 |
+
time.sleep(1)
|
| 71 |
+
|
| 72 |
+
if result_img is None:
|
| 73 |
+
return None, seed, "Tryon failed or timed out"
|
| 74 |
+
|
| 75 |
+
return result_img, seed, "Success"
|
| 76 |
+
|
| 77 |
+
# -----------------------------
|
| 78 |
+
# Merge: Tryon + Hunyuan3D
|
| 79 |
+
# -----------------------------
|
| 80 |
+
def tryon_to_3d(person_img, garment_img, seed, randomize_seed):
|
| 81 |
+
# Run 2D try-on
|
| 82 |
+
tryon_img, seed_used, tryon_info = tryon(person_img, garment_img, seed, randomize_seed)
|
| 83 |
+
if tryon_img is None:
|
| 84 |
+
return None, None, seed_used, tryon_info
|
| 85 |
+
|
| 86 |
+
# Run 3D mesh generation (Hunyuan3D)
|
| 87 |
+
try:
|
| 88 |
+
mesh_file = mesh_client.predict(tryon_img, api_name=MESH_API_NAME)
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return tryon_img, None, seed_used, f"Hunyuan3D error: {str(e)}"
|
| 91 |
+
|
| 92 |
+
return tryon_img, mesh_file, seed_used, "Success"
|
| 93 |
+
|
| 94 |
+
# -----------------------------
|
| 95 |
+
# Gradio Interface
|
| 96 |
+
# -----------------------------
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
gr.Markdown("# Virtual Try-On → 3D Mesh Generator")
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
person_img = gr.Image(label="Person Image", type="numpy")
|
| 102 |
+
garment_img = gr.Image(label="Garment Image", type="numpy")
|
| 103 |
+
|
| 104 |
+
with gr.Row():
|
| 105 |
+
seed = gr.Slider(0, MAX_SEED, step=1, value=0, label="Seed")
|
| 106 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
tryon_out = gr.Image(label="2D Try-On Result")
|
| 110 |
+
mesh_out = gr.Model3D(label="3D Mesh Output")
|
| 111 |
+
|
| 112 |
+
seed_used_out = gr.Number(label="Seed Used")
|
| 113 |
+
status_out = gr.Textbox(label="Status")
|
| 114 |
+
|
| 115 |
+
run_btn = gr.Button("Run")
|
| 116 |
+
run_btn.click(fn=tryon_to_3d,
|
| 117 |
+
inputs=[person_img, garment_img, seed, randomize_seed],
|
| 118 |
+
outputs=[tryon_out, mesh_out, seed_used_out, status_out])
|
| 119 |
|
| 120 |
if __name__ == "__main__":
|
| 121 |
demo.launch()
|