Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,21 @@
|
|
1 |
import os
|
2 |
-
import cv2
|
3 |
-
import base64
|
4 |
-
import random
|
5 |
import requests
|
6 |
import json
|
7 |
import time
|
8 |
-
import numpy as np
|
9 |
import gradio as gr
|
10 |
|
11 |
MAX_SEED = 999999
|
12 |
|
13 |
-
#
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
# Encode images to base64 (forces PNG format)
|
18 |
-
def encode_image(image):
|
19 |
-
# Ensure image is PNG
|
20 |
-
image = convert_to_png(image)
|
21 |
-
_, buffer = cv2.imencode('.png', image)
|
22 |
-
return base64.b64encode(buffer).decode('utf-8')
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Main try-on function using Pixelcut API
|
26 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
@@ -34,9 +29,12 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
34 |
if randomize_seed:
|
35 |
seed = random.randint(0, MAX_SEED)
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
40 |
|
41 |
# Load Pixelcut API key from environment
|
42 |
pixelcut_api_key = os.environ.get('pixelcut_api_key')
|
@@ -51,12 +49,12 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
51 |
'X-API-KEY': pixelcut_api_key
|
52 |
}
|
53 |
|
54 |
-
# Setup request payload with
|
55 |
data = {
|
56 |
-
"
|
57 |
-
"
|
58 |
"seed": seed,
|
59 |
-
"format": "png"
|
60 |
}
|
61 |
|
62 |
result_img = None
|
@@ -79,8 +77,8 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
79 |
# If Pixelcut returns a result URL, fetch the final image
|
80 |
if result_url:
|
81 |
result_img_data = requests.get(result_url).content
|
82 |
-
|
83 |
-
result_img =
|
84 |
info = "Success"
|
85 |
break
|
86 |
|
@@ -115,7 +113,6 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
115 |
return result_img, seed, info
|
116 |
|
117 |
|
118 |
-
|
119 |
example_path = os.path.join(os.path.dirname(__file__), 'assets')
|
120 |
|
121 |
garm_list = os.listdir(os.path.join(example_path,"cloth"))
|
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import time
|
|
|
5 |
import gradio as gr
|
6 |
|
7 |
MAX_SEED = 999999
|
8 |
|
9 |
+
# Upload image to a temporary host (like Hugging Face or Imgur)
|
10 |
+
def upload_image_to_host(image_data):
|
11 |
+
files = {'file': ('image.png', image_data)}
|
12 |
+
upload_response = requests.post("https://tmpfiles.org/api/v1/upload", files=files)
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
if upload_response.status_code == 200:
|
15 |
+
return upload_response.json().get("data", {}).get("url", None)
|
16 |
+
else:
|
17 |
+
print("Image upload failed:", upload_response.text)
|
18 |
+
return None
|
19 |
|
20 |
# Main try-on function using Pixelcut API
|
21 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
|
|
29 |
if randomize_seed:
|
30 |
seed = random.randint(0, MAX_SEED)
|
31 |
|
32 |
+
# Upload images and get URLs
|
33 |
+
person_url = upload_image_to_host(person_img)
|
34 |
+
garment_url = upload_image_to_host(garment_img)
|
35 |
+
|
36 |
+
if not person_url or not garment_url:
|
37 |
+
return None, None, "Image upload failed"
|
38 |
|
39 |
# Load Pixelcut API key from environment
|
40 |
pixelcut_api_key = os.environ.get('pixelcut_api_key')
|
|
|
49 |
'X-API-KEY': pixelcut_api_key
|
50 |
}
|
51 |
|
52 |
+
# Setup request payload with URLs
|
53 |
data = {
|
54 |
+
"person_image_url": person_url,
|
55 |
+
"garment_image_url": garment_url,
|
56 |
"seed": seed,
|
57 |
+
"format": "png"
|
58 |
}
|
59 |
|
60 |
result_img = None
|
|
|
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_img = gr.Image()
|
81 |
+
result_img.value = result_img_data
|
82 |
info = "Success"
|
83 |
break
|
84 |
|
|
|
113 |
return result_img, seed, info
|
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"))
|