|
import requests |
|
import gradio as gr |
|
|
|
|
|
API_KEY = "sk_7c19e4f2ad434a3ebc70fdd85ade5309" |
|
|
|
|
|
def remove_bg(image_path): |
|
try: |
|
with open(image_path, "rb") as image_file: |
|
response = requests.post( |
|
"https://api.developer.pixelcut.ai/v1/remove-background", |
|
headers={ |
|
"Accept": "application/json", |
|
"X-API-KEY": API_KEY, |
|
}, |
|
files={"image": image_file}, |
|
data={"format": "png"}, |
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
result = response.json() |
|
return result["output_url"] |
|
else: |
|
return f"Error: {response.status_code} - {response.json()['error']}" |
|
|
|
except Exception as e: |
|
return f"Exception: {e}" |
|
|
|
|
|
|
|
def tryonn(person_image, garment_image): |
|
|
|
person_result = remove_bg(person_image) |
|
garment_result = remove_bg(garment_image) |
|
|
|
|
|
if "Error" not in person_result and "Error" not in garment_result: |
|
return person_result, garment_result, "✅ Success!" |
|
else: |
|
return person_result, garment_result, "❌ Failed, check errors." |
|
|
|
|
|
with gr.Blocks() as Tryon: |
|
gr.Markdown("## Upload a Person Image and Garment Image") |
|
with gr.Row(): |
|
person_input = gr.Image(type="filepath", label="Person Image") |
|
garment_input = gr.Image(type="filepath", label="Garment Image") |
|
output_person = gr.Image(label="Processed Person Image") |
|
output_garment = gr.Image(label="Processed Garment Image") |
|
status = gr.Textbox(label="Status") |
|
|
|
submit_button = gr.Button("Run Try-On") |
|
|
|
submit_button.click( |
|
fn=tryonn, |
|
inputs=[person_input, garment_input], |
|
outputs=[output_person, output_garment, status], |
|
) |
|
|
|
Tryon.launch() |
|
|
|
|
|
example_path = os.path.join(os.path.dirname(__file__), 'assets') |
|
|
|
garm_list = os.listdir(os.path.join(example_path,"cloth")) |
|
garm_list_path = [os.path.join(example_path,"cloth",garm) for garm in garm_list] |
|
|
|
human_list = os.listdir(os.path.join(example_path,"human")) |
|
human_list_path = [os.path.join(example_path,"human",human) for human in human_list] |
|
|
|
css=""" |
|
#col-left { |
|
margin: 0 auto; |
|
max-width: 430px; |
|
} |
|
#col-mid { |
|
margin: 0 auto; |
|
max-width: 430px; |
|
} |
|
#col-right { |
|
margin: 0 auto; |
|
max-width: 430px; |
|
} |
|
#col-showcase { |
|
margin: 0 auto; |
|
max-width: 1100px; |
|
} |
|
#button { |
|
color: blue; |
|
} |
|
""" |
|
|
|
def load_description(fp): |
|
with open(fp, 'r', encoding='utf-8') as f: |
|
content = f.read() |
|
return content |
|
|
|
def change_imgs(image1, image2): |
|
return image1, image2 |
|
|
|
with gr.Blocks(css=css) as Tryon: |
|
gr.HTML(load_description("assets/new_title.md")) |
|
with gr.Row(): |
|
with gr.Column(elem_id = "col-left"): |
|
gr.HTML(""" |
|
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;"> |
|
<div> |
|
Step 1. Upload a person image ⬇️ |
|
</div> |
|
</div> |
|
""") |
|
with gr.Column(elem_id = "col-mid"): |
|
gr.HTML(""" |
|
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;"> |
|
<div> |
|
Step 2. Upload a garment image ⬇️ |
|
</div> |
|
</div> |
|
""") |
|
with gr.Column(elem_id = "col-right"): |
|
gr.HTML(""" |
|
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;"> |
|
<div> |
|
Step 3. Press “Run” to get try-on results |
|
</div> |
|
</div> |
|
""") |
|
with gr.Row(): |
|
with gr.Column(elem_id = "col-left"): |
|
imgs = gr.Image(label="Person image", sources='upload', type="numpy") |
|
|
|
example = gr.Examples( |
|
inputs=imgs, |
|
examples_per_page=12, |
|
examples=human_list_path |
|
) |
|
with gr.Column(elem_id = "col-mid"): |
|
garm_img = gr.Image(label="Garment image", sources='upload', type="numpy") |
|
example = gr.Examples( |
|
inputs=garm_img, |
|
examples_per_page=12, |
|
examples=garm_list_path |
|
) |
|
with gr.Column(elem_id = "col-right"): |
|
image_out = gr.Image(label="Result", show_share_button=False) |
|
with gr.Row(): |
|
seed = gr.Slider( |
|
label="Seed", |
|
minimum=0, |
|
maximum=MAX_SEED, |
|
step=1, |
|
value=0, |
|
) |
|
randomize_seed = gr.Checkbox(label="Random seed", value=True) |
|
with gr.Row(): |
|
seed_used = gr.Number(label="Seed used") |
|
result_info = gr.Text(label="Response") |
|
|
|
test_button = gr.Button(value="Run", elem_id="button") |
|
|
|
|
|
|
|
test_button.click(fn=tryon, inputs=[imgs, garm_img, seed, randomize_seed], outputs=[image_out, seed_used, result_info], api_name=False, concurrency_limit=45) |
|
|
|
with gr.Column(elem_id = "col-showcase"): |
|
gr.HTML(""" |
|
<div style="display: flex; justify-content: center; align-items: center; text-align: center; font-size: 20px;"> |
|
<div> </div> |
|
<br> |
|
<div> |
|
Virtual try-on examples in pairs of person and garment images |
|
</div> |
|
</div> |
|
""") |
|
show_case = gr.Examples( |
|
examples=[ |
|
["assets/examples/model2.png", "assets/examples/garment2.png", "assets/examples/result2.png"], |
|
["assets/examples/model3.png", "assets/examples/garment3.png", "assets/examples/result3.png"], |
|
["assets/examples/model1.png", "assets/examples/garment1.png", "assets/examples/result1.png"], |
|
], |
|
inputs=[imgs, garm_img, image_out], |
|
label=None |
|
) |
|
|
|
Tryon.queue(api_open=False).launch(show_api=False) |
|
|