Update app.py
Browse files
app.py
CHANGED
@@ -10,8 +10,11 @@ import gradio as gr
|
|
10 |
|
11 |
MAX_SEED = 999999
|
12 |
|
13 |
-
# β
|
14 |
-
pixelcut_api_key = "
|
|
|
|
|
|
|
15 |
|
16 |
# π― Convert image to PNG format
|
17 |
def convert_to_png(image):
|
@@ -23,15 +26,16 @@ def encode_image(image):
|
|
23 |
_, buffer = cv2.imencode('.png', image)
|
24 |
return base64.b64encode(buffer).decode('utf-8')
|
25 |
|
26 |
-
# π οΈ Upload images to
|
27 |
-
def
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
else:
|
34 |
-
print("β
|
35 |
return None
|
36 |
|
37 |
# π Main try-on function using Pixelcut API
|
@@ -50,12 +54,12 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
50 |
_, person_encoded = cv2.imencode('.png', convert_to_png(person_img))
|
51 |
_, garment_encoded = cv2.imencode('.png', convert_to_png(garment_img))
|
52 |
|
53 |
-
# β
Upload person and garment images to get URLs
|
54 |
-
person_url =
|
55 |
-
garment_url =
|
56 |
|
57 |
if not person_url or not garment_url:
|
58 |
-
return None, None, "Image upload failed"
|
59 |
|
60 |
# π Setup Pixelcut API endpoint and headers
|
61 |
url = "https://api.developer.pixelcut.ai/v1/remove-background"
|
@@ -65,16 +69,9 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
65 |
'X-API-KEY': pixelcut_api_key
|
66 |
}
|
67 |
|
68 |
-
# π§ Use uploaded URLs in the payload
|
69 |
-
person_data = {
|
70 |
-
|
71 |
-
"format": "png"
|
72 |
-
}
|
73 |
-
|
74 |
-
garment_data = {
|
75 |
-
"image_url": garment_url,
|
76 |
-
"format": "png"
|
77 |
-
}
|
78 |
|
79 |
result_img = None
|
80 |
max_retries = 5
|
@@ -135,6 +132,7 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
135 |
|
136 |
|
137 |
|
|
|
138 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
139 |
|
140 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
|
|
10 |
|
11 |
MAX_SEED = 999999
|
12 |
|
13 |
+
# β
Pixelcut API Key
|
14 |
+
pixelcut_api_key = "YOUR_PIXELCUT_API_KEY"
|
15 |
+
|
16 |
+
# β
ImgBB API Key (for uploading images to get valid URLs)
|
17 |
+
imgbb_api_key = "YOUR_IMGBB_API_KEY"
|
18 |
|
19 |
# π― Convert image to PNG format
|
20 |
def convert_to_png(image):
|
|
|
26 |
_, buffer = cv2.imencode('.png', image)
|
27 |
return base64.b64encode(buffer).decode('utf-8')
|
28 |
|
29 |
+
# π οΈ Upload images to ImgBB to get permanent URLs
|
30 |
+
def upload_image_to_imgbb(image_data):
|
31 |
+
url = f"https://api.imgbb.com/1/upload?key={imgbb_api_key}"
|
32 |
+
files = {"image": base64.b64encode(image_data).decode('utf-8')}
|
33 |
+
response = requests.post(url, files=files)
|
34 |
+
|
35 |
+
if response.status_code == 200:
|
36 |
+
return response.json().get("data", {}).get("url")
|
37 |
else:
|
38 |
+
print("β ImgBB upload failed:", response.text)
|
39 |
return None
|
40 |
|
41 |
# π Main try-on function using Pixelcut API
|
|
|
54 |
_, person_encoded = cv2.imencode('.png', convert_to_png(person_img))
|
55 |
_, garment_encoded = cv2.imencode('.png', convert_to_png(garment_img))
|
56 |
|
57 |
+
# β
Upload person and garment images to get valid URLs
|
58 |
+
person_url = upload_image_to_imgbb(person_encoded)
|
59 |
+
garment_url = upload_image_to_imgbb(garment_encoded)
|
60 |
|
61 |
if not person_url or not garment_url:
|
62 |
+
return None, None, "Image upload failed β check API keys or connection"
|
63 |
|
64 |
# π Setup Pixelcut API endpoint and headers
|
65 |
url = "https://api.developer.pixelcut.ai/v1/remove-background"
|
|
|
69 |
'X-API-KEY': pixelcut_api_key
|
70 |
}
|
71 |
|
72 |
+
# π§ Use the uploaded URLs in the payload
|
73 |
+
person_data = {"image_url": person_url, "format": "png"}
|
74 |
+
garment_data = {"image_url": garment_url, "format": "png"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
result_img = None
|
77 |
max_retries = 5
|
|
|
132 |
|
133 |
|
134 |
|
135 |
+
|
136 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
137 |
|
138 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|