selfitcamera
commited on
Commit
•
3d0272c
1
Parent(s):
605ffa6
init
Browse files- .DS_Store +0 -0
- Datas/.DS_Store +0 -0
- tmp/12700117287042505075724.jpg → Datas/UserRecord_1/Ips/127.0.0.1.pkl +2 -2
- Datas/UserRecord_2/Ips/127.0.0.1.pkl +3 -0
- Datas/UserRecord_3/Ips/127.0.0.1.pkl +3 -0
- Datas/UserRecord_6/Ips/127.0.0.1.pkl +3 -0
- Datas/task2/.DS_Store +0 -0
- Datas/task2/showcases/.DS_Store +0 -0
- Datas/task2/showcases/out1.jpg +3 -0
- Datas/task2/srcs/.DS_Store +0 -0
- Datas/task2/srcs/caption.jpg +3 -0
- Datas/task2/srcs/street.webp +0 -0
- Datas/task2/temps/.DS_Store +0 -0
- Datas/task2/temps/caption.jpg +3 -0
- __pycache__/config.cpython-312.pyc +0 -0
- __pycache__/utils.cpython-312.pyc +0 -0
- app.py +29 -9
- config.py +23 -0
- utils.py +27 -10
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
Datas/.DS_Store
CHANGED
Binary files a/Datas/.DS_Store and b/Datas/.DS_Store differ
|
|
tmp/12700117287042505075724.jpg → Datas/UserRecord_1/Ips/127.0.0.1.pkl
RENAMED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:16b78f6ccd9445ae37a2e9f1db8b60633acb8dae7e1e8fa97fa86268e80f4668
|
3 |
+
size 306
|
Datas/UserRecord_2/Ips/127.0.0.1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a320c492b81763b540da6e823b2c8eaa2c3151d6a77bf9b107b647b8fc81aa29
|
3 |
+
size 7442
|
Datas/UserRecord_3/Ips/127.0.0.1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d548af32b28aacce0ff6f28b98e6a4528553818c281cef7752a4bf843ddbf2a6
|
3 |
+
size 569
|
Datas/UserRecord_6/Ips/127.0.0.1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:94d8b6463d24175ce07c7e3c63920c1028089b49010e33edd8c2ffaef17f0bac
|
3 |
+
size 830
|
Datas/task2/.DS_Store
ADDED
Binary file (10.2 kB). View file
|
|
Datas/task2/showcases/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
Datas/task2/showcases/out1.jpg
ADDED
Git LFS Details
|
Datas/task2/srcs/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
Datas/task2/srcs/caption.jpg
ADDED
Git LFS Details
|
Datas/task2/srcs/street.webp
ADDED
Datas/task2/temps/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
Datas/task2/temps/caption.jpg
ADDED
Git LFS Details
|
__pycache__/config.cpython-312.pyc
CHANGED
Binary files a/__pycache__/config.cpython-312.pyc and b/__pycache__/config.cpython-312.pyc differ
|
|
__pycache__/utils.cpython-312.pyc
CHANGED
Binary files a/__pycache__/utils.cpython-312.pyc and b/__pycache__/utils.cpython-312.pyc differ
|
|
app.py
CHANGED
@@ -16,15 +16,29 @@ css = """
|
|
16 |
|
17 |
|
18 |
def onClick(temp_image, user_image, caption_text, token_text, request: gr.Request):
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
print("======> user_image ", type(user_image))
|
21 |
print("======> caption_text ", type(caption_text))
|
22 |
if temp_image is None:
|
23 |
yield None, "please choose a template background!!!"
|
24 |
return None, "please choose a template background!!!"
|
25 |
if user_image is None:
|
26 |
-
yield None, "please upload a
|
27 |
-
return None, "please upload a
|
28 |
|
29 |
try:
|
30 |
client_ip = request.client.host
|
@@ -40,21 +54,21 @@ def onClick(temp_image, user_image, caption_text, token_text, request: gr.Reques
|
|
40 |
return None, info
|
41 |
|
42 |
# 上传用户照片
|
43 |
-
upload_url =
|
44 |
if len(upload_url)==0:
|
45 |
yield None, "fail to upload"
|
46 |
return None, "fail to upload"
|
47 |
|
|
|
48 |
# 发布任务
|
49 |
-
taskId = publicSelfitTask(upload_url, temp_image, caption_text)
|
50 |
if not taskId:
|
51 |
yield None, "fail to public task..."
|
52 |
return None, "fail to public task..."
|
53 |
-
|
54 |
-
# taskId = "95a19e33-1ff5-446e-ac37-d3a8dd59eba8-e1"
|
55 |
max_try = 30
|
56 |
wait_s = 3
|
57 |
-
time.sleep(
|
58 |
for i in range(max_try):
|
59 |
time.sleep(wait_s)
|
60 |
taskStatus = getTaskRes(taskId)
|
@@ -103,7 +117,13 @@ with gr.Blocks(css=css) as demo:
|
|
103 |
examples=temp_examples, visible=TempVisible)
|
104 |
with gr.Column():
|
105 |
with gr.Column():
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
caption_text = gr.Textbox(value="", interactive=True, label=CaptionLabel, visible=CapVisible)
|
108 |
with gr.Column():
|
109 |
with gr.Column():
|
|
|
16 |
|
17 |
|
18 |
def onClick(temp_image, user_image, caption_text, token_text, request: gr.Request):
|
19 |
+
|
20 |
+
user_mask = None
|
21 |
+
if taskType=='2':
|
22 |
+
user_mask = user_image['layers'][0]
|
23 |
+
user_image = user_image['background']
|
24 |
+
user_mask = (user_mask.sum(2)>0).astype(np.uint8)*255
|
25 |
+
user_image = np.array(Image.fromarray(user_image).convert('RGB'))
|
26 |
+
if user_image.sum()==0:
|
27 |
+
yield None, "please upload a photo!!!"
|
28 |
+
return None, "please upload a photo!!!"
|
29 |
+
if user_mask.sum()==0:
|
30 |
+
yield None, "please draw a area!!!"
|
31 |
+
return None, "please draw a area!!!"
|
32 |
+
|
33 |
+
print("======> temp_image ", type(temp_image))
|
34 |
print("======> user_image ", type(user_image))
|
35 |
print("======> caption_text ", type(caption_text))
|
36 |
if temp_image is None:
|
37 |
yield None, "please choose a template background!!!"
|
38 |
return None, "please choose a template background!!!"
|
39 |
if user_image is None:
|
40 |
+
yield None, "please upload a photo!!!"
|
41 |
+
return None, "please upload a photo!!!"
|
42 |
|
43 |
try:
|
44 |
client_ip = request.client.host
|
|
|
54 |
return None, info
|
55 |
|
56 |
# 上传用户照片
|
57 |
+
upload_url, uploadm_url = upload_user_img_mask(client_ip, user_image, user_mask)
|
58 |
if len(upload_url)==0:
|
59 |
yield None, "fail to upload"
|
60 |
return None, "fail to upload"
|
61 |
|
62 |
+
# return
|
63 |
# 发布任务
|
64 |
+
taskId = publicSelfitTask(upload_url, uploadm_url, temp_image, caption_text)
|
65 |
if not taskId:
|
66 |
yield None, "fail to public task..."
|
67 |
return None, "fail to public task..."
|
68 |
+
|
|
|
69 |
max_try = 30
|
70 |
wait_s = 3
|
71 |
+
# time.sleep(2)
|
72 |
for i in range(max_try):
|
73 |
time.sleep(wait_s)
|
74 |
taskStatus = getTaskRes(taskId)
|
|
|
117 |
examples=temp_examples, visible=TempVisible)
|
118 |
with gr.Column():
|
119 |
with gr.Column():
|
120 |
+
if taskType=='2':
|
121 |
+
brush = gr.Brush(colors=['#FF0000'], color_mode='fixed')
|
122 |
+
user_image = gr.ImageEditor(value=None, type="numpy",
|
123 |
+
eraser=False, brush=brush ,layers=False, sources=['upload',],
|
124 |
+
transforms=[], label=UserLabel)
|
125 |
+
else:
|
126 |
+
user_image = gr.Image(value=None, type="numpy", label=UserLabel)
|
127 |
caption_text = gr.Textbox(value="", interactive=True, label=CaptionLabel, visible=CapVisible)
|
128 |
with gr.Column():
|
129 |
with gr.Column():
|
config.py
CHANGED
@@ -11,6 +11,7 @@ import requests
|
|
11 |
import func_timeout
|
12 |
import numpy as np
|
13 |
import gradio as gr
|
|
|
14 |
from collections import OrderedDict
|
15 |
|
16 |
|
@@ -129,6 +130,28 @@ elif taskType=="1":
|
|
129 |
CaptionLabel = "English caption text (optional)"
|
130 |
CapVisible = False
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
else:
|
133 |
# 是否显示token输入框
|
134 |
is_show_token = False
|
|
|
11 |
import func_timeout
|
12 |
import numpy as np
|
13 |
import gradio as gr
|
14 |
+
from PIL import Image
|
15 |
from collections import OrderedDict
|
16 |
|
17 |
|
|
|
130 |
CaptionLabel = "English caption text (optional)"
|
131 |
CapVisible = False
|
132 |
|
133 |
+
elif taskType=="2":
|
134 |
+
# 是否显示token输入框
|
135 |
+
is_show_token = True
|
136 |
+
# 标题
|
137 |
+
title = r"""
|
138 |
+
<h1 align="center">Selfit AI App: Object Remove</h1>
|
139 |
+
"""
|
140 |
+
# 标题下的描述
|
141 |
+
description = r"""
|
142 |
+
Go to <a href='https://heybeauty.ai/extension' target='_blank'><b>HeyBeauty</b></a> for Faster and Free AI App! 🤗 .<br>
|
143 |
+
|
144 |
+
1. Please wait for a while after submitting the task, you will get the result within 30 seconds.
|
145 |
+
2. After exhausted all trial attempts, you can <a href='https://heybeauty.ai/keys' target='_blank'><b>find your api key here</b></a> .<br>
|
146 |
+
3. Share app with your friends and enjoy! 😊.<br>
|
147 |
+
"""
|
148 |
+
# 组件信息
|
149 |
+
TempLabel = "guide"
|
150 |
+
TempVisible = True
|
151 |
+
UserLabel = "upload and paint photo"
|
152 |
+
CaptionLabel = "English caption text (optional)"
|
153 |
+
CapVisible = False
|
154 |
+
|
155 |
else:
|
156 |
# 是否显示token输入框
|
157 |
is_show_token = False
|
utils.py
CHANGED
@@ -86,7 +86,7 @@ class UserRecorder(object):
|
|
86 |
if token_valid:
|
87 |
return True, ""
|
88 |
else:
|
89 |
-
return False, "
|
90 |
else:
|
91 |
_, total_n, _ = self.get_record(ip=ip, token=token)
|
92 |
if total_n>=LimitTask:
|
@@ -110,7 +110,6 @@ class UserRecorder(object):
|
|
110 |
success_n, fail_n, process_n = 0, 0, 0
|
111 |
shows = [None]*6
|
112 |
show_i = 0
|
113 |
-
print(record_data)
|
114 |
for key in reversed(record_data):
|
115 |
status = record_data[key]['status']
|
116 |
if status in ['FAILED', 'CANCELLED', 'TIMED_OUT', ]:
|
@@ -189,6 +188,10 @@ def get_showcase_examples(taskType):
|
|
189 |
examples=[
|
190 |
["task1/temps/caption.jpg", "task1/srcs/src01.jpg", "task1/showcases/src01_seg.png"],
|
191 |
]
|
|
|
|
|
|
|
|
|
192 |
|
193 |
for i in range(len(examples)):
|
194 |
for j in range(len(examples[i])):
|
@@ -201,12 +204,16 @@ def get_result_example(cloth_id, pose_id):
|
|
201 |
res_path = os.path.join(result_dir, f"{cloth_id}_{pose_id}.jpg")
|
202 |
return res_path
|
203 |
|
204 |
-
def
|
205 |
timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999)
|
206 |
fileName = clientIp.replace(".", "")+str(timeId)+".jpg"
|
207 |
local_path = os.path.join(tmpFolder, fileName)
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
210 |
session = requests.session()
|
211 |
ret = requests.post(f"{apiUrl}/upload", data=json.dumps(params))
|
212 |
res = ""
|
@@ -216,6 +223,7 @@ def upload_user_img(clientIp, img):
|
|
216 |
ctype = head_dict[ftype]
|
217 |
headers = {"Content-Type": ctype}
|
218 |
|
|
|
219 |
if ret.status_code==200:
|
220 |
if 'upload1' in ret.json():
|
221 |
upload_url = ret.json()['upload1']
|
@@ -225,19 +233,28 @@ def upload_user_img(clientIp, img):
|
|
225 |
res = upload_url
|
226 |
else:
|
227 |
print(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
else:
|
229 |
print(ret.json(), ret.status_code, 'call upload failed')
|
230 |
-
if os.path.exists(local_path):
|
231 |
-
|
232 |
-
return res
|
233 |
|
234 |
|
235 |
-
def publicSelfitTask(image, temp_image, caption_text):
|
236 |
if taskType in ['5']: # 无模板
|
237 |
temp_name = ''
|
238 |
else:
|
239 |
temp_name = os.path.basename(temp_image).split('.')[0]
|
240 |
-
params = {'openId':openId, 'apiKey':apiKey, 'image':image, 'mask':
|
241 |
"image_type":"2", "task_type":taskType, 'param1':temp_name,
|
242 |
'param2':str(caption_text), 'param3':"1", 'param4':"", 'param5':""}
|
243 |
session = requests.session()
|
|
|
86 |
if token_valid:
|
87 |
return True, ""
|
88 |
else:
|
89 |
+
return False, "api key is invalid"
|
90 |
else:
|
91 |
_, total_n, _ = self.get_record(ip=ip, token=token)
|
92 |
if total_n>=LimitTask:
|
|
|
110 |
success_n, fail_n, process_n = 0, 0, 0
|
111 |
shows = [None]*6
|
112 |
show_i = 0
|
|
|
113 |
for key in reversed(record_data):
|
114 |
status = record_data[key]['status']
|
115 |
if status in ['FAILED', 'CANCELLED', 'TIMED_OUT', ]:
|
|
|
188 |
examples=[
|
189 |
["task1/temps/caption.jpg", "task1/srcs/src01.jpg", "task1/showcases/src01_seg.png"],
|
190 |
]
|
191 |
+
elif taskType=="2":
|
192 |
+
examples=[
|
193 |
+
["task2/temps/caption.jpg", "task2/srcs/street.webp", "task2/showcases/out1.jpg"],
|
194 |
+
]
|
195 |
|
196 |
for i in range(len(examples)):
|
197 |
for j in range(len(examples[i])):
|
|
|
204 |
res_path = os.path.join(result_dir, f"{cloth_id}_{pose_id}.jpg")
|
205 |
return res_path
|
206 |
|
207 |
+
def upload_user_img_mask(clientIp, img, mask=None):
|
208 |
timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999)
|
209 |
fileName = clientIp.replace(".", "")+str(timeId)+".jpg"
|
210 |
local_path = os.path.join(tmpFolder, fileName)
|
211 |
+
filemName = clientIp.replace(".", "")+str(timeId)+"_m.jpg"
|
212 |
+
localm_path = os.path.join(tmpFolder, filemName)
|
213 |
+
cv2.imwrite(local_path, img[:,:,::-1].astype(np.uint8))
|
214 |
+
if mask is not None:
|
215 |
+
cv2.imwrite(localm_path, mask)
|
216 |
+
params = {'token':uploadToken, 'input1':fileName, 'input2':filemName}
|
217 |
session = requests.session()
|
218 |
ret = requests.post(f"{apiUrl}/upload", data=json.dumps(params))
|
219 |
res = ""
|
|
|
223 |
ctype = head_dict[ftype]
|
224 |
headers = {"Content-Type": ctype}
|
225 |
|
226 |
+
uploadm_url = ''
|
227 |
if ret.status_code==200:
|
228 |
if 'upload1' in ret.json():
|
229 |
upload_url = ret.json()['upload1']
|
|
|
233 |
res = upload_url
|
234 |
else:
|
235 |
print(response)
|
236 |
+
if mask is not None:
|
237 |
+
uploadm_url = ret.json()['upload2']
|
238 |
+
with open(localm_path, 'rb') as file:
|
239 |
+
response = requests.put(uploadm_url, data=file, headers=headers)
|
240 |
+
if response.status_code == 200:
|
241 |
+
pass
|
242 |
+
else:
|
243 |
+
uploadm_url = ''
|
244 |
+
print(response)
|
245 |
else:
|
246 |
print(ret.json(), ret.status_code, 'call upload failed')
|
247 |
+
if os.path.exists(local_path): os.remove(local_path)
|
248 |
+
if os.path.exists(localm_path): os.remove(localm_path)
|
249 |
+
return res, uploadm_url
|
250 |
|
251 |
|
252 |
+
def publicSelfitTask(image, mask, temp_image, caption_text):
|
253 |
if taskType in ['5']: # 无模板
|
254 |
temp_name = ''
|
255 |
else:
|
256 |
temp_name = os.path.basename(temp_image).split('.')[0]
|
257 |
+
params = {'openId':openId, 'apiKey':apiKey, 'image':image, 'mask':mask,
|
258 |
"image_type":"2", "task_type":taskType, 'param1':temp_name,
|
259 |
'param2':str(caption_text), 'param3':"1", 'param4':"", 'param5':""}
|
260 |
session = requests.session()
|