Spaces:
Running
Running
Commit
·
9a354ff
1
Parent(s):
99397a6
init
Browse files- __pycache__/utils.cpython-310.pyc +0 -0
- app.py +118 -27
- utils.py +72 -0
__pycache__/utils.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/utils.cpython-310.pyc and b/__pycache__/utils.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -43,13 +43,71 @@ def is_http_resource_accessible(url):
|
|
| 43 |
return False
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def onClick(cloth_image, pose_image, high_resolution, request: gr.Request):
|
| 47 |
if pose_image is None:
|
| 48 |
-
yield None, "no pose image found !", ""
|
| 49 |
-
return None, "no pose image found !", ""
|
| 50 |
if cloth_image is None:
|
| 51 |
-
yield None, "no cloth image found !", ""
|
| 52 |
-
return None, "no cloth image found !", ""
|
| 53 |
|
| 54 |
pose_id = os.path.basename(pose_image).split(".")[0]
|
| 55 |
cloth_id = int(os.path.basename(cloth_image).split(".")[0])
|
|
@@ -65,45 +123,45 @@ def onClick(cloth_image, pose_image, high_resolution, request: gr.Request):
|
|
| 65 |
faces = face_detector.detect_faces(pose_np[:,:,::-1])
|
| 66 |
if len(faces)==0:
|
| 67 |
print(client_ip, 'faces num is 0! ', flush=True)
|
| 68 |
-
yield None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", ""
|
| 69 |
-
return None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", ""
|
| 70 |
else:
|
| 71 |
x, y, w, h = faces[0]["box"]
|
| 72 |
H, W = pose_np.shape[:2]
|
| 73 |
max_face_ratio = 1/3.3
|
| 74 |
if w/W>max_face_ratio or h/H>max_face_ratio:
|
| 75 |
-
yield None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", ""
|
| 76 |
-
return None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", ""
|
| 77 |
|
| 78 |
if not check_region_warp(client_ip):
|
| 79 |
-
yield None, "Failed !!! Our server is under maintenance, please try again later", ""
|
| 80 |
-
return None, "Failed !!! Our server is under maintenance, please try again later", ""
|
| 81 |
|
| 82 |
# client_ip = '8.8.8.8'
|
| 83 |
-
yield None, "begin to upload ", ""
|
| 84 |
|
| 85 |
timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999)
|
| 86 |
upload_url = upload_pose_img(client_ip, timeId, pose_image)
|
| 87 |
# exit(0)
|
| 88 |
-
yield None, "begin to public task ", ""
|
| 89 |
# return None, "begin to public task ", ""
|
| 90 |
|
| 91 |
if len(upload_url)==0:
|
| 92 |
-
yield None, "fail to upload", ""
|
| 93 |
-
return None, "fail to upload", ""
|
| 94 |
|
| 95 |
if high_resolution:
|
| 96 |
public_res = publicClothSwap(upload_url, cloth_id, is_hr=1)
|
| 97 |
else:
|
| 98 |
public_res = publicClothSwap(upload_url, cloth_id, is_hr=0)
|
| 99 |
if public_res is None:
|
| 100 |
-
yield None, "fail to public you task", ""
|
| 101 |
-
return None, "fail to public you task", ""
|
| 102 |
|
| 103 |
print(client_ip, public_res['mid_result'])
|
| 104 |
# Check if mid_result resource is accessible
|
| 105 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 106 |
-
yield mid_result, f"task is processing, task id: {public_res['id']}, {public_res['msg']}", ""
|
| 107 |
|
| 108 |
max_try = 120*3
|
| 109 |
wait_s = 0.5
|
|
@@ -114,26 +172,27 @@ def onClick(cloth_image, pose_image, high_resolution, request: gr.Request):
|
|
| 114 |
if state is None:
|
| 115 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 116 |
result_url = mid_result + f"?t={timestamp}" if mid_result else None
|
| 117 |
-
yield result_url, "task query failed,", ""
|
| 118 |
elif state['status']=='PROCESSING':
|
| 119 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 120 |
result_url = mid_result + f"?t={timestamp}" if mid_result else None
|
| 121 |
-
yield result_url, f"task is processing, query {i}", ""
|
| 122 |
elif state['status']=='SUCCEED':
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 125 |
elif state['status']=='FAILED':
|
| 126 |
-
yield None, f"task failed, {state['msg']}", ""
|
| 127 |
-
return None, f"task failed, {state['msg']}", ""
|
| 128 |
else:
|
| 129 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 130 |
result_url = mid_result + f"?t={timestamp}" if mid_result else None
|
| 131 |
-
yield result_url, f"task is on processing, query {i}", ""
|
| 132 |
-
return None, "no machine...", ""
|
| 133 |
except Exception as e:
|
| 134 |
print(e)
|
| 135 |
raise e
|
| 136 |
-
return None, "fail to create task", ""
|
| 137 |
|
| 138 |
with gr.Blocks() as demo:
|
| 139 |
gr.Markdown(title)
|
|
@@ -167,8 +226,40 @@ with gr.Blocks() as demo:
|
|
| 167 |
res_image = gr.Image(label="result image", value=None, type="filepath")
|
| 168 |
MK01 = gr.Markdown()
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
run_button.click(fn=onClick, inputs=[cloth_image, pose_image, high_resolution],
|
| 171 |
-
outputs=[res_image, info_text, MK01])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
|
|
|
| 43 |
return False
|
| 44 |
|
| 45 |
|
| 46 |
+
def onPoseChange(prompt_text, source_image, request: gr.Request):
|
| 47 |
+
"""处理姿势变换请求"""
|
| 48 |
+
if source_image is None:
|
| 49 |
+
return "请先提供源图片!", None, None, None
|
| 50 |
+
|
| 51 |
+
if not prompt_text or prompt_text.strip() == "":
|
| 52 |
+
prompt_text = "Change the pose: two hands on hips.#Change the pose: arms extended to show outfit."
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
client_ip = request.client.host
|
| 56 |
+
x_forwarded_for = dict(request.headers).get('x-forwarded-for')
|
| 57 |
+
if x_forwarded_for:
|
| 58 |
+
client_ip = x_forwarded_for
|
| 59 |
+
|
| 60 |
+
# 如果source_image是本地文件路径,需要先上传
|
| 61 |
+
if isinstance(source_image, str) and not source_image.startswith('http'):
|
| 62 |
+
timeId = int(str(time.time()).replace(".", "")) + random.randint(1000, 9999)
|
| 63 |
+
image_url = upload_pose_img(client_ip, timeId, source_image)
|
| 64 |
+
if not image_url:
|
| 65 |
+
return "图片上传失败!", None, None, None
|
| 66 |
+
else:
|
| 67 |
+
image_url = source_image
|
| 68 |
+
|
| 69 |
+
# 发起姿势变换请求
|
| 70 |
+
pose_result = public_pose_changer(image_url, prompt_text)
|
| 71 |
+
if pose_result is None:
|
| 72 |
+
return "姿势变换请求失败!", None, None, None
|
| 73 |
+
|
| 74 |
+
# 轮询结果
|
| 75 |
+
max_try = 120
|
| 76 |
+
wait_s = 1
|
| 77 |
+
for i in range(max_try):
|
| 78 |
+
time.sleep(wait_s)
|
| 79 |
+
result = get_pose_changer_res(pose_result['id'])
|
| 80 |
+
|
| 81 |
+
if result is None:
|
| 82 |
+
continue
|
| 83 |
+
elif result['status'] == 'PROCESSING':
|
| 84 |
+
continue
|
| 85 |
+
elif result['status'] == 'SUCCEED':
|
| 86 |
+
# 提取前3个有效的输出图片
|
| 87 |
+
output_images = [None, None, None]
|
| 88 |
+
for j in range(1, 4): # output1 到 output3
|
| 89 |
+
output_key = f'output{j}'
|
| 90 |
+
if output_key in result and result[output_key] and result[output_key].strip():
|
| 91 |
+
timestamp = int(time.time() * 1000)
|
| 92 |
+
output_images[j-1] = result[output_key] + f"?t={timestamp}"
|
| 93 |
+
|
| 94 |
+
return f"姿势变换完成! {result.get('msg', '')}", output_images[0], output_images[1], output_images[2]
|
| 95 |
+
elif result['status'] == 'FAILED':
|
| 96 |
+
return f"姿势变换失败: {result.get('msg', '')}", None, None, None
|
| 97 |
+
|
| 98 |
+
return "姿势变换超时!", None, None, None
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"姿势变换异常: {e}")
|
| 102 |
+
return f"处理异常: {str(e)}", None, None, None
|
| 103 |
+
|
| 104 |
def onClick(cloth_image, pose_image, high_resolution, request: gr.Request):
|
| 105 |
if pose_image is None:
|
| 106 |
+
yield None, "no pose image found !", "", None
|
| 107 |
+
return None, "no pose image found !", "", None
|
| 108 |
if cloth_image is None:
|
| 109 |
+
yield None, "no cloth image found !", "", None
|
| 110 |
+
return None, "no cloth image found !", "", None
|
| 111 |
|
| 112 |
pose_id = os.path.basename(pose_image).split(".")[0]
|
| 113 |
cloth_id = int(os.path.basename(cloth_image).split(".")[0])
|
|
|
|
| 123 |
faces = face_detector.detect_faces(pose_np[:,:,::-1])
|
| 124 |
if len(faces)==0:
|
| 125 |
print(client_ip, 'faces num is 0! ', flush=True)
|
| 126 |
+
yield None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", "", None
|
| 127 |
+
return None, "Fatal Error !!! No face detected !!! You must upload a human photo!!! Not clothing photo!!!", "", None
|
| 128 |
else:
|
| 129 |
x, y, w, h = faces[0]["box"]
|
| 130 |
H, W = pose_np.shape[:2]
|
| 131 |
max_face_ratio = 1/3.3
|
| 132 |
if w/W>max_face_ratio or h/H>max_face_ratio:
|
| 133 |
+
yield None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", "", None
|
| 134 |
+
return None, "Fatal Error !!! Headshot is not allowed !!! You must upload a full-body or half-body photo!!!", "", None
|
| 135 |
|
| 136 |
if not check_region_warp(client_ip):
|
| 137 |
+
yield None, "Failed !!! Our server is under maintenance, please try again later", "", None
|
| 138 |
+
return None, "Failed !!! Our server is under maintenance, please try again later", "", None
|
| 139 |
|
| 140 |
# client_ip = '8.8.8.8'
|
| 141 |
+
yield None, "begin to upload ", "", None
|
| 142 |
|
| 143 |
timeId = int( str(time.time()).replace(".", "") )+random.randint(1000, 9999)
|
| 144 |
upload_url = upload_pose_img(client_ip, timeId, pose_image)
|
| 145 |
# exit(0)
|
| 146 |
+
yield None, "begin to public task ", "", None
|
| 147 |
# return None, "begin to public task ", ""
|
| 148 |
|
| 149 |
if len(upload_url)==0:
|
| 150 |
+
yield None, "fail to upload", "", None
|
| 151 |
+
return None, "fail to upload", "", None
|
| 152 |
|
| 153 |
if high_resolution:
|
| 154 |
public_res = publicClothSwap(upload_url, cloth_id, is_hr=1)
|
| 155 |
else:
|
| 156 |
public_res = publicClothSwap(upload_url, cloth_id, is_hr=0)
|
| 157 |
if public_res is None:
|
| 158 |
+
yield None, "fail to public you task", "", None
|
| 159 |
+
return None, "fail to public you task", "", None
|
| 160 |
|
| 161 |
print(client_ip, public_res['mid_result'])
|
| 162 |
# Check if mid_result resource is accessible
|
| 163 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 164 |
+
yield mid_result, f"task is processing, task id: {public_res['id']}, {public_res['msg']}", "", mid_result
|
| 165 |
|
| 166 |
max_try = 120*3
|
| 167 |
wait_s = 0.5
|
|
|
|
| 172 |
if state is None:
|
| 173 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 174 |
result_url = mid_result + f"?t={timestamp}" if mid_result else None
|
| 175 |
+
yield result_url, "task query failed,", "", result_url
|
| 176 |
elif state['status']=='PROCESSING':
|
| 177 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 178 |
result_url = mid_result + f"?t={timestamp}" if mid_result else None
|
| 179 |
+
yield result_url, f"task is processing, query {i}", "", result_url
|
| 180 |
elif state['status']=='SUCCEED':
|
| 181 |
+
result_image = state['output1'] + f"?t={timestamp}"
|
| 182 |
+
yield result_image, f"task finished, {state['msg']}", "", result_image
|
| 183 |
+
return result_image, f"task finished, {state['msg']}", "", result_image
|
| 184 |
elif state['status']=='FAILED':
|
| 185 |
+
yield None, f"task failed, {state['msg']}", "", None
|
| 186 |
+
return None, f"task failed, {state['msg']}", "", None
|
| 187 |
else:
|
| 188 |
mid_result = public_res['mid_result'] if is_http_resource_accessible(public_res['mid_result']) else None
|
| 189 |
result_url = mid_result + f"?t={timestamp}" if mid_result else None
|
| 190 |
+
yield result_url, f"task is on processing, query {i}", "", result_url
|
| 191 |
+
return None, "no machine...", "", None
|
| 192 |
except Exception as e:
|
| 193 |
print(e)
|
| 194 |
raise e
|
| 195 |
+
return None, "fail to create task", "", None
|
| 196 |
|
| 197 |
with gr.Blocks() as demo:
|
| 198 |
gr.Markdown(title)
|
|
|
|
| 226 |
res_image = gr.Image(label="result image", value=None, type="filepath")
|
| 227 |
MK01 = gr.Markdown()
|
| 228 |
|
| 229 |
+
# 添加pose changer模块
|
| 230 |
+
with gr.Accordion('pose changer', open=False):
|
| 231 |
+
# 上方:文本框和按钮
|
| 232 |
+
with gr.Row():
|
| 233 |
+
pose_prompt = gr.Textbox(
|
| 234 |
+
value="Change the pose: two hands on hips.#Change the pose: arms extended to show outfit.",
|
| 235 |
+
label="姿势变换提示词",
|
| 236 |
+
placeholder="输入姿势变换的描述...",
|
| 237 |
+
lines=2,
|
| 238 |
+
scale=4
|
| 239 |
+
)
|
| 240 |
+
change_button = gr.Button(value="Change", scale=1)
|
| 241 |
+
|
| 242 |
+
# 下方:左边源图片,右边结果图片
|
| 243 |
+
with gr.Row():
|
| 244 |
+
with gr.Column(scale=1):
|
| 245 |
+
pose_changer_image = gr.Image(value=None, type="filepath", label="源图片", interactive=True)
|
| 246 |
+
pose_change_info = gr.Textbox(value="", interactive=False, label="处理信息")
|
| 247 |
+
|
| 248 |
+
with gr.Column(scale=2):
|
| 249 |
+
with gr.Row():
|
| 250 |
+
pose_result_1 = gr.Image(label="结果1", value=None, type="filepath")
|
| 251 |
+
pose_result_2 = gr.Image(label="结果2", value=None, type="filepath")
|
| 252 |
+
pose_result_3 = gr.Image(label="结果3", value=None, type="filepath")
|
| 253 |
+
|
| 254 |
run_button.click(fn=onClick, inputs=[cloth_image, pose_image, high_resolution],
|
| 255 |
+
outputs=[res_image, info_text, MK01, pose_changer_image])
|
| 256 |
+
|
| 257 |
+
# 绑定pose changer的change按钮
|
| 258 |
+
change_button.click(
|
| 259 |
+
fn=onPoseChange,
|
| 260 |
+
inputs=[pose_prompt, pose_changer_image],
|
| 261 |
+
outputs=[pose_change_info, pose_result_1, pose_result_2, pose_result_3]
|
| 262 |
+
)
|
| 263 |
|
| 264 |
|
| 265 |
if __name__ == "__main__":
|
utils.py
CHANGED
|
@@ -174,3 +174,75 @@ def check_region_warp(ip):
|
|
| 174 |
except Exception as e:
|
| 175 |
print(e)
|
| 176 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
except Exception as e:
|
| 175 |
print(e)
|
| 176 |
return True
|
| 177 |
+
|
| 178 |
+
def public_pose_changer(image_url, prompt="Change the pose: two hands on hips.#Change the pose: arms extended to show outfit."):
|
| 179 |
+
"""
|
| 180 |
+
发送姿势变换请求到API
|
| 181 |
+
"""
|
| 182 |
+
headers = {
|
| 183 |
+
'Content-Type': 'application/json',
|
| 184 |
+
'Authorization': f'Bearer {TOKEN}'
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
json_data = {
|
| 188 |
+
'image': image_url,
|
| 189 |
+
'param1': 'pose_change',
|
| 190 |
+
'param2': prompt,
|
| 191 |
+
'param3': '',
|
| 192 |
+
'param4': '',
|
| 193 |
+
'param5': '',
|
| 194 |
+
'is_private': '0',
|
| 195 |
+
'delete_if_complete': '0'
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
try:
|
| 199 |
+
ret = requests.post(
|
| 200 |
+
f'{UKAPIURL}/public_comfyui',
|
| 201 |
+
headers=headers,
|
| 202 |
+
json=json_data
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
if ret.status_code == 200:
|
| 206 |
+
response = ret.json()
|
| 207 |
+
if 'id' in response:
|
| 208 |
+
return {'id': response['id'], 'msg': response.get('msg', '')}
|
| 209 |
+
return None
|
| 210 |
+
else:
|
| 211 |
+
print(f"姿势变换请求失败,状态码: {ret.status_code}")
|
| 212 |
+
return None
|
| 213 |
+
except Exception as e:
|
| 214 |
+
print(f"姿势变换请求异常: {e}")
|
| 215 |
+
return None
|
| 216 |
+
|
| 217 |
+
def get_pose_changer_res(task_id):
|
| 218 |
+
"""
|
| 219 |
+
获取姿势变换任务的结果
|
| 220 |
+
"""
|
| 221 |
+
headers = {
|
| 222 |
+
'Content-Type': 'application/json'
|
| 223 |
+
}
|
| 224 |
+
try:
|
| 225 |
+
ret = requests.post(
|
| 226 |
+
f'{UKAPIURL}/status_comfyui',
|
| 227 |
+
headers=headers,
|
| 228 |
+
json={ 'id': task_id}
|
| 229 |
+
)
|
| 230 |
+
if ret.status_code == 200:
|
| 231 |
+
response = ret.json()
|
| 232 |
+
return response
|
| 233 |
+
else:
|
| 234 |
+
print(f"获取姿势变换结果失败,状态码: {ret.status_code}")
|
| 235 |
+
return None
|
| 236 |
+
except Exception as e:
|
| 237 |
+
print(f"获取姿势变换结果异常: {e}")
|
| 238 |
+
return None
|
| 239 |
+
|
| 240 |
+
if __name__ == "__main__":
|
| 241 |
+
|
| 242 |
+
res = public_pose_changer(image_url="https://www.selfitcamera.site/SelfitUpload/2024-10-11/p2.jpg",
|
| 243 |
+
prompt="Change the pose: two hands on hips.#Change the pose: arms extended to show outfit.")
|
| 244 |
+
print(res)
|
| 245 |
+
for _ in range(100):
|
| 246 |
+
result = get_pose_changer_res(res['id'])
|
| 247 |
+
print(result)
|
| 248 |
+
time.sleep(1)
|