Spaces:
Runtime error
Runtime error
VictorFS82
commited on
Commit
β’
220d642
1
Parent(s):
f65e13b
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
3 |
+
from fastapi.responses import JSONResponse
|
4 |
+
from typing import Optional
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
import os
|
8 |
+
import random
|
9 |
+
import requests
|
10 |
+
import base64
|
11 |
+
import json
|
12 |
+
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
# Define your models and other configurations here
|
16 |
+
MODEL_MAP = {
|
17 |
+
"AI Model Rouyan_0": 'models/rouyan_new/Rouyan_0.png',
|
18 |
+
"AI Model Rouyan_1": 'models/rouyan_new/Rouyan_1.png',
|
19 |
+
"AI Model Rouyan_2": 'models/rouyan_new/Rouyan_2.png',
|
20 |
+
"AI Model Eva_0": 'models/eva/Eva_0.png',
|
21 |
+
"AI Model Eva_1": 'models/eva/Eva_1.png',
|
22 |
+
"AI Model Simon_0": 'models/simon_online/Simon_0.png',
|
23 |
+
"AI Model Simon_1": 'models/simon_online/Simon_1.png',
|
24 |
+
"AI Model Xuanxuan_0": 'models/xiaoxuan_online/Xuanxuan_0.png',
|
25 |
+
"AI Model Xuanxuan_1": 'models/xiaoxuan_online/Xuanxuan_1.png',
|
26 |
+
"AI Model Xuanxuan_2": 'models/xiaoxuan_online/Xuanxuan_2.png',
|
27 |
+
"AI Model Yaqi_0": 'models/yaqi/Yaqi_0.png',
|
28 |
+
"AI Model Yaqi_1": 'models/yaqi/Yaqi_1.png',
|
29 |
+
"AI Model Yaqi_2": 'models/yaqi/Yaqi_2.png',
|
30 |
+
"AI Model Yaqi_3": 'models/yaqi/Yaqi_3.png',
|
31 |
+
"AI Model Yifeng_0": 'models/yifeng_online/Yifeng_0.png',
|
32 |
+
"AI Model Yifeng_1": 'models/yifeng_online/Yifeng_1.png',
|
33 |
+
"AI Model Yifeng_2": 'models/yifeng_online/Yifeng_2.png',
|
34 |
+
"AI Model Yifeng_3": 'models/yifeng_online/Yifeng_3.png',
|
35 |
+
}
|
36 |
+
|
37 |
+
def add_waterprint(img):
|
38 |
+
h, w, _ = img.shape
|
39 |
+
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)
|
40 |
+
return img
|
41 |
+
|
42 |
+
def get_tryon_result(model_name, garment1, garment2, seed=1234):
|
43 |
+
model_name = "AI Model " + model_name.split("/")[-1].split(".")[0]
|
44 |
+
print(model_name)
|
45 |
+
|
46 |
+
encoded_garment1 = cv2.imencode('.jpg', garment1)[1].tobytes()
|
47 |
+
encoded_garment1 = base64.b64encode(encoded_garment1).decode('utf-8')
|
48 |
+
|
49 |
+
if garment2 is not None:
|
50 |
+
encoded_garment2 = cv2.imencode('.jpg', garment2)[1].tobytes()
|
51 |
+
encoded_garment2 = base64.b64encode(encoded_garment2).decode('utf-8')
|
52 |
+
else:
|
53 |
+
encoded_garment2 = ''
|
54 |
+
|
55 |
+
# Simulate the processing of the try-on result
|
56 |
+
# This should be replaced with actual processing logic
|
57 |
+
result_img = garment1 # Use garment1 image as a placeholder for the result
|
58 |
+
if garment2 is not None:
|
59 |
+
result_img = cv2.addWeighted(result_img, 0.5, garment2, 0.5, 0)
|
60 |
+
|
61 |
+
final_img = add_waterprint(result_img)
|
62 |
+
return final_img
|
63 |
+
|
64 |
+
@app.post("/tryon/")
|
65 |
+
async def tryon(model_name: str = Form(...), garment1: UploadFile = File(...), garment2: Optional[UploadFile] = File(None)):
|
66 |
+
garment1_img = cv2.imdecode(np.fromstring(await garment1.read(), np.uint8), cv2.IMREAD_COLOR)
|
67 |
+
garment2_img = cv2.imdecode(np.fromstring(await garment2.read(), np.uint8), cv2.IMREAD_COLOR) if garment2 else None
|
68 |
+
|
69 |
+
result_img = get_tryon_result(model_name, garment1_img, garment2_img)
|
70 |
+
|
71 |
+
if result_img is None:
|
72 |
+
return JSONResponse(status_code=500, content={"message": "Server error while processing the images"})
|
73 |
+
|
74 |
+
_, img_encoded = cv2.imencode('.png', result_img)
|
75 |
+
return JSONResponse(content={"image": base64.b64encode(img_encoded.tobytes()).decode('utf-8')})
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
import uvicorn
|
79 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|