|
import streamlit as st |
|
from PIL import Image |
|
from io import BytesIO |
|
from transformers import pipeline |
|
|
|
|
|
st.title("π Virtual Dress Try-On") |
|
st.write(""" |
|
Upload a **Human Body Image** and a **Garment Image** to generate a Virtual Try-On. |
|
Images will be compressed to meet Hugging Face size constraints. |
|
""") |
|
|
|
|
|
def compress_image(image_file, max_size_kb=512): |
|
""" |
|
Compresses the input image to meet size constraints while preserving the aspect ratio. |
|
Args: |
|
image_file: Uploaded file from Streamlit |
|
max_size_kb: Maximum file size in kilobytes |
|
Returns: |
|
Compressed PIL Image object |
|
""" |
|
img = Image.open(image_file).convert("RGB") |
|
quality = 95 |
|
img_format = "JPEG" |
|
|
|
|
|
while True: |
|
img_bytes = BytesIO() |
|
img.save(img_bytes, format=img_format, quality=quality) |
|
size_kb = len(img_bytes.getvalue()) / 1024 |
|
|
|
if size_kb <= max_size_kb or quality <= 10: |
|
break |
|
quality -= 5 |
|
|
|
compressed_img = Image.open(img_bytes) |
|
return compressed_img |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
model_pipeline = pipeline("image-to-image", model="ares1123/virtual-dress-try-on") |
|
return model_pipeline |
|
|
|
model = load_model() |
|
|
|
|
|
st.sidebar.header("Upload Images") |
|
uploaded_person = st.sidebar.file_uploader("Upload Human Body Image", type=["jpg", "jpeg", "png"]) |
|
uploaded_clothing = st.sidebar.file_uploader("Upload Garment Image", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
if uploaded_person and uploaded_clothing: |
|
|
|
st.sidebar.info("Compressing images to meet size constraints...") |
|
person_image = compress_image(uploaded_person) |
|
garment_image = compress_image(uploaded_clothing) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
col1.subheader("Compressed Human Body Image") |
|
col1.image(person_image, use_column_width=True) |
|
|
|
col2.subheader("Compressed Garment Image") |
|
col2.image(garment_image, use_column_width=True) |
|
|
|
|
|
if st.button("π Generate Virtual Try-On"): |
|
with st.spinner("Processing images... Please wait β³"): |
|
try: |
|
|
|
inputs = {"image": person_image, "clothing": garment_image} |
|
|
|
|
|
output_image = model(inputs) |
|
|
|
|
|
st.subheader("β¨ Virtual Try-On Result") |
|
st.image(output_image, use_column_width=True, caption="Composite Virtual Try-On Image") |
|
except Exception as e: |
|
st.error(f"An error occurred during processing: {e}") |
|
else: |
|
st.warning("Please upload both the Human Body Image and Garment Image.") |
|
|
|
|
|
st.markdown("---") |
|
st.write("Developed with β€οΈ using Streamlit and Hugging Face.") |
|
|