Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,37 @@
|
|
1 |
import os
|
2 |
-
import cv2
|
3 |
-
import gradio as gr
|
4 |
-
import numpy as np
|
5 |
-
import random
|
6 |
-
import base64
|
7 |
import requests
|
8 |
import json
|
9 |
-
import time
|
10 |
-
|
11 |
-
|
12 |
-
import os
|
13 |
-
import cv2
|
14 |
import base64
|
15 |
-
import
|
16 |
-
import requests
|
17 |
-
import json
|
18 |
-
import time
|
19 |
import numpy as np
|
20 |
-
import
|
|
|
21 |
|
22 |
-
MAX_SEED =
|
23 |
|
24 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
25 |
start_time = time.time()
|
26 |
-
|
27 |
-
# Check if images are provided
|
28 |
if person_img is None or garment_img is None:
|
29 |
return None, None, "Empty image"
|
30 |
|
31 |
-
# Handle seed randomization
|
32 |
if randomize_seed:
|
33 |
seed = random.randint(0, MAX_SEED)
|
34 |
-
|
35 |
-
# Encode images
|
36 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
37 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
38 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
39 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
40 |
|
41 |
-
#
|
42 |
-
url = "
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
-
# Define headers and data payload
|
48 |
-
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
49 |
data = {
|
50 |
"clothImage": encoded_garment_img,
|
51 |
"humanImage": encoded_person_img,
|
@@ -53,66 +39,58 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
53 |
}
|
54 |
|
55 |
result_img = None
|
56 |
-
max_retries = 5
|
57 |
-
retry_delay = 5
|
58 |
|
59 |
try:
|
60 |
session = requests.Session()
|
61 |
-
|
62 |
# Retry loop for "Too many users" error
|
63 |
for attempt in range(max_retries):
|
64 |
response = session.post(url, headers=headers, data=json.dumps(data), timeout=60)
|
65 |
-
print("
|
66 |
|
67 |
if response.status_code == 200:
|
68 |
-
result = response.json()
|
69 |
-
status = result
|
70 |
|
71 |
if status == "success":
|
72 |
-
|
73 |
-
|
74 |
-
result_np = np.frombuffer(result, np.uint8)
|
75 |
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
76 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
77 |
info = "Success"
|
78 |
-
break
|
79 |
-
|
80 |
else:
|
81 |
info = "Try again later"
|
82 |
-
print("Status returned:", status)
|
83 |
-
|
84 |
else:
|
85 |
-
print(response.text)
|
86 |
if "Too many users" in response.text:
|
87 |
print(f"Attempt {attempt + 1}/{max_retries}: API busy. Retrying in {retry_delay} seconds...")
|
88 |
time.sleep(retry_delay)
|
89 |
else:
|
90 |
-
info = "
|
91 |
-
break
|
92 |
|
93 |
else:
|
94 |
-
# If all retries fail, return this message
|
95 |
info = "API still overloaded β please try again later."
|
96 |
|
97 |
-
# Handle timeouts specifically
|
98 |
except requests.exceptions.ReadTimeout:
|
99 |
-
print("
|
100 |
info = "Too many users, please try again later"
|
101 |
raise gr.Error("Too many users, please try again later")
|
102 |
|
103 |
-
# Handle any other unexpected errors
|
104 |
except Exception as err:
|
105 |
print(f"Other error: {err}")
|
106 |
info = "Error, please contact the admin"
|
107 |
|
108 |
end_time = time.time()
|
109 |
-
print(f"
|
110 |
|
111 |
return result_img, seed, info
|
112 |
|
113 |
|
114 |
|
115 |
-
|
116 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
117 |
|
118 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
2 |
import requests
|
3 |
import json
|
|
|
|
|
|
|
|
|
|
|
4 |
import base64
|
5 |
+
import cv2
|
|
|
|
|
|
|
6 |
import numpy as np
|
7 |
+
import time
|
8 |
+
import random
|
9 |
|
10 |
+
MAX_SEED = 1000000
|
11 |
|
12 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
13 |
start_time = time.time()
|
|
|
|
|
14 |
if person_img is None or garment_img is None:
|
15 |
return None, None, "Empty image"
|
16 |
|
|
|
17 |
if randomize_seed:
|
18 |
seed = random.randint(0, MAX_SEED)
|
19 |
+
|
20 |
+
# Encode images
|
21 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
22 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
23 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
24 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
25 |
|
26 |
+
# Pixelcut API setup
|
27 |
+
url = "https://api.developer.pixelcut.ai/v1/remove-background"
|
28 |
+
pixelcut_api_key = os.environ.get('PIXELCUT_API_KEY')
|
29 |
+
|
30 |
+
headers = {
|
31 |
+
'Content-Type': 'application/json',
|
32 |
+
'X-API-KEY': pixelcut_api_key
|
33 |
+
}
|
34 |
|
|
|
|
|
35 |
data = {
|
36 |
"clothImage": encoded_garment_img,
|
37 |
"humanImage": encoded_person_img,
|
|
|
39 |
}
|
40 |
|
41 |
result_img = None
|
42 |
+
max_retries = 5
|
43 |
+
retry_delay = 5
|
44 |
|
45 |
try:
|
46 |
session = requests.Session()
|
47 |
+
|
48 |
# Retry loop for "Too many users" error
|
49 |
for attempt in range(max_retries):
|
50 |
response = session.post(url, headers=headers, data=json.dumps(data), timeout=60)
|
51 |
+
print("Response code:", response.status_code)
|
52 |
|
53 |
if response.status_code == 200:
|
54 |
+
result = response.json().get('result', {})
|
55 |
+
status = result.get('status', '')
|
56 |
|
57 |
if status == "success":
|
58 |
+
result_data = base64.b64decode(result['result'])
|
59 |
+
result_np = np.frombuffer(result_data, np.uint8)
|
|
|
60 |
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
61 |
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
62 |
info = "Success"
|
63 |
+
break
|
|
|
64 |
else:
|
65 |
info = "Try again later"
|
|
|
|
|
66 |
else:
|
67 |
+
print("Full response:", response.text)
|
68 |
if "Too many users" in response.text:
|
69 |
print(f"Attempt {attempt + 1}/{max_retries}: API busy. Retrying in {retry_delay} seconds...")
|
70 |
time.sleep(retry_delay)
|
71 |
else:
|
72 |
+
info = f"Error: {response.status_code} - {response.text}"
|
73 |
+
break
|
74 |
|
75 |
else:
|
|
|
76 |
info = "API still overloaded β please try again later."
|
77 |
|
|
|
78 |
except requests.exceptions.ReadTimeout:
|
79 |
+
print("Timeout")
|
80 |
info = "Too many users, please try again later"
|
81 |
raise gr.Error("Too many users, please try again later")
|
82 |
|
|
|
83 |
except Exception as err:
|
84 |
print(f"Other error: {err}")
|
85 |
info = "Error, please contact the admin"
|
86 |
|
87 |
end_time = time.time()
|
88 |
+
print(f"Time used: {end_time - start_time}")
|
89 |
|
90 |
return result_img, seed, info
|
91 |
|
92 |
|
93 |
|
|
|
94 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
95 |
|
96 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|