Spaces:
Running
Running
Upload 7 files
Browse files- Procfile +1 -0
- README.md +56 -12
- app.py +148 -0
- output.jpg +0 -0
- packages.txt +3 -0
- requirements.txt +4 -0
- vton.py +67 -0
Procfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
web: streamlit run app.py
|
README.md
CHANGED
@@ -1,12 +1,56 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Virtual Try-On Application
|
2 |
+
|
3 |
+
This application uses the IDM-VTON model via Replicate API to perform virtual try-on of garments on human images.
|
4 |
+
|
5 |
+
## Requirements
|
6 |
+
|
7 |
+
- Python 3.6+
|
8 |
+
- `replicate` Python package
|
9 |
+
|
10 |
+
## Installation
|
11 |
+
|
12 |
+
1. Install the required package:
|
13 |
+
|
14 |
+
```bash
|
15 |
+
pip install replicate
|
16 |
+
```
|
17 |
+
|
18 |
+
2. Set up your Replicate API token as an environment variable:
|
19 |
+
|
20 |
+
```bash
|
21 |
+
export REPLICATE_API_TOKEN='your_token_here'
|
22 |
+
```
|
23 |
+
|
24 |
+
You can get your API token from [Replicate](https://replicate.com/account/api-tokens) after creating an account.
|
25 |
+
|
26 |
+
## Usage
|
27 |
+
|
28 |
+
Run the script with:
|
29 |
+
|
30 |
+
```bash
|
31 |
+
python vton.py
|
32 |
+
```
|
33 |
+
|
34 |
+
By default, the script uses example images. To use your own images, modify the `garm_img`, `human_img`, and `garment_des` variables in the script.
|
35 |
+
|
36 |
+
### Function Usage
|
37 |
+
|
38 |
+
You can also import the function in your own Python code:
|
39 |
+
|
40 |
+
```python
|
41 |
+
from vton import virtual_try_on
|
42 |
+
|
43 |
+
output_path = virtual_try_on(
|
44 |
+
garm_img_url="URL_TO_GARMENT_IMAGE",
|
45 |
+
human_img_url="URL_TO_HUMAN_IMAGE",
|
46 |
+
garment_des="Description of the garment"
|
47 |
+
)
|
48 |
+
```
|
49 |
+
|
50 |
+
## Output
|
51 |
+
|
52 |
+
The script will save the generated try-on image as `output.jpg` in the current directory.
|
53 |
+
|
54 |
+
## Model Information
|
55 |
+
|
56 |
+
This application uses the IDM-VTON model created by cuuupid on Replicate. The model performs virtual try-on by overlaying garments onto human images based on the provided description.
|
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from vton import virtual_try_on
|
4 |
+
import requests
|
5 |
+
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Set page config for a wider layout and custom title
|
9 |
+
st.set_page_config(
|
10 |
+
page_title="Virtual Try-On App",
|
11 |
+
page_icon="👔",
|
12 |
+
layout="wide",
|
13 |
+
initial_sidebar_state="expanded"
|
14 |
+
)
|
15 |
+
|
16 |
+
# Custom CSS for better styling
|
17 |
+
st.markdown("""
|
18 |
+
<style>
|
19 |
+
.stApp {
|
20 |
+
max-width: 1200px;
|
21 |
+
margin: 0 auto;
|
22 |
+
}
|
23 |
+
.upload-box {
|
24 |
+
border: 2px dashed #4c4c4c;
|
25 |
+
border-radius: 10px;
|
26 |
+
padding: 20px;
|
27 |
+
text-align: center;
|
28 |
+
margin: 10px 0;
|
29 |
+
}
|
30 |
+
.output-box {
|
31 |
+
border: 1px solid #e0e0e0;
|
32 |
+
border-radius: 10px;
|
33 |
+
padding: 20px;
|
34 |
+
margin: 20px 0;
|
35 |
+
background-color: #f8f9fa;
|
36 |
+
}
|
37 |
+
</style>
|
38 |
+
""", unsafe_allow_html=True)
|
39 |
+
|
40 |
+
# Title and description
|
41 |
+
st.title("✨ Virtual Try-On Application")
|
42 |
+
st.markdown("""Upload a garment image and a person's photo to see how the garment would look on them!""")
|
43 |
+
|
44 |
+
# Create two columns for input images
|
45 |
+
col1, col2 = st.columns(2)
|
46 |
+
|
47 |
+
# Garment image upload
|
48 |
+
with col1:
|
49 |
+
st.subheader("Garment Image")
|
50 |
+
garment_source = st.radio(
|
51 |
+
"Choose garment image source:",
|
52 |
+
["Upload", "URL"],
|
53 |
+
key="garment_source"
|
54 |
+
)
|
55 |
+
|
56 |
+
if garment_source == "Upload":
|
57 |
+
garment_file = st.file_uploader("Upload garment image", type=["jpg", "jpeg", "png", "webp"], key="garment_upload")
|
58 |
+
if garment_file:
|
59 |
+
st.image(garment_file, caption="Uploaded Garment", use_container_width=True)
|
60 |
+
# Save uploaded file to temporary URL
|
61 |
+
garment_img = Image.open(garment_file)
|
62 |
+
garment_buffer = BytesIO()
|
63 |
+
garment_img.save(garment_buffer, format="PNG")
|
64 |
+
garment_url = "temp_garment.png"
|
65 |
+
with open(garment_url, "wb") as f:
|
66 |
+
f.write(garment_buffer.getvalue())
|
67 |
+
else:
|
68 |
+
garment_url = st.text_input("Enter garment image URL", key="garment_url")
|
69 |
+
if garment_url:
|
70 |
+
try:
|
71 |
+
st.image(garment_url, caption="Garment from URL", use_container_width=True)
|
72 |
+
except:
|
73 |
+
st.error("Unable to load image from URL")
|
74 |
+
|
75 |
+
# Person image upload
|
76 |
+
with col2:
|
77 |
+
st.subheader("Person Image")
|
78 |
+
person_source = st.radio(
|
79 |
+
"Choose person image source:",
|
80 |
+
["Upload", "URL"],
|
81 |
+
key="person_source"
|
82 |
+
)
|
83 |
+
|
84 |
+
if person_source == "Upload":
|
85 |
+
person_file = st.file_uploader("Upload person image", type=["jpg", "jpeg", "png", "webp"], key="person_upload")
|
86 |
+
if person_file:
|
87 |
+
st.image(person_file, caption="Uploaded Person", use_container_width=True)
|
88 |
+
# Save uploaded file to temporary URL
|
89 |
+
person_img = Image.open(person_file)
|
90 |
+
person_buffer = BytesIO()
|
91 |
+
person_img.save(person_buffer, format="PNG")
|
92 |
+
person_url = "temp_person.png"
|
93 |
+
with open(person_url, "wb") as f:
|
94 |
+
f.write(person_buffer.getvalue())
|
95 |
+
else:
|
96 |
+
person_url = st.text_input("Enter person image URL", key="person_url")
|
97 |
+
if person_url:
|
98 |
+
try:
|
99 |
+
st.image(person_url, caption="Person from URL", use_container_width=True)
|
100 |
+
except:
|
101 |
+
st.error("Unable to load image from URL")
|
102 |
+
|
103 |
+
# Garment description input
|
104 |
+
st.subheader("Garment Description")
|
105 |
+
garment_desc = st.text_area(
|
106 |
+
"Describe the garment (e.g., color, style, type)",
|
107 |
+
height=100,
|
108 |
+
placeholder="Example: A cute pink top with floral pattern"
|
109 |
+
)
|
110 |
+
|
111 |
+
# Check for API token
|
112 |
+
if "REPLICATE_API_TOKEN" not in os.environ:
|
113 |
+
st.warning(
|
114 |
+
"⚠️ REPLICATE_API_TOKEN is not set. Please set it using: "
|
115 |
+
"export REPLICATE_API_TOKEN='your_token_here'"
|
116 |
+
)
|
117 |
+
|
118 |
+
# Process button
|
119 |
+
if st.button("Generate Try-On", type="primary"):
|
120 |
+
if not (garment_url and person_url and garment_desc):
|
121 |
+
st.error("Please provide all required inputs!")
|
122 |
+
else:
|
123 |
+
try:
|
124 |
+
with st.spinner("🔄 Processing virtual try-on..."):
|
125 |
+
output_path = virtual_try_on(garment_url, person_url, garment_desc)
|
126 |
+
|
127 |
+
# Display result in a styled box
|
128 |
+
st.markdown("<div class='output-box'>", unsafe_allow_html=True)
|
129 |
+
st.subheader("🎉 Try-On Result")
|
130 |
+
st.image(output_path, caption="Virtual Try-On Result", use_container_width=True)
|
131 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
132 |
+
|
133 |
+
# Cleanup temporary files
|
134 |
+
if os.path.exists("temp_garment.png"):
|
135 |
+
os.remove("temp_garment.png")
|
136 |
+
if os.path.exists("temp_person.png"):
|
137 |
+
os.remove("temp_person.png")
|
138 |
+
|
139 |
+
except Exception as e:
|
140 |
+
st.error(f"Error occurred: {str(e)}")
|
141 |
+
|
142 |
+
# Footer
|
143 |
+
st.markdown("---")
|
144 |
+
st.markdown("""
|
145 |
+
<div style='text-align: center; color: #666;'>
|
146 |
+
<p>Powered by IDM-VTON model via Replicate API</p>
|
147 |
+
</div>
|
148 |
+
""", unsafe_allow_html=True)
|
output.jpg
ADDED
![]() |
packages.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
python3-dev
|
2 |
+
python3-pip
|
3 |
+
python3-venv
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
replicate==0.22.0
|
2 |
+
streamlit==1.31.0
|
3 |
+
Pillow==10.2.0
|
4 |
+
requests==2.31.0
|
vton.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import replicate
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
def virtual_try_on(garm_img_url, human_img_url, garment_des):
|
6 |
+
"""
|
7 |
+
Perform virtual try-on using the IDM-VTON model on Replicate.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
garm_img_url (str): Path or URL of the garment image
|
11 |
+
human_img_url (str): Path or URL of the human image
|
12 |
+
garment_des (str): Description of the garment
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
str: Path to the saved output image
|
16 |
+
"""
|
17 |
+
# Convert local files to data URLs if needed
|
18 |
+
def file_to_data_url(file_path):
|
19 |
+
if file_path.startswith('http'):
|
20 |
+
response = requests.get(file_path)
|
21 |
+
img_data = response.content
|
22 |
+
else:
|
23 |
+
with open(file_path, 'rb') as f:
|
24 |
+
img_data = f.read()
|
25 |
+
import base64
|
26 |
+
encoded = base64.b64encode(img_data).decode('utf-8')
|
27 |
+
return f"data:image/png;base64,{encoded}"
|
28 |
+
|
29 |
+
# Prepare input for the model
|
30 |
+
input_data = {
|
31 |
+
"garm_img": file_to_data_url(garm_img_url),
|
32 |
+
"human_img": file_to_data_url(human_img_url),
|
33 |
+
"garment_des": garment_des
|
34 |
+
}
|
35 |
+
|
36 |
+
print("Sending request to IDM-VTON model...")
|
37 |
+
# Run the model on Replicate
|
38 |
+
output = replicate.run(
|
39 |
+
"cuuupid/idm-vton:c871bb9b046607b680449ecbae55fd8c6d945e0a1948644bf2361b3d021d3ff4",
|
40 |
+
input=input_data
|
41 |
+
)
|
42 |
+
|
43 |
+
# Save the output image
|
44 |
+
output_path = "output.jpg"
|
45 |
+
with open(output_path, "wb") as file:
|
46 |
+
file.write(output.read())
|
47 |
+
|
48 |
+
print(f"Virtual try-on complete! Output saved to {output_path}")
|
49 |
+
return output_path
|
50 |
+
|
51 |
+
# Example usage
|
52 |
+
if __name__ == "__main__":
|
53 |
+
# Example inputs
|
54 |
+
garm_img = "https://replicate.delivery/pbxt/KgwTlZyFx5aUU3gc5gMiKuD5nNPTgliMlLUWx160G4z99YjO/sweater.webp"
|
55 |
+
human_img = "https://replicate.delivery/pbxt/KgwTlhCMvDagRrcVzZJbuozNJ8esPqiNAIJS3eMgHrYuHmW4/KakaoTalk_Photo_2024-04-04-21-44-45.png"
|
56 |
+
garment_des = "cute pink top"
|
57 |
+
|
58 |
+
# Check if REPLICATE_API_TOKEN is set
|
59 |
+
if "REPLICATE_API_TOKEN" not in os.environ:
|
60 |
+
print("Warning: REPLICATE_API_TOKEN environment variable is not set.")
|
61 |
+
print("Please set it using: export REPLICATE_API_TOKEN='your_token_here'")
|
62 |
+
|
63 |
+
try:
|
64 |
+
output_path = virtual_try_on(garm_img, human_img, garment_des)
|
65 |
+
print(f"Success! Try-on image saved to {output_path}")
|
66 |
+
except Exception as e:
|
67 |
+
print(f"Error occurred: {e}")
|