Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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']
|
| 38 |
+
status = result['status']
|
| 39 |
+
if status == "success":
|
| 40 |
+
uuid = result['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']
|
| 55 |
+
status = result['status']
|
| 56 |
+
if status == "success":
|
| 57 |
+
result = base64.b64decode(result['result'])
|
| 58 |
+
result_np = np.frombuffer(result, np.uint8)
|
| 59 |
+
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 60 |
+
result_img = cv2.cvtColor(result_img, cv2.COLOR_RGB2BGR)
|
| 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 |
+
|
| 80 |
+
return result_img, seed, info
|
| 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 |
+
st.title("Virtual Try-On")
|
| 88 |
+
st.markdown("""
|
| 89 |
+
**Step 1:** Upload a person image ⬇️
|
| 90 |
+
**Step 2:** Upload a garment image ⬇️
|
| 91 |
+
**Step 3:** Press “Run” to get try-on results
|
| 92 |
+
""")
|
| 93 |
+
|
| 94 |
+
col1, col2 = st.columns(2)
|
| 95 |
+
|
| 96 |
+
with col1:
|
| 97 |
+
person_img = st.file_uploader("Person Image", type=["jpg", "jpeg", "png"])
|
| 98 |
+
|
| 99 |
+
with col2:
|
| 100 |
+
garment_img = st.file_uploader("Garment Image", type=["jpg", "jpeg", "png"])
|
| 101 |
+
|
| 102 |
+
if person_img and garment_img:
|
| 103 |
+
person_img = np.array(bytearray(person_img.read()), dtype=np.uint8)
|
| 104 |
+
garment_img = np.array(bytearray(garment_img.read()), dtype=np.uint8)
|
| 105 |
+
person_img = cv2.imdecode(person_img, cv2.IMREAD_COLOR)
|
| 106 |
+
garment_img = cv2.imdecode(garment_img, cv2.IMREAD_COLOR)
|
| 107 |
+
|
| 108 |
+
st.sidebar.header("Options")
|
| 109 |
+
seed = st.sidebar.slider("Seed", 0, MAX_SEED, 0)
|
| 110 |
+
randomize_seed = st.sidebar.checkbox("Random seed", value=True)
|
| 111 |
+
|
| 112 |
+
if st.sidebar.button("Run"):
|
| 113 |
+
result_img, seed_used, result_info = tryon(person_img, garment_img, seed, randomize_seed)
|
| 114 |
+
if result_info == "Success":
|
| 115 |
+
st.image(result_img, caption="Result", channels="BGR")
|
| 116 |
+
st.sidebar.text(f"Seed used: {seed_used}")
|
| 117 |
+
else:
|
| 118 |
+
st.sidebar.error(result_info)
|
| 119 |
+
else:
|
| 120 |
+
st.sidebar.warning("Please upload both images to proceed.")
|