Update app.py
Browse files
app.py
CHANGED
@@ -6,26 +6,39 @@ import os
|
|
6 |
|
7 |
# Title and Description
|
8 |
st.title("Denim Virtual Try-On")
|
9 |
-
st.write("Upload your leg
|
10 |
|
11 |
# File Upload Section
|
12 |
st.sidebar.header("Upload Images")
|
13 |
subject_image = st.sidebar.file_uploader("Upload Subject Image (Legs)", type=["jpg", "jpeg", "png"])
|
14 |
pant_front_image = st.sidebar.file_uploader("Upload Denim Pant Front Image", type=["jpg", "jpeg", "png"])
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Function to process images using Hugging Face API
|
18 |
-
def virtual_tryon(subject_img, front_img
|
19 |
url = "https://api-inference.huggingface.co/models/ramim36/Kolors-Virtual-Try-On"
|
20 |
headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"} # Set your Hugging Face API token in env variable
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
# Prepare data
|
23 |
files = {
|
24 |
-
"subject_image":
|
25 |
-
"front_image":
|
26 |
-
"back_image": back_img
|
27 |
}
|
28 |
-
|
29 |
response = requests.post(url, headers=headers, files=files)
|
30 |
if response.status_code == 200:
|
31 |
return Image.open(BytesIO(response.content))
|
@@ -33,20 +46,30 @@ def virtual_tryon(subject_img, front_img, back_img):
|
|
33 |
st.error(f"Error: {response.status_code} - {response.text}")
|
34 |
return None
|
35 |
|
36 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if subject_image:
|
38 |
st.image(subject_image, caption="Uploaded Subject Image", use_column_width=True)
|
|
|
39 |
if pant_front_image:
|
40 |
-
st.
|
41 |
-
|
42 |
-
st.image(
|
|
|
|
|
|
|
43 |
|
44 |
# Generate Try-On Output
|
45 |
if st.button("Generate Virtual Try-On"):
|
46 |
-
if subject_image and pant_front_image
|
47 |
with st.spinner("Processing... Please wait."):
|
48 |
try:
|
49 |
-
composite_image = virtual_tryon(subject_image, pant_front_image
|
50 |
if composite_image:
|
51 |
st.image(composite_image, caption="Virtual Try-On Result", use_column_width=True)
|
52 |
except Exception as e:
|
|
|
6 |
|
7 |
# Title and Description
|
8 |
st.title("Denim Virtual Try-On")
|
9 |
+
st.write("Upload your leg image and the front image of denim pants to see how they fit.")
|
10 |
|
11 |
# File Upload Section
|
12 |
st.sidebar.header("Upload Images")
|
13 |
subject_image = st.sidebar.file_uploader("Upload Subject Image (Legs)", type=["jpg", "jpeg", "png"])
|
14 |
pant_front_image = st.sidebar.file_uploader("Upload Denim Pant Front Image", type=["jpg", "jpeg", "png"])
|
15 |
+
|
16 |
+
# Function to resize images to a manageable size
|
17 |
+
def resize_image(image, max_size=(512, 512)):
|
18 |
+
"""Resize the image to a max size to reduce payload."""
|
19 |
+
img = Image.open(image)
|
20 |
+
img = img.convert("RGB") # Ensure image is in RGB format
|
21 |
+
img.thumbnail(max_size) # Resize while maintaining aspect ratio
|
22 |
+
byte_arr = BytesIO()
|
23 |
+
img.save(byte_arr, format="JPEG") # Save to BytesIO in JPEG format
|
24 |
+
byte_arr.seek(0)
|
25 |
+
return byte_arr
|
26 |
|
27 |
# Function to process images using Hugging Face API
|
28 |
+
def virtual_tryon(subject_img, front_img):
|
29 |
url = "https://api-inference.huggingface.co/models/ramim36/Kolors-Virtual-Try-On"
|
30 |
headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"} # Set your Hugging Face API token in env variable
|
31 |
+
|
32 |
+
# Resize images before sending
|
33 |
+
resized_subject = resize_image(subject_img)
|
34 |
+
resized_front = resize_image(front_img)
|
35 |
+
|
36 |
# Prepare data
|
37 |
files = {
|
38 |
+
"subject_image": ("subject.jpg", resized_subject, "image/jpeg"),
|
39 |
+
"front_image": ("front.jpg", resized_front, "image/jpeg")
|
|
|
40 |
}
|
41 |
+
|
42 |
response = requests.post(url, headers=headers, files=files)
|
43 |
if response.status_code == 200:
|
44 |
return Image.open(BytesIO(response.content))
|
|
|
46 |
st.error(f"Error: {response.status_code} - {response.text}")
|
47 |
return None
|
48 |
|
49 |
+
# Image Rotation Function
|
50 |
+
def rotate_image(image, rotation_angle):
|
51 |
+
"""Rotate the uploaded image by the specified angle."""
|
52 |
+
img = Image.open(image)
|
53 |
+
return img.rotate(rotation_angle, expand=True)
|
54 |
+
|
55 |
+
# Display Input Images and Add Rotation Option
|
56 |
if subject_image:
|
57 |
st.image(subject_image, caption="Uploaded Subject Image", use_column_width=True)
|
58 |
+
|
59 |
if pant_front_image:
|
60 |
+
rotation_angle = st.sidebar.slider("Rotate Front Denim Image", -180, 180, 0)
|
61 |
+
rotated_front_image = rotate_image(pant_front_image, rotation_angle)
|
62 |
+
st.image(rotated_front_image, caption="Rotated Front Denim Image", use_column_width=True)
|
63 |
+
pant_front_image = BytesIO()
|
64 |
+
rotated_front_image.save(pant_front_image, format="JPEG")
|
65 |
+
pant_front_image.seek(0)
|
66 |
|
67 |
# Generate Try-On Output
|
68 |
if st.button("Generate Virtual Try-On"):
|
69 |
+
if subject_image and pant_front_image:
|
70 |
with st.spinner("Processing... Please wait."):
|
71 |
try:
|
72 |
+
composite_image = virtual_tryon(subject_image, pant_front_image)
|
73 |
if composite_image:
|
74 |
st.image(composite_image, caption="Virtual Try-On Result", use_column_width=True)
|
75 |
except Exception as e:
|