Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import base64
|
4 |
+
import json
|
5 |
+
import random
|
6 |
+
import cv2
|
7 |
+
from PIL import Image
|
8 |
+
import requests
|
9 |
+
import gradio as gr
|
10 |
+
|
11 |
+
# In-memory user storage for demonstration
|
12 |
+
users_db = {}
|
13 |
+
|
14 |
+
# Ensure the model path is correct
|
15 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
16 |
+
default_model = os.path.join(current_dir, "models/eva/Eva_0.png")
|
17 |
+
|
18 |
+
# Map of AI models with their corresponding file paths
|
19 |
+
MODEL_MAP = {
|
20 |
+
"AI Model Rouyan_0": os.path.join(current_dir, 'models/rouyan_new/Rouyan_0.png'),
|
21 |
+
"AI Model Eva_0": os.path.join(current_dir, 'models/eva/Eva_0.png'),
|
22 |
+
"AI Model Eva_1": os.path.join(current_dir, 'models/eva/Eva_1.png'),
|
23 |
+
"AI Model Simon_0": os.path.join(current_dir, 'models/simon_online/Simon_0.png'),
|
24 |
+
"AI Model Simon_1": os.path.join(current_dir, 'models/simon_online/Simon_1.png'),
|
25 |
+
"AI Model Xuanxuan_0": os.path.join(current_dir, 'models/xiaoxuan_online/Xuanxuan_0.png'),
|
26 |
+
"AI Model Xuanxuan_1": os.path.join(current_dir, 'models/xiaoxuan_online/Xuanxuan_1.png'),
|
27 |
+
"AI Model Xuanxuan_2": os.path.join(current_dir, 'models/xiaoxuan_online/Xuanxuan_2.png'),
|
28 |
+
"AI Model Yaqi_0": os.path.join(current_dir, 'models/yaqi/Yaqi_0.png'),
|
29 |
+
"AI Model Yaqi_1": os.path.join(current_dir, 'models/yaqi/Yaqi_1.png'),
|
30 |
+
"AI Model Yaqi_2": os.path.join(current_dir, 'models/yaqi/Yaqi_2.png'),
|
31 |
+
"AI Model Yaqi_3": os.path.join(current_dir, 'models/yaqi/Yaqi_3.png'),
|
32 |
+
"AI Model Yifeng_0": os.path.join(current_dir, 'models/yifeng_online/Yifeng_0.png'),
|
33 |
+
"AI Model Yifeng_1": os.path.join(current_dir, 'models/yifeng_online/Yifeng_1.png'),
|
34 |
+
"AI Model Yifeng_2": os.path.join(current_dir, 'models/yifeng_online/Yifeng_2.png'),
|
35 |
+
"AI Model Yifeng_3": os.path.join(current_dir, 'models/yifeng_online/Yifeng_3.png'),
|
36 |
+
}
|
37 |
+
|
38 |
+
# Registration function
|
39 |
+
def register(username, password, confirm_password):
|
40 |
+
if username in users_db:
|
41 |
+
return "Username already exists", gr.update(visible=True), gr.update(visible=False)
|
42 |
+
if password != confirm_password:
|
43 |
+
return "Passwords do not match", gr.update(visible=True), gr.update(visible=False)
|
44 |
+
users_db[username] = password
|
45 |
+
return "Registration successful! Please login.", gr.update(visible=True), gr.update(visible=False)
|
46 |
+
|
47 |
+
# Login function
|
48 |
+
def login(username, password):
|
49 |
+
if users_db.get(username) == password:
|
50 |
+
return gr.update(visible=False), gr.update(visible=True)
|
51 |
+
else:
|
52 |
+
return gr.update(visible=True), gr.update(visible=False)
|
53 |
+
|
54 |
+
# Watermark function
|
55 |
+
def add_watermark(image):
|
56 |
+
height, width, _ = image.shape
|
57 |
+
cv2.putText(image, 'Powered by OutfitAnyone', (int(0.3 * width), height - 20),
|
58 |
+
cv2.FONT_HERSHEY_PLAIN, 2, (128, 128, 128), 2, cv2.LINE_AA)
|
59 |
+
return image
|
60 |
+
|
61 |
+
# Try-on result processing
|
62 |
+
def get_tryon_result(model_name, top_garment, bottom_garment=None):
|
63 |
+
model_key = "AI Model " + model_name.split("/")[-1].split(".")[0]
|
64 |
+
print(f"Selected model: {model_key}")
|
65 |
+
|
66 |
+
encoded_top = base64.b64encode(cv2.imencode('.jpg', top_garment)[1].tobytes()).decode('utf-8')
|
67 |
+
|
68 |
+
# Check if bottom_garment is not None and is not an empty array
|
69 |
+
encoded_bottom = ""
|
70 |
+
if bottom_garment is not None and bottom_garment.size > 0:
|
71 |
+
encoded_bottom = base64.b64encode(cv2.imencode('.jpg', bottom_garment)[1].tobytes()).decode('utf-8')
|
72 |
+
|
73 |
+
server_url = os.environ.get('OA_IP_ADDRESS', 'http://localhost:5000')
|
74 |
+
headers = {'Content-Type': 'application/json'}
|
75 |
+
payload = {
|
76 |
+
"garment1": encoded_top,
|
77 |
+
"garment2": encoded_bottom,
|
78 |
+
"model_name": model_key,
|
79 |
+
"seed": random.randint(0, 99999999)
|
80 |
+
}
|
81 |
+
|
82 |
+
try:
|
83 |
+
response = requests.post(server_url, headers=headers, data=json.dumps(payload))
|
84 |
+
|
85 |
+
if response.status_code == 200:
|
86 |
+
result = response.json()
|
87 |
+
result_img = cv2.imdecode(np.frombuffer(base64.b64decode(result['images'][0]), np.uint8), cv2.IMREAD_UNCHANGED)
|
88 |
+
final_img = add_watermark(result_img)
|
89 |
+
return final_img
|
90 |
+
else:
|
91 |
+
print(f"Error: Server responded with status code {response.status_code}")
|
92 |
+
return None
|
93 |
+
except requests.exceptions.ConnectionError as e:
|
94 |
+
print(f"ConnectionError: Could not connect to server at {server_url}. Please ensure the server is running.")
|
95 |
+
print(f"Details: {e}")
|
96 |
+
return None
|
97 |
+
except requests.exceptions.RequestException as e:
|
98 |
+
print(f"RequestException: An error occurred during the request.")
|
99 |
+
print(f"Details: {e}")
|
100 |
+
return None
|
101 |
+
|
102 |
+
# Gradio Interface
|
103 |
+
with gr.Blocks() as demo:
|
104 |
+
gr.HTML("<script>console.log('Custom JavaScript');</script>")
|
105 |
+
login_block = gr.Column(visible=True)
|
106 |
+
register_block = gr.Column(visible=False)
|
107 |
+
main_app = gr.Column(visible=False)
|
108 |
+
|
109 |
+
# Registration Page
|
110 |
+
with register_block:
|
111 |
+
gr.HTML("<h2>Register for Outfit Anyone</h2>")
|
112 |
+
username = gr.Textbox(label="Username")
|
113 |
+
password = gr.Textbox(label="Password", type="password", elem_id="reg_pass")
|
114 |
+
confirm_password = gr.Textbox(label="Confirm Password", type="password", elem_id="reg_conf_pass")
|
115 |
+
register_button = gr.Button("Register")
|
116 |
+
back_to_login_button = gr.Button("Back to Login")
|
117 |
+
register_message = gr.Textbox(label="", visible=False)
|
118 |
+
|
119 |
+
# Login Page
|
120 |
+
with login_block:
|
121 |
+
gr.HTML("<h2>Login to Outfit Anyone</h2>")
|
122 |
+
username_login = gr.Textbox(label="Username")
|
123 |
+
password_login = gr.Textbox(label="Password", type="password", elem_id="login_pass")
|
124 |
+
login_button = gr.Button("Login")
|
125 |
+
go_to_register_button = gr.Button("Register")
|
126 |
+
login_message = gr.Textbox(label="", visible=False)
|
127 |
+
|
128 |
+
# Main App Interface
|
129 |
+
with main_app:
|
130 |
+
gr.HTML("""
|
131 |
+
<div style="text-align: center;">
|
132 |
+
<h1>Outfit Anyone: Virtual Try-On</h1>
|
133 |
+
<h4>v1.0</h4>
|
134 |
+
<p>Upload your garments and choose a model to see the virtual try-on.</p>
|
135 |
+
</div>
|
136 |
+
""")
|
137 |
+
with gr.Row():
|
138 |
+
with gr.Column():
|
139 |
+
model_selector = gr.Image(sources='upload', type="filepath", label="Model", value=default_model)
|
140 |
+
example_models = gr.Examples(inputs=model_selector, examples=[MODEL_MAP['AI Model Rouyan_0'], MODEL_MAP['AI Model Eva_0']], examples_per_page=4)
|
141 |
+
with gr.Column():
|
142 |
+
gr.HTML("<h3>Select Garments for Virtual Try-On</h3>")
|
143 |
+
top_garment_input = gr.Image(sources='upload', type="numpy", label="Top Garment")
|
144 |
+
bottom_garment_input = gr.Image(sources='upload', type="numpy", label="Bottom Garment (Optional)")
|
145 |
+
generate_button = gr.Button(value="Generate Outfit")
|
146 |
+
with gr.Column():
|
147 |
+
result_display = gr.Image()
|
148 |
+
|
149 |
+
generate_button.click(fn=get_tryon_result, inputs=[model_selector, top_garment_input, bottom_garment_input], outputs=[result_display])
|
150 |
+
|
151 |
+
# Button Click Actions
|
152 |
+
register_button.click(fn=register, inputs=[username, password, confirm_password], outputs=[register_message, register_block, login_block])
|
153 |
+
back_to_login_button.click(lambda: (gr.update(visible=False), gr.update(visible=True)), None, [register_block, login_block])
|
154 |
+
go_to_register_button.click(lambda: (gr.update(visible=True), gr.update(visible=False)), None, [register_block, login_block])
|
155 |
+
login_button.click(fn=login, inputs=[username_login, password_login], outputs=[login_block, main_app])
|
156 |
+
|
157 |
+
if __name__ == "__main__":
|
158 |
+
demo.queue(max_size=10)
|
159 |
+
demo.launch(server_name="0.0.0.0", server_port=8080)
|