Spaces:
Running
Running
Update app.py
#163
by
ali2780
- opened
app.py
CHANGED
|
@@ -9,46 +9,54 @@ import asyncio
|
|
| 9 |
import requests
|
| 10 |
import base64
|
| 11 |
import gradio as gr
|
| 12 |
-
# from IPython import embed
|
| 13 |
|
|
|
|
| 14 |
machine_number = 0
|
| 15 |
-
model = os.path.join(os.path.dirname(__file__), "models
|
| 16 |
|
|
|
|
| 17 |
MODEL_MAP = {
|
| 18 |
-
"AI Model Rouyan_0":
|
| 19 |
-
"AI Model Rouyan_1":
|
| 20 |
-
"AI Model Rouyan_2":
|
| 21 |
-
"AI Model Eva_0":
|
| 22 |
-
"AI Model Eva_1":
|
| 23 |
-
"AI Model Simon_0":
|
| 24 |
-
"AI Model Simon_1":
|
| 25 |
-
"AI Model Xuanxuan_0":
|
| 26 |
-
"AI Model Xuanxuan_1":
|
| 27 |
-
"AI Model Xuanxuan_2":
|
| 28 |
-
"AI Model Yaqi_0":
|
| 29 |
-
"AI Model Yaqi_1":
|
| 30 |
-
"AI Model Yaqi_2":
|
| 31 |
-
"AI Model Yaqi_3":
|
| 32 |
-
"AI Model Yifeng_0":
|
| 33 |
-
"AI Model Yifeng_1":
|
| 34 |
-
"AI Model Yifeng_2":
|
| 35 |
-
"AI Model Yifeng_3":
|
| 36 |
}
|
| 37 |
|
|
|
|
| 38 |
def add_waterprint(img):
|
| 39 |
-
|
| 40 |
h, w, _ = img.shape
|
| 41 |
img = cv2.putText(img, 'Powered by OutfitAnyone', (int(0.3*w), h-20), cv2.FONT_HERSHEY_PLAIN, 2, (128, 128, 128), 2, cv2.LINE_AA)
|
| 42 |
-
|
| 43 |
return img
|
| 44 |
|
| 45 |
-
|
| 46 |
def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
|
|
|
| 52 |
encoded_garment1 = cv2.imencode('.jpg', garment1)[1].tobytes()
|
| 53 |
encoded_garment1 = base64.b64encode(encoded_garment1).decode('utf-8')
|
| 54 |
|
|
@@ -58,134 +66,102 @@ def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
|
| 58 |
else:
|
| 59 |
encoded_garment2 = ''
|
| 60 |
|
| 61 |
-
|
|
|
|
| 62 |
headers = {'Content-Type': 'application/json'}
|
| 63 |
seed = random.randint(0, 1222222222)
|
|
|
|
|
|
|
| 64 |
data = {
|
| 65 |
"garment1": encoded_garment1,
|
| 66 |
"garment2": encoded_garment2,
|
| 67 |
"model_name": model_name,
|
| 68 |
"seed": seed
|
| 69 |
}
|
|
|
|
|
|
|
| 70 |
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 71 |
print("response code", response.status_code)
|
|
|
|
| 72 |
if response.status_code == 200:
|
| 73 |
result = response.json()
|
| 74 |
result = base64.b64decode(result['images'][0])
|
| 75 |
result_np = np.frombuffer(result, np.uint8)
|
| 76 |
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 77 |
else:
|
| 78 |
-
print('
|
| 79 |
|
| 80 |
-
|
| 81 |
final_img = add_waterprint(result_img)
|
| 82 |
|
| 83 |
return final_img
|
| 84 |
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
#
|
| 89 |
gr.HTML(
|
| 90 |
"""
|
| 91 |
-
<div style="
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
<div>
|
| 95 |
-
<h1 >Outfit Anyone: Ultra-high quality virtual try-on for Any Clothing and Any Person</h1>
|
| 96 |
-
<h4 >v0.9</h4>
|
| 97 |
-
<h5 style="margin: 0;">If you like our project, please give us a star on Github to stay updated with the latest developments.</h5>
|
| 98 |
-
<div style="display: flex; justify-content: center; align-items: center; text-align: center;>
|
| 99 |
-
<a href="https://arxiv.org/abs/2407.16224"><img src="https://img.shields.io/badge/Arxiv-2407.16224-red"></a>
|
| 100 |
-
<a href='https://humanaigc.github.io/outfit-anyone/'><img src='https://img.shields.io/badge/Project_Page-OutfitAnyone-green' alt='Project Page'></a>
|
| 101 |
-
<a href='https://github.com/HumanAIGC/OutfitAnyone'><img src='https://img.shields.io/badge/Github-Repo-blue'></a>
|
| 102 |
-
</div>
|
| 103 |
</div>
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column():
|
| 108 |
-
|
|
|
|
| 109 |
example = gr.Examples(inputs=init_image,
|
| 110 |
examples_per_page=4,
|
| 111 |
-
examples=[os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Rouyan_0'))
|
| 112 |
-
|
| 113 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Eva_0')),
|
| 114 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Simon_1')),
|
| 115 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Eva_1')),
|
| 116 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Simon_0')),
|
| 117 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Xuanxuan_0')),
|
| 118 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Xuanxuan_2')),
|
| 119 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Yaqi_1')),
|
| 120 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Yifeng_0')),
|
| 121 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Yifeng_3')),
|
| 122 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Rouyan_1')),
|
| 123 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Yifeng_2')),
|
| 124 |
-
os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Yaqi_0')),
|
| 125 |
-
])
|
| 126 |
with gr.Column():
|
| 127 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
"""
|
| 129 |
-
|
| 130 |
-
<div>
|
| 131 |
-
<h3>Models are fixed and cannot be uploaded or modified; we only support users uploading their own garments.</h3>
|
| 132 |
-
<h4 style="margin: 0;">For a one-piece dress or coat, you only need to upload the image to the 'top garment' section and leave the 'lower garment' section empty.</h4>
|
| 133 |
-
</div>
|
| 134 |
-
</div>
|
| 135 |
-
""")
|
| 136 |
with gr.Row():
|
| 137 |
-
garment_top = gr.Image(sources='upload', type="numpy", label="
|
| 138 |
example_top = gr.Examples(inputs=garment_top,
|
| 139 |
examples_per_page=5,
|
| 140 |
-
examples=[os.path.join(os.path.dirname(__file__), "garments
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
os.path.join(os.path.dirname(__file__), "garments/dress1.png"),
|
| 144 |
-
os.path.join(os.path.dirname(__file__), "garments/dress2.png"),
|
| 145 |
-
])
|
| 146 |
-
garment_down = gr.Image(sources='upload', type="numpy", label="lower garment")
|
| 147 |
example_down = gr.Examples(inputs=garment_down,
|
| 148 |
examples_per_page=5,
|
| 149 |
-
examples=[os.path.join(os.path.dirname(__file__), "garments
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
os.path.join(os.path.dirname(__file__), "garments/bottom5.png"),
|
| 154 |
-
])
|
| 155 |
-
|
| 156 |
-
run_button = gr.Button(value="Run")
|
| 157 |
with gr.Column():
|
| 158 |
-
gallery = gr.Image()
|
| 159 |
|
| 160 |
run_button.click(fn=get_tryon_result,
|
| 161 |
-
inputs=[
|
| 162 |
-
|
| 163 |
-
garment_top,
|
| 164 |
-
garment_down,
|
| 165 |
-
],
|
| 166 |
-
outputs=[gallery],
|
| 167 |
concurrency_limit=2)
|
| 168 |
-
|
| 169 |
|
| 170 |
-
#
|
| 171 |
-
gr.Markdown("##
|
| 172 |
with gr.Row():
|
| 173 |
-
reference_image1
|
| 174 |
-
reference_image2
|
| 175 |
-
reference_image3
|
|
|
|
| 176 |
gr.Examples(
|
| 177 |
-
examples=[
|
| 178 |
-
["examples/basemodel.png", "examples/garment1.png", "examples/result1.png"],
|
| 179 |
-
["examples/basemodel.png", "examples/garment2.png", "examples/result2.png"],
|
| 180 |
-
["examples/basemodel.png", "examples/garment3.png", "examples/result3.png"],
|
| 181 |
-
],
|
| 182 |
inputs=[reference_image1, reference_image2, reference_image3],
|
| 183 |
-
label=
|
| 184 |
)
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
ip = requests.get('http://ifconfig.me/ip', timeout=1).text.strip()
|
| 188 |
-
print("
|
| 189 |
demo.queue(max_size=10)
|
| 190 |
demo.launch()
|
| 191 |
-
|
|
|
|
| 9 |
import requests
|
| 10 |
import base64
|
| 11 |
import gradio as gr
|
|
|
|
| 12 |
|
| 13 |
+
# Set the machine number and model path
|
| 14 |
machine_number = 0
|
| 15 |
+
model = os.path.join(os.path.dirname(__file__), "models", "eva", "Eva_0.png")
|
| 16 |
|
| 17 |
+
# Define a mapping of model names to file paths
|
| 18 |
MODEL_MAP = {
|
| 19 |
+
"AI Model Rouyan_0": os.path.join("models", "rouyan_new", "rouyan_new\\Rouyan_0.png"),
|
| 20 |
+
"AI Model Rouyan_1": os.path.join("models", "rouyan_new", "rouyan_new\\Rouyan_1.png"),
|
| 21 |
+
"AI Model Rouyan_2": os.path.join("models", "rouyan_new", "rouyan_new\\Rouyan_2.png"),
|
| 22 |
+
"AI Model Eva_0": os.path.join("models", "eva", "Eva_0.png"),
|
| 23 |
+
"AI Model Eva_1": os.path.join("models", "eva", "Eva_1.png"),
|
| 24 |
+
"AI Model Simon_0": os.path.join("models", "simon_online", "Simon_0.png"),
|
| 25 |
+
"AI Model Simon_1": os.path.join("models", "simon_online", "Simon_1.png"),
|
| 26 |
+
"AI Model Xuanxuan_0": os.path.join("models", "xiaoxuan_online", "Xuanxuan_0.png"),
|
| 27 |
+
"AI Model Xuanxuan_1": os.path.join("models", "xiaoxuan_online", "Xuanxuan_1.png"),
|
| 28 |
+
"AI Model Xuanxuan_2": os.path.join("models", "xiaoxuan_online", "Xuanxuan_2.png"),
|
| 29 |
+
"AI Model Yaqi_0": os.path.join("models", "yaqi", "Yaqi_0.png"),
|
| 30 |
+
"AI Model Yaqi_1": os.path.join("models", "yaqi", "Yaqi_1.png"),
|
| 31 |
+
"AI Model Yaqi_2": os.path.join("models", "yaqi", "Yaqi_2.png"),
|
| 32 |
+
"AI Model Yaqi_3": os.path.join("models", "yaqi", "Yaqi_3.png"),
|
| 33 |
+
"AI Model Yifeng_0": os.path.join("models", "yifeng_online", "Yifeng_0.png"),
|
| 34 |
+
"AI Model Yifeng_1": os.path.join("models", "yifeng_online", "Yifeng_1.png"),
|
| 35 |
+
"AI Model Yifeng_2": os.path.join("models", "yifeng_online", "Yifeng_2.png"),
|
| 36 |
+
"AI Model Yifeng_3": os.path.join("models", "yifeng_online", "Yifeng_3.png"),
|
| 37 |
}
|
| 38 |
|
| 39 |
+
# Function to add watermark text to image
|
| 40 |
def add_waterprint(img):
|
|
|
|
| 41 |
h, w, _ = img.shape
|
| 42 |
img = cv2.putText(img, 'Powered by OutfitAnyone', (int(0.3*w), h-20), cv2.FONT_HERSHEY_PLAIN, 2, (128, 128, 128), 2, cv2.LINE_AA)
|
|
|
|
| 43 |
return img
|
| 44 |
|
| 45 |
+
# Function to process try-on results
|
| 46 |
def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
| 47 |
+
if isinstance(model_name, np.ndarray):
|
| 48 |
+
model_name = model_name[0]
|
| 49 |
+
|
| 50 |
+
model_name = "AI Model " + model_name.split("\\")[-1].split(".")[0] # Handle Windows path
|
| 51 |
+
print(type(model_name))
|
| 52 |
+
|
| 53 |
|
| 54 |
+
# Directly load the model image from the disk, no need for Gradio file upload
|
| 55 |
+
model_image = cv2.imread(MODEL_MAP.get(model_name)) # Load model image from disk
|
| 56 |
+
if model_image is None:
|
| 57 |
+
raise ValueError(f"Model image {model_name} could not be loaded.")
|
| 58 |
|
| 59 |
+
# Encode garments as base64
|
| 60 |
encoded_garment1 = cv2.imencode('.jpg', garment1)[1].tobytes()
|
| 61 |
encoded_garment1 = base64.b64encode(encoded_garment1).decode('utf-8')
|
| 62 |
|
|
|
|
| 66 |
else:
|
| 67 |
encoded_garment2 = ''
|
| 68 |
|
| 69 |
+
# Get the IP address from environment variable or default to localhost
|
| 70 |
+
url = os.environ.get('OA_IP_ADDRESS', 'http://localhost:5000')
|
| 71 |
headers = {'Content-Type': 'application/json'}
|
| 72 |
seed = random.randint(0, 1222222222)
|
| 73 |
+
|
| 74 |
+
# Prepare data for POST request
|
| 75 |
data = {
|
| 76 |
"garment1": encoded_garment1,
|
| 77 |
"garment2": encoded_garment2,
|
| 78 |
"model_name": model_name,
|
| 79 |
"seed": seed
|
| 80 |
}
|
| 81 |
+
|
| 82 |
+
# Send POST request to server
|
| 83 |
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 84 |
print("response code", response.status_code)
|
| 85 |
+
|
| 86 |
if response.status_code == 200:
|
| 87 |
result = response.json()
|
| 88 |
result = base64.b64decode(result['images'][0])
|
| 89 |
result_np = np.frombuffer(result, np.uint8)
|
| 90 |
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 91 |
else:
|
| 92 |
+
print('Server error!')
|
| 93 |
|
|
|
|
| 94 |
final_img = add_waterprint(result_img)
|
| 95 |
|
| 96 |
return final_img
|
| 97 |
|
| 98 |
|
| 99 |
+
with gr.Blocks(css=".output-image, .input-image, .image-preview {height: 400px !important}") as demo:
|
| 100 |
+
|
| 101 |
+
# Header Section
|
| 102 |
gr.HTML(
|
| 103 |
"""
|
| 104 |
+
<div style="text-align: center; padding: 20px;">
|
| 105 |
+
<h1 style="font-size: 2.5rem; color: #2c3e50;">Outfit Anyone</h1>
|
| 106 |
+
<h2 style="color: #34495e;">Ultra-high quality virtual try-on for any clothing and any person</h2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
</div>
|
| 108 |
+
"""
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# UI Layout for Image Inputs and Text Description
|
| 112 |
with gr.Row():
|
| 113 |
with gr.Column():
|
| 114 |
+
gr.Markdown("### Upload Your Model Image")
|
| 115 |
+
init_image = gr.Image(sources='upload', type="numpy", label="Select a Model Image", value=None)
|
| 116 |
example = gr.Examples(inputs=init_image,
|
| 117 |
examples_per_page=4,
|
| 118 |
+
examples=[os.path.join(os.path.dirname(__file__), MODEL_MAP.get('AI Model Rouyan_0'))])
|
| 119 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
with gr.Column():
|
| 121 |
+
gr.Markdown(
|
| 122 |
+
"""
|
| 123 |
+
<h3 style="color: #2c3e50;">Instructions</h3>
|
| 124 |
+
<p style="font-size: 1.1rem; color: #7f8c8d;">Please upload your model image and garment images (top and bottom).
|
| 125 |
+
The models are pre-loaded and cannot be modified.
|
| 126 |
+
For a dress or coat, you only need to upload the image for the 'Top Garment' section and leave the 'Bottom Garment' section empty.</p>
|
| 127 |
"""
|
| 128 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
with gr.Row():
|
| 130 |
+
garment_top = gr.Image(sources='upload', type="numpy", label="Top Garment")
|
| 131 |
example_top = gr.Examples(inputs=garment_top,
|
| 132 |
examples_per_page=5,
|
| 133 |
+
examples=[os.path.join(os.path.dirname(__file__), "garments", "top222.JPG")])
|
| 134 |
+
|
| 135 |
+
garment_down = gr.Image(sources='upload', type="numpy", label="Bottom Garment")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
example_down = gr.Examples(inputs=garment_down,
|
| 137 |
examples_per_page=5,
|
| 138 |
+
examples=[os.path.join(os.path.dirname(__file__), "garments", "bottom1.png")])
|
| 139 |
+
|
| 140 |
+
run_button = gr.Button(value="Run Try-On")
|
| 141 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
with gr.Column():
|
| 143 |
+
gallery = gr.Image(label="Try-On Result")
|
| 144 |
|
| 145 |
run_button.click(fn=get_tryon_result,
|
| 146 |
+
inputs=[init_image, garment_top, garment_down],
|
| 147 |
+
outputs=[gallery],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
concurrency_limit=2)
|
|
|
|
| 149 |
|
| 150 |
+
# Example Section
|
| 151 |
+
gr.Markdown("## Example Try-On Results")
|
| 152 |
with gr.Row():
|
| 153 |
+
reference_image1 = gr.Image(label="Model Example", scale=1, value="examples\\examples_basemodel.png")
|
| 154 |
+
reference_image2 = gr.Image(label="Garment Example", scale=1, value="examples\\examples_garment1.jpg")
|
| 155 |
+
reference_image3 = gr.Image(label="Result Example", scale=1, value="examples\\examples_result1.png")
|
| 156 |
+
|
| 157 |
gr.Examples(
|
| 158 |
+
examples=[["examples\\examples_basemodel.png", "examples\\examples_garment1.png", "examples\\examples_result1.png"]],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
inputs=[reference_image1, reference_image2, reference_image3],
|
| 160 |
+
label="Check out our example outfits!",
|
| 161 |
)
|
| 162 |
|
| 163 |
if __name__ == "__main__":
|
| 164 |
ip = requests.get('http://ifconfig.me/ip', timeout=1).text.strip()
|
| 165 |
+
print("IP address", ip)
|
| 166 |
demo.queue(max_size=10)
|
| 167 |
demo.launch()
|
|
|