Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
from torchvision import models, transforms
|
7 |
+
|
8 |
+
# Load a pre-trained body segmentation model (DeepLabV3) for human segmentation
|
9 |
+
model = models.segmentation.deeplabv3_resnet101(pretrained=True)
|
10 |
+
model.eval()
|
11 |
+
|
12 |
+
# Define the transformation for the input image
|
13 |
+
transform = transforms.Compose([
|
14 |
+
transforms.ToTensor(),
|
15 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
16 |
+
])
|
17 |
+
|
18 |
+
# Function to segment the body
|
19 |
+
def segment_body(image):
|
20 |
+
# Convert the image to RGB (needed by the model)
|
21 |
+
image = image.convert("RGB")
|
22 |
+
|
23 |
+
# Apply the transformations
|
24 |
+
input_tensor = transform(image).unsqueeze(0)
|
25 |
+
|
26 |
+
# Get the model's prediction
|
27 |
+
with torch.no_grad():
|
28 |
+
output = model(input_tensor)["out"][0]
|
29 |
+
|
30 |
+
# Convert the output to a probability map and get the human mask (class 15 is human)
|
31 |
+
output_predictions = output.argmax(0)
|
32 |
+
human_mask = output_predictions == 15 # Class 15 corresponds to human
|
33 |
+
|
34 |
+
return human_mask
|
35 |
+
|
36 |
+
# Function to overlay clothing onto the image
|
37 |
+
def overlay_clothing(user_image, clothing_image, mask):
|
38 |
+
# Resize clothing image to match the user's body size (for simplicity, we use resizing here)
|
39 |
+
clothing_resized = clothing_image.resize((user_image.width, user_image.height), Image.ANTIALIAS)
|
40 |
+
|
41 |
+
# Convert to numpy arrays for OpenCV processing
|
42 |
+
user_image_np = np.array(user_image)
|
43 |
+
clothing_resized_np = np.array(clothing_resized)
|
44 |
+
|
45 |
+
# Apply the mask to the clothing image and the user's body image
|
46 |
+
clothing_overlay = np.zeros_like(user_image_np)
|
47 |
+
clothing_overlay[mask] = clothing_resized_np[mask]
|
48 |
+
|
49 |
+
# Combine the user's image with the clothing overlay
|
50 |
+
result = user_image_np.copy()
|
51 |
+
result[mask] = clothing_overlay[mask]
|
52 |
+
|
53 |
+
return Image.fromarray(result)
|
54 |
+
|
55 |
+
def main():
|
56 |
+
st.title("Virtual Try-On App")
|
57 |
+
st.write("Upload a photo of yourself and a clothing item to virtually try it on!")
|
58 |
+
|
59 |
+
# Upload user image
|
60 |
+
uploaded_user_image = st.file_uploader("Upload your photo", type=["jpg", "png", "jpeg"])
|
61 |
+
|
62 |
+
# Upload clothing item
|
63 |
+
uploaded_clothing_image = st.file_uploader("Upload the clothing item (e.g., T-shirt)", type=["jpg", "png", "jpeg"])
|
64 |
+
|
65 |
+
if uploaded_user_image and uploaded_clothing_image:
|
66 |
+
# Open images
|
67 |
+
user_image = Image.open(uploaded_user_image)
|
68 |
+
clothing_image = Image.open(uploaded_clothing_image)
|
69 |
+
|
70 |
+
# Segment the user's body from the image
|
71 |
+
mask = segment_body(user_image)
|
72 |
+
|
73 |
+
# Overlay the clothing item onto the user's body
|
74 |
+
result_image = overlay_clothing(user_image, clothing_image, mask)
|
75 |
+
|
76 |
+
# Show the result image
|
77 |
+
st.image(result_image, caption="Virtual Try-On Result", use_column_width=True)
|
78 |
+
|
79 |
+
if __name__ == "__main__":
|
80 |
+
main()
|