Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,61 @@
|
|
1 |
import requests
|
2 |
-
import
|
3 |
|
4 |
-
#
|
5 |
API_KEY = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
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 |
-
|
39 |
-
|
40 |
-
|
41 |
else:
|
42 |
-
|
43 |
|
44 |
-
#
|
45 |
-
|
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')
|
|
|
1 |
import requests
|
2 |
+
import base64
|
3 |
|
4 |
+
# --- Configuration ---
|
5 |
API_KEY = "sk_7c19e4f2ad434a3ebc70fdd85ade5309"
|
6 |
+
PERSON_IMAGE_PATH = "path_to_person_image.jpg"
|
7 |
+
GARMENT_IMAGE_PATH = "path_to_garment_image.jpg"
|
8 |
+
|
9 |
+
# --- Helper Function: Read Image and Convert to Base64 ---
|
10 |
+
def encode_image(image_path):
|
11 |
+
with open(image_path, "rb") as image_file:
|
12 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
13 |
+
|
14 |
+
# --- Step 1: Remove Background from Person Image ---
|
15 |
+
def remove_background(image_path):
|
16 |
+
with open(image_path, "rb") as image_file:
|
17 |
+
response = requests.post(
|
18 |
+
"https://api.developer.pixelcut.ai/v1/remove-background",
|
19 |
+
headers={
|
20 |
+
"Accept": "application/json",
|
21 |
+
"X-API-KEY": API_KEY,
|
22 |
+
},
|
23 |
+
files={"image": image_file},
|
24 |
+
data={"format": "png"},
|
25 |
+
)
|
26 |
|
27 |
+
if response.status_code == 200:
|
28 |
+
return response.json().get("url")
|
29 |
+
else:
|
30 |
+
print("Error:", response.json())
|
31 |
+
return None
|
32 |
+
|
33 |
+
# --- Step 2: Try-On Function ---
|
34 |
+
def tryonn(person_image_path, garment_image_path):
|
35 |
+
# Get background-removed URLs
|
36 |
+
person_url = remove_background(person_image_path)
|
37 |
+
garment_url = remove_background(garment_image_path)
|
38 |
+
|
39 |
+
if not person_url or not garment_url:
|
40 |
+
print("Failed to process images!")
|
41 |
+
return
|
42 |
+
|
43 |
+
# Make the try-on API call
|
44 |
+
tryon_response = requests.post(
|
45 |
+
"https://api.example.com/v1/tryon",
|
46 |
+
headers={"Accept": "application/json", "X-API-KEY": API_KEY},
|
47 |
+
json={"person_image_url": person_url, "garment_image_url": garment_url},
|
48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
if tryon_response.status_code == 200:
|
51 |
+
tryon_result_url = tryon_response.json().get("result_url")
|
52 |
+
print("Try-On Success! Result:", tryon_result_url)
|
53 |
else:
|
54 |
+
print("Try-On Failed:", tryon_response.json())
|
55 |
|
56 |
+
# --- Execute the try-on function ---
|
57 |
+
tryonn(PERSON_IMAGE_PATH, GARMENT_IMAGE_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
|
|
59 |
|
60 |
|
61 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|