Update app.py
Browse files
app.py
CHANGED
@@ -1,116 +1,66 @@
|
|
1 |
-
import os
|
2 |
import requests
|
3 |
-
import json
|
4 |
-
import time
|
5 |
-
import cv2
|
6 |
-
import base64
|
7 |
-
import random
|
8 |
-
import numpy as np
|
9 |
import gradio as gr
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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 |
-
|
33 |
-
# π Check if images are provided
|
34 |
-
if person_img is None or garment_img is None:
|
35 |
-
return None, None, "Empty image"
|
36 |
-
|
37 |
-
# π² Handle seed randomization
|
38 |
-
if randomize_seed:
|
39 |
-
seed = random.randint(0, MAX_SEED)
|
40 |
-
|
41 |
-
# π₯ Convert images to base64 PNG
|
42 |
-
encoded_person_img = encode_image(person_img)
|
43 |
-
encoded_garment_img = encode_image(garment_img)
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
}
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
"image": encoded_person_img,
|
56 |
-
"format": "png"
|
57 |
-
}
|
58 |
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
response = session.post(url, headers=headers, json=data, timeout=60)
|
70 |
-
print("Response code:", response.status_code)
|
71 |
-
|
72 |
-
# β
Success β process the result
|
73 |
-
if response.status_code == 200:
|
74 |
-
result_url = response.json().get('result_url', '')
|
75 |
-
print("β
Success:", result_url)
|
76 |
-
|
77 |
-
# If Pixelcut returns a result URL, fetch the final image
|
78 |
-
if result_url:
|
79 |
-
result_img_data = requests.get(result_url).content
|
80 |
-
result_np = np.frombuffer(result_img_data, np.uint8)
|
81 |
-
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
82 |
-
info = "Success"
|
83 |
-
break
|
84 |
-
|
85 |
-
else:
|
86 |
-
# π₯ If API returns "Too many users" error, retry with delay
|
87 |
-
print("Full response:", response.text)
|
88 |
-
if "Too many users" in response.text:
|
89 |
-
print(f"Attempt {attempt + 1}/{max_retries}: API busy. Retrying in {retry_delay} seconds...")
|
90 |
-
time.sleep(retry_delay)
|
91 |
-
else:
|
92 |
-
info = f"Error: {response.status_code} - {response.text}"
|
93 |
-
break
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
except requests.exceptions.ReadTimeout:
|
101 |
-
print("Timeout!")
|
102 |
-
info = "Too many users, please try again later"
|
103 |
-
raise gr.Error("Too many users, please try again later")
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
109 |
|
110 |
-
|
111 |
-
print(f"β³ Time used: {end_time - start_time:.2f} seconds")
|
112 |
|
113 |
-
return result_img, seed, info
|
114 |
|
115 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
116 |
|
|
|
|
|
1 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
+
# Set your Pixelcut API key directly
|
5 |
+
API_KEY = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
|
6 |
|
7 |
+
# Function to remove background from a local image file
|
8 |
+
def remove_bg(image_path):
|
9 |
+
try:
|
10 |
+
with open(image_path, "rb") as image_file:
|
11 |
+
response = requests.post(
|
12 |
+
"https://api.developer.pixelcut.ai/v1/remove-background",
|
13 |
+
headers={
|
14 |
+
"Accept": "application/json",
|
15 |
+
"X-API-KEY": API_KEY,
|
16 |
+
},
|
17 |
+
files={"image": image_file},
|
18 |
+
data={"format": "png"},
|
19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Handle API response
|
22 |
+
if response.status_code == 200:
|
23 |
+
result = response.json()
|
24 |
+
return result["output_url"] # Return the output image URL from API
|
25 |
+
else:
|
26 |
+
return f"Error: {response.status_code} - {response.json()['error']}"
|
|
|
27 |
|
28 |
+
except Exception as e:
|
29 |
+
return f"Exception: {e}"
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
+
# Define the Tryonn function without CSS
|
33 |
+
def tryonn(person_image, garment_image):
|
34 |
+
# Process the images using the Pixelcut API
|
35 |
+
person_result = remove_bg(person_image)
|
36 |
+
garment_result = remove_bg(garment_image)
|
37 |
|
38 |
+
# If both images are processed successfully, combine them (mock-up display)
|
39 |
+
if "Error" not in person_result and "Error" not in garment_result:
|
40 |
+
return person_result, garment_result, "β
Success!"
|
41 |
+
else:
|
42 |
+
return person_result, garment_result, "β Failed, check errors."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Gradio interface setup (CSS removed)
|
45 |
+
with gr.Blocks() as Tryon:
|
46 |
+
gr.Markdown("## Upload a Person Image and Garment Image")
|
47 |
+
with gr.Row():
|
48 |
+
person_input = gr.Image(type="filepath", label="Person Image")
|
49 |
+
garment_input = gr.Image(type="filepath", label="Garment Image")
|
50 |
+
output_person = gr.Image(label="Processed Person Image")
|
51 |
+
output_garment = gr.Image(label="Processed Garment Image")
|
52 |
+
status = gr.Textbox(label="Status")
|
53 |
|
54 |
+
submit_button = gr.Button("Run Try-On")
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
submit_button.click(
|
57 |
+
fn=tryonn,
|
58 |
+
inputs=[person_input, garment_input],
|
59 |
+
outputs=[output_person, output_garment, status],
|
60 |
+
)
|
61 |
|
62 |
+
Tryon.launch()
|
|
|
63 |
|
|
|
64 |
|
65 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
66 |
|