Update app.py
Browse files
app.py
CHANGED
@@ -10,23 +10,23 @@ import gradio as gr
|
|
10 |
|
11 |
MAX_SEED = 999999
|
12 |
|
13 |
-
# β
|
14 |
pixelcut_api_key = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
|
15 |
|
16 |
|
17 |
-
# Convert
|
18 |
def convert_to_png(image):
|
19 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
20 |
|
21 |
|
22 |
-
# Encode
|
23 |
def encode_image(image):
|
24 |
image = convert_to_png(image)
|
25 |
_, buffer = cv2.imencode('.png', image)
|
26 |
return base64.b64encode(buffer).decode('utf-8')
|
27 |
|
28 |
|
29 |
-
# Main try-on function using Pixelcut API
|
30 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
31 |
start_time = time.time()
|
32 |
|
@@ -45,54 +45,60 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
45 |
# π Setup Pixelcut API endpoint and headers
|
46 |
url = "https://api.developer.pixelcut.ai/v1/remove-background"
|
47 |
headers = {
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
}
|
52 |
|
53 |
-
#
|
54 |
-
person_data = {
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
try:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
# π Process Garment Image
|
66 |
-
garment_response = requests.post(url, headers=headers, json=garment_data, timeout=60)
|
67 |
-
if garment_response.status_code == 200:
|
68 |
-
garment_result_url = garment_response.json().get('url', '')
|
69 |
else:
|
70 |
-
|
71 |
-
|
72 |
-
# π₯ Call the try-on API with both processed images
|
73 |
-
tryon_url = "https://api.example.com/v1/tryon"
|
74 |
-
tryon_payload = {
|
75 |
-
"person_image_url": person_result_url,
|
76 |
-
"garment_image_url": garment_result_url
|
77 |
-
}
|
78 |
-
tryon_response = requests.post(tryon_url, headers=headers, json=tryon_payload, timeout=60)
|
79 |
-
|
80 |
-
# β
Handle the try-on result
|
81 |
-
if tryon_response.status_code == 200:
|
82 |
-
result_url = tryon_response.json().get('result_url', '')
|
83 |
-
result_img_data = requests.get(result_url).content
|
84 |
-
result_np = np.frombuffer(result_img_data, np.uint8)
|
85 |
-
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
86 |
-
info = "Success"
|
87 |
-
else:
|
88 |
-
info = f"Try-On error: {tryon_response.status_code}"
|
89 |
|
90 |
-
# π Handle
|
91 |
except requests.exceptions.ReadTimeout:
|
92 |
print("Timeout!")
|
93 |
info = "Too many users, please try again later"
|
94 |
raise gr.Error("Too many users, please try again later")
|
95 |
|
|
|
96 |
except Exception as err:
|
97 |
print(f"β Other error: {err}")
|
98 |
info = "Error, please contact the admin"
|
@@ -103,6 +109,7 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
103 |
|
104 |
return result_img, seed, info
|
105 |
|
|
|
106 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
107 |
|
108 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
|
|
10 |
|
11 |
MAX_SEED = 999999
|
12 |
|
13 |
+
# β
Pixelcut API key
|
14 |
pixelcut_api_key = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
|
15 |
|
16 |
|
17 |
+
# π― Convert image to PNG format
|
18 |
def convert_to_png(image):
|
19 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
20 |
|
21 |
|
22 |
+
# π₯ Encode image to base64 PNG format
|
23 |
def encode_image(image):
|
24 |
image = convert_to_png(image)
|
25 |
_, buffer = cv2.imencode('.png', image)
|
26 |
return base64.b64encode(buffer).decode('utf-8')
|
27 |
|
28 |
|
29 |
+
# π Main try-on function using Pixelcut API
|
30 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
31 |
start_time = time.time()
|
32 |
|
|
|
45 |
# π Setup Pixelcut API endpoint and headers
|
46 |
url = "https://api.developer.pixelcut.ai/v1/remove-background"
|
47 |
headers = {
|
48 |
+
"Content-Type": "application/json",
|
49 |
+
"Accept": "application/json",
|
50 |
+
"X-API-KEY": pixelcut_api_key
|
51 |
}
|
52 |
|
53 |
+
# π© Setup request payload for person image
|
54 |
+
person_data = {
|
55 |
+
"image_url": f"data:image/png;base64,{encoded_person_img}",
|
56 |
+
"format": "png"
|
57 |
+
}
|
58 |
+
|
59 |
+
result_img = None
|
60 |
+
max_retries = 5
|
61 |
+
retry_delay = 5
|
62 |
|
63 |
try:
|
64 |
+
session = requests.Session()
|
65 |
+
|
66 |
+
# π Send person image for background removal
|
67 |
+
for attempt in range(max_retries):
|
68 |
+
person_response = session.post(url, headers=headers, json=person_data, timeout=60)
|
69 |
+
print("Person Response:", person_response.status_code, person_response.text)
|
70 |
+
|
71 |
+
# β
Success β get the result URL
|
72 |
+
if person_response.status_code == 200:
|
73 |
+
person_result_url = person_response.json().get("result_url", "")
|
74 |
+
print("β
Person Success:", person_result_url)
|
75 |
+
|
76 |
+
# Fetch the processed person image
|
77 |
+
if person_result_url:
|
78 |
+
person_img_data = requests.get(person_result_url).content
|
79 |
+
person_np = np.frombuffer(person_img_data, np.uint8)
|
80 |
+
result_img = cv2.imdecode(person_np, cv2.IMREAD_UNCHANGED)
|
81 |
+
info = "Success"
|
82 |
+
break
|
83 |
+
|
84 |
+
else:
|
85 |
+
# π₯ If API returns "Too many users" or other errors
|
86 |
+
error_details = person_response.json().get("error", "Unknown error")
|
87 |
+
print(f"Attempt {attempt + 1}/{max_retries}: Error - {error_details}")
|
88 |
+
time.sleep(retry_delay)
|
89 |
+
info = f"Person image error: {person_response.status_code} - {error_details}"
|
90 |
|
|
|
|
|
|
|
|
|
91 |
else:
|
92 |
+
# β If retries fail
|
93 |
+
info = "API still overloaded β please try again later."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
# π Handle timeouts
|
96 |
except requests.exceptions.ReadTimeout:
|
97 |
print("Timeout!")
|
98 |
info = "Too many users, please try again later"
|
99 |
raise gr.Error("Too many users, please try again later")
|
100 |
|
101 |
+
# β Catch any other unexpected errors
|
102 |
except Exception as err:
|
103 |
print(f"β Other error: {err}")
|
104 |
info = "Error, please contact the admin"
|
|
|
109 |
|
110 |
return result_img, seed, info
|
111 |
|
112 |
+
|
113 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
114 |
|
115 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|