Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,13 @@ garm_list_path = [os.path.join(example_path, "cloth", garm) for garm in garm_lis
|
|
15 |
human_list = os.listdir(os.path.join(example_path, "human"))
|
16 |
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
19 |
post_start_time = time.time()
|
20 |
if person_img is None or garment_img is None:
|
@@ -23,12 +30,13 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
23 |
if randomize_seed:
|
24 |
seed = random.randint(0, MAX_SEED)
|
25 |
|
|
|
26 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
27 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
28 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
29 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
30 |
|
31 |
-
|
32 |
data = {
|
33 |
"clothImage": encoded_garment_img,
|
34 |
"humanImage": encoded_person_img,
|
@@ -37,11 +45,16 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
37 |
|
38 |
uuid = None
|
39 |
try:
|
40 |
-
|
|
|
41 |
if response.status_code == 200:
|
42 |
result = response.json().get('result', {})
|
43 |
if result.get('status') == "success":
|
44 |
-
uuid = result.get('
|
|
|
|
|
|
|
|
|
45 |
except Exception as err:
|
46 |
print(f"Post Exception Error: {err}")
|
47 |
raise gr.Error("Too many users, please try again later")
|
@@ -49,7 +62,7 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
49 |
post_end_time = time.time()
|
50 |
print(f"post time used: {post_end_time - post_start_time}")
|
51 |
|
52 |
-
# Retry loop
|
53 |
get_start_time = time.time()
|
54 |
time.sleep(5)
|
55 |
Max_Retry = 20
|
@@ -63,7 +76,7 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
63 |
else:
|
64 |
for i in range(Max_Retry):
|
65 |
try:
|
66 |
-
url = f"
|
67 |
response = requests.get(url, timeout=20)
|
68 |
if response.status_code == 200:
|
69 |
result = response.json()['result']
|
@@ -148,11 +161,9 @@ with gr.Blocks(css=css) as Tryon:
|
|
148 |
with gr.Column(elem_id="col-showcase"):
|
149 |
gr.HTML("<div style='text-align: center; font-size: 20px;'>Virtual try-on examples in pairs of person and garment images</div>")
|
150 |
gr.Examples(
|
151 |
-
examples=[
|
152 |
-
|
153 |
-
|
154 |
-
["assets/examples/model1.png", "assets/examples/garment1.png", "assets/examples/result1.png"]
|
155 |
-
],
|
156 |
inputs=[imgs, garm_img, image_out]
|
157 |
)
|
158 |
|
|
|
15 |
human_list = os.listdir(os.path.join(example_path, "human"))
|
16 |
human_list_path = [os.path.join(example_path, "human", human) for human in human_list]
|
17 |
|
18 |
+
# API details
|
19 |
+
base_url = "https://huggingface.co/spaces/zaghamrasool/Z-Virtual-Try-On"
|
20 |
+
upload_image_url = f"{base_url}/upload_image"
|
21 |
+
create_save_task_url = f"{base_url}/create_save_task"
|
22 |
+
execute_task_url = f"{base_url}/execute_task"
|
23 |
+
query_task_url = f"{base_url}/query_task"
|
24 |
+
|
25 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
26 |
post_start_time = time.time()
|
27 |
if person_img is None or garment_img is None:
|
|
|
30 |
if randomize_seed:
|
31 |
seed = random.randint(0, MAX_SEED)
|
32 |
|
33 |
+
# Encode images
|
34 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
35 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
36 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
37 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
38 |
|
39 |
+
# Prepare data
|
40 |
data = {
|
41 |
"clothImage": encoded_garment_img,
|
42 |
"humanImage": encoded_person_img,
|
|
|
45 |
|
46 |
uuid = None
|
47 |
try:
|
48 |
+
# First API call to create task
|
49 |
+
response = requests.post(create_save_task_url, data=json.dumps(data), timeout=50)
|
50 |
if response.status_code == 200:
|
51 |
result = response.json().get('result', {})
|
52 |
if result.get('status') == "success":
|
53 |
+
uuid = result.get('taskId') # Use taskId for querying
|
54 |
+
else:
|
55 |
+
raise Exception("Failed to create task, no task ID received.")
|
56 |
+
else:
|
57 |
+
raise Exception(f"Failed to create task. Status Code: {response.status_code}")
|
58 |
except Exception as err:
|
59 |
print(f"Post Exception Error: {err}")
|
60 |
raise gr.Error("Too many users, please try again later")
|
|
|
62 |
post_end_time = time.time()
|
63 |
print(f"post time used: {post_end_time - post_start_time}")
|
64 |
|
65 |
+
# Retry loop to query task status
|
66 |
get_start_time = time.time()
|
67 |
time.sleep(5)
|
68 |
Max_Retry = 20
|
|
|
76 |
else:
|
77 |
for i in range(Max_Retry):
|
78 |
try:
|
79 |
+
url = f"{query_task_url}?taskId={uuid}"
|
80 |
response = requests.get(url, timeout=20)
|
81 |
if response.status_code == 200:
|
82 |
result = response.json()['result']
|
|
|
161 |
with gr.Column(elem_id="col-showcase"):
|
162 |
gr.HTML("<div style='text-align: center; font-size: 20px;'>Virtual try-on examples in pairs of person and garment images</div>")
|
163 |
gr.Examples(
|
164 |
+
examples=[["assets/examples/model2.png", "assets/examples/garment2.png", "assets/examples/result2.png"],
|
165 |
+
["assets/examples/model3.png", "assets/examples/garment3.png", "assets/examples/result3.png"],
|
166 |
+
["assets/examples/model1.png", "assets/examples/garment1.png", "assets/examples/result1.png"]],
|
|
|
|
|
167 |
inputs=[imgs, garm_img, image_out]
|
168 |
)
|
169 |
|