Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,37 @@
|
|
| 1 |
-
import
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import random
|
| 5 |
import base64
|
| 6 |
import requests
|
| 7 |
import json
|
| 8 |
import time
|
| 9 |
-
import
|
|
|
|
| 10 |
|
| 11 |
-
# Function
|
| 12 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
| 13 |
-
post_start_time = time.time()
|
| 14 |
if person_img is None or garment_img is None:
|
| 15 |
st.warning("Empty image")
|
| 16 |
return None, None, "Empty image"
|
| 17 |
if randomize_seed:
|
| 18 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 19 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 20 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 21 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 22 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
-
url = "http://" + os.getenv('tryon_url', 'localhost:5000') + "/Submit"
|
| 26 |
-
token = os.getenv('token', '')
|
| 27 |
-
cookie = os.getenv('Cookie', '')
|
| 28 |
-
referer = os.getenv('referer', '')
|
| 29 |
-
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 30 |
-
data = {
|
| 31 |
-
"clothImage": encoded_garment_img,
|
| 32 |
-
"humanImage": encoded_person_img,
|
| 33 |
-
"seed": seed
|
| 34 |
-
}
|
| 35 |
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
|
| 36 |
if response.status_code == 200:
|
| 37 |
result = response.json()['result']
|
|
@@ -41,17 +41,14 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
| 41 |
except Exception as err:
|
| 42 |
st.error(f"Post Exception Error: {err}")
|
| 43 |
return None, None, "Too many users, please try again later"
|
| 44 |
-
post_end_time = time.time()
|
| 45 |
|
| 46 |
-
get_start_time = time.time()
|
| 47 |
time.sleep(9)
|
| 48 |
Max_Retry = 12
|
| 49 |
result_img = None
|
| 50 |
info = ""
|
| 51 |
-
err_log = ""
|
| 52 |
for i in range(Max_Retry):
|
| 53 |
try:
|
| 54 |
-
url = "http://" + os.
|
| 55 |
response = requests.get(url, headers=headers, timeout=20)
|
| 56 |
if response.status_code == 200:
|
| 57 |
result = response.json()['result']
|
|
@@ -64,23 +61,19 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
| 64 |
info = "Success"
|
| 65 |
break
|
| 66 |
elif status == "error":
|
| 67 |
-
err_log = f"Status is Error"
|
| 68 |
info = "Error"
|
| 69 |
break
|
| 70 |
else:
|
| 71 |
-
err_log = "URL error, please contact the admin"
|
| 72 |
info = "URL error, please contact the admin"
|
| 73 |
break
|
| 74 |
except requests.exceptions.ReadTimeout:
|
| 75 |
-
err_log = "Http Timeout"
|
| 76 |
info = "Http Timeout, please try again later"
|
| 77 |
except Exception as err:
|
| 78 |
-
|
| 79 |
time.sleep(1)
|
| 80 |
-
|
| 81 |
if info == "":
|
| 82 |
-
|
| 83 |
-
info = "Too many users, please try again later"
|
| 84 |
if info != "Success":
|
| 85 |
st.warning("Too many users, please try again later")
|
| 86 |
|
|
@@ -88,29 +81,63 @@ def tryon(person_img, garment_img, seed, randomize_seed):
|
|
| 88 |
|
| 89 |
MAX_SEED = 999999
|
| 90 |
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
""")
|
| 96 |
-
person_img = st.file_uploader("Choose a person image", type=["jpg", "jpeg", "png"])
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
if
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
result_img, seed_used, result_info = tryon(person_img, garment_img, seed, randomize_seed)
|
| 111 |
-
if
|
| 112 |
-
st.image(result_img, caption=
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import base64
|
| 5 |
import requests
|
| 6 |
import json
|
| 7 |
import time
|
| 8 |
+
import random
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# Function to handle the try-on process
|
| 12 |
def tryon(person_img, garment_img, seed, randomize_seed):
|
|
|
|
| 13 |
if person_img is None or garment_img is None:
|
| 14 |
st.warning("Empty image")
|
| 15 |
return None, None, "Empty image"
|
| 16 |
if randomize_seed:
|
| 17 |
seed = random.randint(0, MAX_SEED)
|
| 18 |
+
|
| 19 |
encoded_person_img = cv2.imencode('.jpg', cv2.cvtColor(person_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 20 |
encoded_person_img = base64.b64encode(encoded_person_img).decode('utf-8')
|
| 21 |
encoded_garment_img = cv2.imencode('.jpg', cv2.cvtColor(garment_img, cv2.COLOR_RGB2BGR))[1].tobytes()
|
| 22 |
encoded_garment_img = base64.b64encode(encoded_garment_img).decode('utf-8')
|
| 23 |
|
| 24 |
+
url = "http://" + os.environ['tryon_url'] + "Submit"
|
| 25 |
+
token = os.environ['token']
|
| 26 |
+
cookie = os.environ['Cookie']
|
| 27 |
+
referer = os.environ['referer']
|
| 28 |
+
headers = {'Content-Type': 'application/json', 'token': token, 'Cookie': cookie, 'referer': referer}
|
| 29 |
+
data = {
|
| 30 |
+
"clothImage": encoded_garment_img,
|
| 31 |
+
"humanImage": encoded_person_img,
|
| 32 |
+
"seed": seed
|
| 33 |
+
}
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=50)
|
| 36 |
if response.status_code == 200:
|
| 37 |
result = response.json()['result']
|
|
|
|
| 41 |
except Exception as err:
|
| 42 |
st.error(f"Post Exception Error: {err}")
|
| 43 |
return None, None, "Too many users, please try again later"
|
|
|
|
| 44 |
|
|
|
|
| 45 |
time.sleep(9)
|
| 46 |
Max_Retry = 12
|
| 47 |
result_img = None
|
| 48 |
info = ""
|
|
|
|
| 49 |
for i in range(Max_Retry):
|
| 50 |
try:
|
| 51 |
+
url = "http://" + os.environ['tryon_url'] + "Query?taskId=" + uuid
|
| 52 |
response = requests.get(url, headers=headers, timeout=20)
|
| 53 |
if response.status_code == 200:
|
| 54 |
result = response.json()['result']
|
|
|
|
| 61 |
info = "Success"
|
| 62 |
break
|
| 63 |
elif status == "error":
|
|
|
|
| 64 |
info = "Error"
|
| 65 |
break
|
| 66 |
else:
|
|
|
|
| 67 |
info = "URL error, please contact the admin"
|
| 68 |
break
|
| 69 |
except requests.exceptions.ReadTimeout:
|
|
|
|
| 70 |
info = "Http Timeout, please try again later"
|
| 71 |
except Exception as err:
|
| 72 |
+
info = f"Get Exception Error: {err}"
|
| 73 |
time.sleep(1)
|
| 74 |
+
|
| 75 |
if info == "":
|
| 76 |
+
info = f"No image after {Max_Retry} retries"
|
|
|
|
| 77 |
if info != "Success":
|
| 78 |
st.warning("Too many users, please try again later")
|
| 79 |
|
|
|
|
| 81 |
|
| 82 |
MAX_SEED = 999999
|
| 83 |
|
| 84 |
+
# Set up the Streamlit app
|
| 85 |
+
st.set_page_config(page_title="Virtual Try-On", page_icon=":guardsman:", layout="wide")
|
| 86 |
|
| 87 |
+
# Title and description
|
| 88 |
+
st.title("Virtual Try-On")
|
| 89 |
+
st.markdown("""
|
| 90 |
+
**Step 1:** Upload a person image ⬇️
|
| 91 |
+
**Step 2:** Upload a garment image ⬇️
|
| 92 |
+
**Step 3:** Press “Run” to get try-on results
|
| 93 |
""")
|
|
|
|
| 94 |
|
| 95 |
+
# Columns for uploading images
|
| 96 |
+
col1, col2 = st.columns(2)
|
| 97 |
+
|
| 98 |
+
with col1:
|
| 99 |
+
st.image("assets/upload_person.png", caption="Upload your person image here.", use_column_width=True)
|
| 100 |
+
person_img = st.file_uploader("Person Image", type=["jpg", "jpeg", "png"], label_visibility="collapsed")
|
| 101 |
|
| 102 |
+
with col2:
|
| 103 |
+
st.image("assets/upload_garment.png", caption="Upload your garment image here.", use_column_width=True)
|
| 104 |
+
garment_img = st.file_uploader("Garment Image", type=["jpg", "jpeg", "png"], label_visibility="collapsed")
|
| 105 |
|
| 106 |
+
# Show options and button if images are uploaded
|
| 107 |
+
if person_img and garment_img:
|
| 108 |
+
person_img = np.array(bytearray(person_img.read()), dtype=np.uint8)
|
| 109 |
+
garment_img = np.array(bytearray(garment_img.read()), dtype=np.uint8)
|
| 110 |
+
person_img = cv2.imdecode(person_img, cv2.IMREAD_COLOR)
|
| 111 |
+
garment_img = cv2.imdecode(garment_img, cv2.IMREAD_COLOR)
|
| 112 |
+
|
| 113 |
+
st.sidebar.header("Options")
|
| 114 |
+
seed = st.sidebar.slider("Seed", 0, MAX_SEED, 0)
|
| 115 |
+
randomize_seed = st.sidebar.checkbox("Random seed", value=True)
|
| 116 |
+
|
| 117 |
+
st.sidebar.markdown("---")
|
| 118 |
+
|
| 119 |
+
# Display example images
|
| 120 |
+
st.sidebar.image("assets/seed_example.png", caption="Example of seed usage", use_column_width=True)
|
| 121 |
+
|
| 122 |
+
if st.sidebar.button("Run"):
|
| 123 |
result_img, seed_used, result_info = tryon(person_img, garment_img, seed, randomize_seed)
|
| 124 |
+
if result_info == "Success":
|
| 125 |
+
st.image(result_img, caption="Result", channels="BGR")
|
| 126 |
+
st.sidebar.text(f"Seed used: {seed_used}")
|
| 127 |
+
else:
|
| 128 |
+
st.sidebar.error(result_info)
|
| 129 |
+
else:
|
| 130 |
+
st.sidebar.warning("Please upload both images to proceed.")
|
| 131 |
+
|
| 132 |
+
# Footer or additional information
|
| 133 |
+
st.markdown("---")
|
| 134 |
+
st.markdown("Built with Streamlit & Python. [GitHub repository](#)")
|
| 135 |
+
|
| 136 |
+
# Add some styling and visual improvements
|
| 137 |
+
st.markdown("""
|
| 138 |
+
<style>
|
| 139 |
+
.css-18e3th9 {padding: 0.5rem 1rem;} /* Increase padding for the sidebar */
|
| 140 |
+
.css-1d391kg {padding: 1rem;} /* Increase padding for main content area */
|
| 141 |
+
.css-1v0mbdj {font-size: 20px;} /* Adjust font size for titles and labels */
|
| 142 |
+
</style>
|
| 143 |
+
""", unsafe_allow_html=True)
|