Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,11 +20,40 @@ MODEL_MAP = {
|
|
| 20 |
"AI Model male_model2": 'models/male_model2.png',
|
| 21 |
}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def add_waterprint(img):
|
| 24 |
h, w, _ = img.shape
|
| 25 |
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)
|
| 26 |
return img
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
| 30 |
# model_name = "AI Model " + model_name.split("\\")[-1].split(".")[0] # windows
|
|
@@ -40,7 +69,17 @@ def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
|
| 40 |
else:
|
| 41 |
encoded_garment2 = ''
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
headers = {'Content-Type': 'application/json'}
|
| 45 |
seed = random.randint(0, 1222222222)
|
| 46 |
data = {
|
|
@@ -49,15 +88,26 @@ def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
|
| 49 |
"model_name": model_name,
|
| 50 |
"seed": seed
|
| 51 |
}
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
final_img = add_waterprint(result_img)
|
| 63 |
return final_img
|
|
@@ -95,6 +145,26 @@ with gr.Blocks(css = ".output-image, .input-image, .image-preview {height: 400px
|
|
| 95 |
</div>
|
| 96 |
</div>
|
| 97 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
with gr.Row():
|
| 99 |
garment_top = gr.Image(sources='upload', type="numpy", label="top garment")
|
| 100 |
garment_down = gr.Image(sources='upload', type="numpy", label="lower garment")
|
|
@@ -104,14 +174,49 @@ with gr.Blocks(css = ".output-image, .input-image, .image-preview {height: 400px
|
|
| 104 |
with gr.Column():
|
| 105 |
gallery = gr.Image()
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
ip = requests.get('http://ifconfig.me/ip', timeout=1).text.strip()
|
|
|
|
| 20 |
"AI Model male_model2": 'models/male_model2.png',
|
| 21 |
}
|
| 22 |
|
| 23 |
+
# Sample clothing items from your clothes folder
|
| 24 |
+
SAMPLE_CLOTHES = {
|
| 25 |
+
"top": [
|
| 26 |
+
"clothes/shirt1.jpg",
|
| 27 |
+
"clothes/shirt2.jpg",
|
| 28 |
+
"clothes/dress1.jpg",
|
| 29 |
+
"clothes/jacket1.jpg"
|
| 30 |
+
],
|
| 31 |
+
"bottom": [
|
| 32 |
+
"clothes/pants1.jpg",
|
| 33 |
+
"clothes/skirt1.jpg",
|
| 34 |
+
"clothes/jeans1.jpg",
|
| 35 |
+
"clothes/shorts1.jpg"
|
| 36 |
+
]
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
def add_waterprint(img):
|
| 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 |
return img
|
| 43 |
|
| 44 |
+
def load_sample_image(image_path):
|
| 45 |
+
"""Load a sample clothing image and return it as numpy array"""
|
| 46 |
+
try:
|
| 47 |
+
if os.path.exists(image_path):
|
| 48 |
+
img = cv2.imread(image_path)
|
| 49 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 50 |
+
return img
|
| 51 |
+
else:
|
| 52 |
+
# Return a placeholder if image doesn't exist
|
| 53 |
+
return np.zeros((400, 400, 3), dtype=np.uint8)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error loading image {image_path}: {e}")
|
| 56 |
+
return np.zeros((400, 400, 3), dtype=np.uint8)
|
| 57 |
|
| 58 |
def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
| 59 |
# model_name = "AI Model " + model_name.split("\\")[-1].split(".")[0] # windows
|
|
|
|
| 69 |
else:
|
| 70 |
encoded_garment2 = ''
|
| 71 |
|
| 72 |
+
# Fix for the missing environment variable
|
| 73 |
+
try:
|
| 74 |
+
url = os.environ['OA_IP_ADDRESS']
|
| 75 |
+
except KeyError:
|
| 76 |
+
print("Error: OA_IP_ADDRESS environment variable not set!")
|
| 77 |
+
# Return a dummy image or handle the error appropriately
|
| 78 |
+
dummy_img = np.zeros((512, 512, 3), dtype=np.uint8)
|
| 79 |
+
dummy_img = cv2.putText(dummy_img, 'Error: OA_IP_ADDRESS not set', (50, 256),
|
| 80 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 81 |
+
return add_waterprint(dummy_img)
|
| 82 |
+
|
| 83 |
headers = {'Content-Type': 'application/json'}
|
| 84 |
seed = random.randint(0, 1222222222)
|
| 85 |
data = {
|
|
|
|
| 88 |
"model_name": model_name,
|
| 89 |
"seed": seed
|
| 90 |
}
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 94 |
+
print("response code", response.status_code)
|
| 95 |
+
if response.status_code == 200:
|
| 96 |
+
result = response.json()
|
| 97 |
+
result = base64.b64decode(result['images'][0])
|
| 98 |
+
result_np = np.frombuffer(result, np.uint8)
|
| 99 |
+
result_img = cv2.imdecode(result_np, cv2.IMREAD_UNCHANGED)
|
| 100 |
+
else:
|
| 101 |
+
print('server error!')
|
| 102 |
+
# Return error image
|
| 103 |
+
result_img = np.zeros((512, 512, 3), dtype=np.uint8)
|
| 104 |
+
result_img = cv2.putText(result_img, f'Server Error: {response.status_code}',
|
| 105 |
+
(50, 256), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f"Request error: {e}")
|
| 108 |
+
result_img = np.zeros((512, 512, 3), dtype=np.uint8)
|
| 109 |
+
result_img = cv2.putText(result_img, 'Connection Error', (50, 256),
|
| 110 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
|
| 111 |
|
| 112 |
final_img = add_waterprint(result_img)
|
| 113 |
return final_img
|
|
|
|
| 145 |
</div>
|
| 146 |
</div>
|
| 147 |
""")
|
| 148 |
+
|
| 149 |
+
# Sample clothing buttons section
|
| 150 |
+
gr.HTML("<h3>Quick Select Sample Clothes:</h3>")
|
| 151 |
+
|
| 152 |
+
with gr.Row():
|
| 153 |
+
gr.HTML("<h4>Top Garments:</h4>")
|
| 154 |
+
with gr.Row():
|
| 155 |
+
top_btn1 = gr.Button("Shirt 1", variant="secondary")
|
| 156 |
+
top_btn2 = gr.Button("Shirt 2", variant="secondary")
|
| 157 |
+
top_btn3 = gr.Button("Dress", variant="secondary")
|
| 158 |
+
top_btn4 = gr.Button("Jacket", variant="secondary")
|
| 159 |
+
|
| 160 |
+
with gr.Row():
|
| 161 |
+
gr.HTML("<h4>Bottom Garments:</h4>")
|
| 162 |
+
with gr.Row():
|
| 163 |
+
bottom_btn1 = gr.Button("Pants", variant="secondary")
|
| 164 |
+
bottom_btn2 = gr.Button("Skirt", variant="secondary")
|
| 165 |
+
bottom_btn3 = gr.Button("Jeans", variant="secondary")
|
| 166 |
+
bottom_btn4 = gr.Button("Shorts", variant="secondary")
|
| 167 |
+
|
| 168 |
with gr.Row():
|
| 169 |
garment_top = gr.Image(sources='upload', type="numpy", label="top garment")
|
| 170 |
garment_down = gr.Image(sources='upload', type="numpy", label="lower garment")
|
|
|
|
| 174 |
with gr.Column():
|
| 175 |
gallery = gr.Image()
|
| 176 |
|
| 177 |
+
# Connect buttons to load sample images
|
| 178 |
+
top_btn1.click(
|
| 179 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["top"][0]),
|
| 180 |
+
outputs=[garment_top]
|
| 181 |
+
)
|
| 182 |
+
top_btn2.click(
|
| 183 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["top"][1]),
|
| 184 |
+
outputs=[garment_top]
|
| 185 |
+
)
|
| 186 |
+
top_btn3.click(
|
| 187 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["top"][2]),
|
| 188 |
+
outputs=[garment_top]
|
| 189 |
+
)
|
| 190 |
+
top_btn4.click(
|
| 191 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["top"][3]),
|
| 192 |
+
outputs=[garment_top]
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
bottom_btn1.click(
|
| 196 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["bottom"][0]),
|
| 197 |
+
outputs=[garment_down]
|
| 198 |
+
)
|
| 199 |
+
bottom_btn2.click(
|
| 200 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["bottom"][1]),
|
| 201 |
+
outputs=[garment_down]
|
| 202 |
+
)
|
| 203 |
+
bottom_btn3.click(
|
| 204 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["bottom"][2]),
|
| 205 |
+
outputs=[garment_down]
|
| 206 |
+
)
|
| 207 |
+
bottom_btn4.click(
|
| 208 |
+
lambda: load_sample_image(SAMPLE_CLOTHES["bottom"][3]),
|
| 209 |
+
outputs=[garment_down]
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
run_button.click(fn=get_tryon_result,
|
| 213 |
+
inputs=[
|
| 214 |
+
init_image,
|
| 215 |
+
garment_top,
|
| 216 |
+
garment_down,
|
| 217 |
+
],
|
| 218 |
+
outputs=[gallery],
|
| 219 |
+
concurrency_limit=2)
|
| 220 |
|
| 221 |
if __name__ == "__main__":
|
| 222 |
ip = requests.get('http://ifconfig.me/ip', timeout=1).text.strip()
|