Spaces:
Running
Running
init
Browse files- .gitattributes +3 -12
- .gitignore +2 -0
- app.py +149 -0
- requirements.txt +8 -0
- utils.py +176 -0
.gitattributes
CHANGED
@@ -33,15 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
Datas/PoseImgs/Camera_XHS_17290596474851040g2sg318tukj613q1g40jbin4o5urom8cc8mo.jpg filter=lfs diff=lfs merge=lfs -text
|
40 |
-
Datas/PoseImgs/Camera_XHS_17290620188071040g2sg318alvhqvjq7g49nod0383sqsdtdie2g.jpg filter=lfs diff=lfs merge=lfs -text
|
41 |
-
Datas/PoseImgs/Camera_XHS_17290620294581040g008318qd9jalgq1g4a0l3adj9gpk5oo3hbg.jpg filter=lfs diff=lfs merge=lfs -text
|
42 |
-
Datas/PoseImgs/Camera_XHS_17290690744921040g2sg318b2gi894490414p5j7vc25evc8cj88.jpg filter=lfs diff=lfs merge=lfs -text
|
43 |
-
Datas/PoseImgs/Camera_XHS_17292407307731040g2sg318i8mvmv3k0g5nhq88ug8bo472f45lg.jpg filter=lfs diff=lfs merge=lfs -text
|
44 |
-
Datas/PoseImgs/Camera_XHS_17292407361051040g2sg31927euo04u7043nr4n5hrue9dvm5n5g.jpg filter=lfs diff=lfs merge=lfs -text
|
45 |
-
Datas/PoseImgs/Camera_XHS_17292407854531040g00830okpafop2c0049vv6g4hguaccc6d2ng.jpg filter=lfs diff=lfs merge=lfs -text
|
46 |
-
Datas/PoseImgs/Camera_XHS_17292408016631040g008313kg6jdo000049ch9ulbcgkl0hvvp68.jpg filter=lfs diff=lfs merge=lfs -text
|
47 |
-
Datas/PoseImgs/Camera_XHS_17292408186161040g2sg30qqb14ohgskg5nqk8ljg80munke428o.jpg filter=lfs diff=lfs merge=lfs -text
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.gif filter=lfs diff=lfs merge=lfs -text
|
38 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
order.txt
|
2 |
+
push.sh
|
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import cv2
|
3 |
+
from mtcnn.mtcnn import MTCNN
|
4 |
+
from utils import *
|
5 |
+
|
6 |
+
|
7 |
+
cloth_examples = get_cloth_examples(hr=0)
|
8 |
+
cloth_hr_examples = get_cloth_examples(hr=1)
|
9 |
+
pose_examples = get_pose_examples()
|
10 |
+
tip1, tip2 = get_tips()
|
11 |
+
face_detector = MTCNN()
|
12 |
+
|
13 |
+
# Description
|
14 |
+
title = r"""
|
15 |
+
<h1 align="center">Outfit Anyway: Best customer try-on You ever See</h1>
|
16 |
+
"""
|
17 |
+
|
18 |
+
description = r"""
|
19 |
+
<b>Join discord to know more about </b> <a href='https://discord.com/invite/QgJWCtSG58' target='_blank'><b> heybeauty prebuy vton solution</b></a>.<br>
|
20 |
+
"""
|
21 |
+
|
22 |
+
|
23 |
+
def onClick(cloth_image, pose_image, high_resolution, request: gr.Request):
|
24 |
+
if pose_image is None:
|
25 |
+
yield None, "no pose image found !", ""
|
26 |
+
return None, "no pose image found !", ""
|
27 |
+
if cloth_image is None:
|
28 |
+
yield None, "no cloth image found !", ""
|
29 |
+
return None, "no cloth image found !", ""
|
30 |
+
|
31 |
+
pose_id = os.path.basename(pose_image).split(".")[0]
|
32 |
+
cloth_id = int(os.path.basename(cloth_image).split(".")[0])
|
33 |
+
|
34 |
+
try:
|
35 |
+
|
36 |
+
client_ip = request.client.host
|
37 |
+
x_forwarded_for = dict(request.headers).get('x-forwarded-for')
|
38 |
+
if x_forwarded_for:
|
39 |
+
client_ip = x_forwarded_for
|
40 |
+
|
41 |
+
pose_np = cv2.imread(pose_image)
|
42 |
+
faces = face_detector.detect_faces(pose_np[:,:,::-1])
|
43 |
+
if len(faces)==0:
|
44 |
+
print(client_ip, 'faces num is 0! ', flush=True)
|
45 |
+
yield None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", ""
|
46 |
+
return None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", ""
|
47 |
+
else:
|
48 |
+
x, y, w, h = faces[0]["box"]
|
49 |
+
H, W = pose_np.shape[:2]
|
50 |
+
max_face_ratio = 1/3.3
|
51 |
+
if w/W>max_face_ratio or h/H>max_face_ratio:
|
52 |
+
yield None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", ""
|
53 |
+
return None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", ""
|
54 |
+
|
55 |
+
if not check_region_warp(client_ip):
|
56 |
+
yield None, "Failed !!! Our server is under maintenance, please try again later", ""
|
57 |
+
return None, "Failed !!! Our server is under maintenance, please try again later", ""
|
58 |
+
|
59 |
+
# client_ip = '8.8.8.8'
|
60 |
+
yield None, "begin to upload ", ""
|
61 |
+
|
62 |
+
timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999)
|
63 |
+
upload_url = upload_pose_img(client_ip, timeId, pose_image)
|
64 |
+
# exit(0)
|
65 |
+
yield None, "begin to public task ", ""
|
66 |
+
# return None, "begin to public task ", ""
|
67 |
+
|
68 |
+
if len(upload_url)==0:
|
69 |
+
yield None, "fail to upload", ""
|
70 |
+
return None, "fail to upload", ""
|
71 |
+
|
72 |
+
if high_resolution:
|
73 |
+
public_res = publicClothSwap(upload_url, cloth_id, is_hr=1)
|
74 |
+
else:
|
75 |
+
public_res = publicClothSwap(upload_url, cloth_id, is_hr=0)
|
76 |
+
if public_res is None:
|
77 |
+
yield None, "fail to public you task", ""
|
78 |
+
return None, "fail to public you task", ""
|
79 |
+
|
80 |
+
print(client_ip, public_res['mid_result'])
|
81 |
+
yield public_res['mid_result'], f"task is processing, task id: {public_res['id']}, {public_res['msg']}", ""
|
82 |
+
|
83 |
+
max_try = 120*3
|
84 |
+
wait_s = 0.5
|
85 |
+
for i in range(max_try):
|
86 |
+
time.sleep(wait_s)
|
87 |
+
state = getInfRes(public_res['id'])
|
88 |
+
timestamp = int(time.time() * 1000)
|
89 |
+
if state is None:
|
90 |
+
yield public_res['mid_result'] + f"?t={timestamp}", "task query failed,", ""
|
91 |
+
elif state['status']=='PROCESSING':
|
92 |
+
yield public_res['mid_result'] + f"?t={timestamp}", f"task is processing, query {i}", ""
|
93 |
+
elif state['status']=='SUCCEED':
|
94 |
+
yield state['output1'] + f"?t={timestamp}", f"task finished, {state['msg']}", ""
|
95 |
+
return state['output1'] + f"?t={timestamp}", f"task finished, {state['msg']}", ""
|
96 |
+
elif state['status']=='FAILED':
|
97 |
+
yield None, f"task failed, {state['msg']}", ""
|
98 |
+
return None, f"task failed, {state['msg']}", ""
|
99 |
+
else:
|
100 |
+
yield public_res['mid_result'] + f"?t={timestamp}", f"task is on processing, query {i}", ""
|
101 |
+
return None, "no machine...", ""
|
102 |
+
except Exception as e:
|
103 |
+
print(e)
|
104 |
+
raise e
|
105 |
+
return None, "fail to create task", ""
|
106 |
+
|
107 |
+
with gr.Blocks() as demo:
|
108 |
+
gr.Markdown(title)
|
109 |
+
gr.Markdown(description)
|
110 |
+
|
111 |
+
with gr.Accordion('upload tips', open=False):
|
112 |
+
with gr.Row():
|
113 |
+
gr.HTML(f"<img src=\"{tip1}\" >")
|
114 |
+
gr.HTML(f"<img src=\"{tip2}\" >")
|
115 |
+
|
116 |
+
with gr.Row():
|
117 |
+
with gr.Column():
|
118 |
+
cloth_image = gr.Image(value=None, interactive=False, type="filepath", label="choose a clothing")
|
119 |
+
example = gr.Examples(inputs=cloth_image,examples_per_page=20,examples=cloth_examples, label="clothing")
|
120 |
+
hr_example = gr.Examples(inputs=cloth_image,examples_per_page=9,examples=cloth_hr_examples, label="invalid clothing")
|
121 |
+
|
122 |
+
with gr.Column():
|
123 |
+
pose_image = gr.Image(value=None, type="filepath", label="choose/upload a photo")
|
124 |
+
example_pose = gr.Examples(inputs=pose_image,
|
125 |
+
examples_per_page=20,
|
126 |
+
examples=pose_examples)
|
127 |
+
|
128 |
+
with gr.Column():
|
129 |
+
with gr.Column():
|
130 |
+
# size_slider = gr.Slider(-3, 3, value=1, interactive=True, label="clothes size")
|
131 |
+
high_resolution = gr.Checkbox(value=False, label="high resolution", interactive=True)
|
132 |
+
|
133 |
+
run_button = gr.Button(value="Run")
|
134 |
+
info_text = gr.Textbox(value="", interactive=False,
|
135 |
+
label='runtime information')
|
136 |
+
res_image = gr.Image(label="result image", value=None, type="filepath")
|
137 |
+
MK01 = gr.Markdown()
|
138 |
+
|
139 |
+
run_button.click(fn=onClick, inputs=[cloth_image, pose_image, high_resolution],
|
140 |
+
outputs=[res_image, info_text, MK01])
|
141 |
+
|
142 |
+
|
143 |
+
if __name__ == "__main__":
|
144 |
+
|
145 |
+
demo.queue(max_size=50)
|
146 |
+
# demo.queue(concurrency_count=60)
|
147 |
+
# demo.launch(server_name='0.0.0.0', server_port=225)
|
148 |
+
demo.launch(server_name='0.0.0.0')
|
149 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python
|
2 |
+
numpy
|
3 |
+
requests
|
4 |
+
gradio==4.40.0
|
5 |
+
gradio-client==1.2.0
|
6 |
+
func_timeout
|
7 |
+
mtcnn
|
8 |
+
tensorflow
|
utils.py
ADDED
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import cv2
|
5 |
+
import json
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
import requests
|
9 |
+
import func_timeout
|
10 |
+
import numpy as np
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
|
14 |
+
OssUrl = "https://selfit-deploy-1256039085.cos.accelerate.myqcloud.com/"
|
15 |
+
Regions = "IndiaPakistanBengal"
|
16 |
+
TOKEN = os.environ['TOKEN']
|
17 |
+
UKAPIURL = os.environ['UKAPIURL']
|
18 |
+
|
19 |
+
|
20 |
+
proj_dir = os.path.dirname(os.path.abspath(__file__))
|
21 |
+
data_dir = os.path.join(proj_dir, 'Datas')
|
22 |
+
# data_dir = "Datas"
|
23 |
+
tmpFolder = "tmp"
|
24 |
+
os.makedirs(tmpFolder, exist_ok=True)
|
25 |
+
|
26 |
+
|
27 |
+
def get_cloth_examples(hr=0):
|
28 |
+
cloth_dir = os.path.join(data_dir, 'ClothImgs')
|
29 |
+
examples = []
|
30 |
+
files = sorted(os.listdir(cloth_dir))
|
31 |
+
hr_clothes = list(range(588, 597))
|
32 |
+
# hr_clothes = []
|
33 |
+
for f in files:
|
34 |
+
if '.jpg' not in f and '.png' not in f:
|
35 |
+
continue
|
36 |
+
|
37 |
+
cloth_id = f.split(".")[0]
|
38 |
+
if int(cloth_id) in hr_clothes and hr==0:
|
39 |
+
continue
|
40 |
+
if int(cloth_id) not in hr_clothes and hr==1:
|
41 |
+
continue
|
42 |
+
|
43 |
+
cloth_path = os.path.join(cloth_dir, f)
|
44 |
+
examples.append(cloth_path)
|
45 |
+
examples = examples[::-1]
|
46 |
+
return examples
|
47 |
+
|
48 |
+
def get_pose_examples():
|
49 |
+
pose_dir = os.path.join(data_dir, 'PoseImgs')
|
50 |
+
examples = []
|
51 |
+
for f in os.listdir(pose_dir):
|
52 |
+
if '.jpg' not in f and '.png' not in f:
|
53 |
+
continue
|
54 |
+
pose_id = f.split(".")[0]
|
55 |
+
pose_path = os.path.join(pose_dir, f)
|
56 |
+
examples.append(pose_path)
|
57 |
+
return examples
|
58 |
+
|
59 |
+
def get_result_example(cloth_id, pose_id):
|
60 |
+
result_dir = os.path.join(data_dir, 'ResultImgs')
|
61 |
+
res_path = os.path.join(result_dir, f"{cloth_id}_{pose_id}.jpg")
|
62 |
+
return res_path
|
63 |
+
|
64 |
+
def get_tips():
|
65 |
+
path1 = OssUrl+'ClothData/Publics/PoseGuide/tip1.jpg'
|
66 |
+
path2 = OssUrl+'ClothData/Publics/PoseGuide/tip2.jpg'
|
67 |
+
return path1, path2
|
68 |
+
|
69 |
+
def upload_pose_img(clientIp, timeId, img):
|
70 |
+
fileName = clientIp.replace(".", "")+str(timeId)+".jpg"
|
71 |
+
local_path = os.path.join(tmpFolder, fileName)
|
72 |
+
img = cv2.imread(img)
|
73 |
+
cv2.imwrite(os.path.join(tmpFolder, fileName), img)
|
74 |
+
|
75 |
+
json_data = {
|
76 |
+
"token": "c0e69e5d129b11efa10c525400b75156",
|
77 |
+
"input1": fileName,
|
78 |
+
"input2": "",
|
79 |
+
"protocol": "",
|
80 |
+
"cloud": "ali"
|
81 |
+
}
|
82 |
+
|
83 |
+
session = requests.session()
|
84 |
+
ret = requests.post(
|
85 |
+
f"{UKAPIURL}/upload",
|
86 |
+
headers={'Content-Type': 'application/json'},
|
87 |
+
json=json_data
|
88 |
+
)
|
89 |
+
|
90 |
+
res = ""
|
91 |
+
if ret.status_code==200:
|
92 |
+
if 'upload1' in ret.json():
|
93 |
+
upload_url = ret.json()['upload1']
|
94 |
+
headers = {'Content-Type': 'image/jpeg'}
|
95 |
+
response = session.put(upload_url, data=open(local_path, 'rb').read(), headers=headers)
|
96 |
+
# print(response.status_code)
|
97 |
+
if response.status_code == 200:
|
98 |
+
res = upload_url
|
99 |
+
if os.path.exists(local_path):
|
100 |
+
os.remove(local_path)
|
101 |
+
return res
|
102 |
+
|
103 |
+
|
104 |
+
def publicClothSwap(image, clothId, is_hr=0):
|
105 |
+
json_data = {
|
106 |
+
"image": image,
|
107 |
+
"task_type": "11",
|
108 |
+
"param1": str(clothId),
|
109 |
+
"param2": "",
|
110 |
+
"param3": "",
|
111 |
+
"param4": str(is_hr),
|
112 |
+
"delete_if_complete": "1",
|
113 |
+
"force_celery":"1"
|
114 |
+
}
|
115 |
+
|
116 |
+
headers = {
|
117 |
+
'Authorization': f'Bearer {TOKEN}',
|
118 |
+
'Content-Type': 'application/json'
|
119 |
+
}
|
120 |
+
|
121 |
+
ret = requests.post(
|
122 |
+
f'{UKAPIURL}/public_advton',
|
123 |
+
headers=headers,
|
124 |
+
json=json_data
|
125 |
+
)
|
126 |
+
|
127 |
+
if ret.status_code == 200:
|
128 |
+
response = ret.json()
|
129 |
+
if 'mid_result' in response and 'id' in response:
|
130 |
+
return {'mid_result': response['mid_result'], 'id': response['id'], "msg": response['msg']}
|
131 |
+
# print(response)
|
132 |
+
return None
|
133 |
+
else:
|
134 |
+
return None
|
135 |
+
|
136 |
+
def getInfRes(taskId):
|
137 |
+
headers = {
|
138 |
+
'Content-Type': 'application/json'
|
139 |
+
}
|
140 |
+
json_data = {
|
141 |
+
'id': taskId
|
142 |
+
}
|
143 |
+
ret = requests.post(
|
144 |
+
f'{UKAPIURL}/status_advton',
|
145 |
+
headers=headers,
|
146 |
+
json=json_data
|
147 |
+
)
|
148 |
+
if ret.status_code == 200:
|
149 |
+
response = ret.json()
|
150 |
+
if 'status' in response:
|
151 |
+
return response
|
152 |
+
print(response)
|
153 |
+
return None
|
154 |
+
else:
|
155 |
+
return None
|
156 |
+
|
157 |
+
@func_timeout.func_set_timeout(10)
|
158 |
+
def check_region(ip):
|
159 |
+
session = requests.session()
|
160 |
+
# ret = requests.get(f"https://api.ip2location.io/?ip={ip}")
|
161 |
+
ret = requests.get(f"https://realip.cc/?ip={ip}")
|
162 |
+
# print(ret)
|
163 |
+
nat = ret.json()['country'].lower()
|
164 |
+
if nat in Regions.lower():
|
165 |
+
print(nat, 'invalid', ip)
|
166 |
+
return False
|
167 |
+
else:
|
168 |
+
print(nat, 'valid', ip)
|
169 |
+
return True
|
170 |
+
|
171 |
+
def check_region_warp(ip):
|
172 |
+
try:
|
173 |
+
return check_region(ip)
|
174 |
+
except Exception as e:
|
175 |
+
print(e)
|
176 |
+
return True
|