From a1796debb888823d0c48154fb4b93f536486adff Mon Sep 17 00:00:00 2001 From: colomi <1421901449@qq.com> Date: Tue, 15 Jul 2025 23:25:13 +0800 Subject: [PATCH] save code --- http_app.py | 75 +++++++++++++++++++++++++++++++++++++++++++---- static/index.html | 2 +- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/http_app.py b/http_app.py index cdeb3bb..5c6e82b 100644 --- a/http_app.py +++ b/http_app.py @@ -49,8 +49,62 @@ def GetPicDesc(img_url): print(text) return text -def change(human_name, cloth_name, c_width, c_height, cloth_url): +def takeoff_cloth_first(human_name): + print('换泳装') + queue = requests.get("http://localhost:8188/queue").json() + if queue["queue_running"] or queue["queue_pending"]: + return None, "cur gpu is busy" + with open('/home/szlc/code/ComfyUI/change_cloth/change_new.json', 'r', encoding='utf-8') as file: + prompt_text = file.read() + + prompt = json.loads(prompt_text) + + prompt["96"]["inputs"]["cloth_len"] = '脚踝' + prompt["96"]["inputs"]["cloth_short"] = True + + # prompt["99"]["inputs"]["width"] = int((c_width/c_height) * 1024) + + #input cloth img + prompt["22"]["inputs"]["image"] = 'cloth_black.jpg' + + #input human img + prompt["61"]["inputs"]["image"] = human_name + + #out put name + out_img_name = str(uuid.uuid4())[:8] + prompt["102"]["inputs"]["filename_prefix"] = out_img_name + + p = {"prompt": prompt} + + data = json.dumps(p).encode('utf-8') + + response = requests.post("http://localhost:8188/prompt", data=data) + prompt_id = response.json()["prompt_id"] + + + # 2. 轮询队列,直到任务完成 + while True: + queue = requests.get("http://localhost:8188/queue").json() + # print(queue) + if not queue["queue_running"] and not queue["queue_pending"]: + break # 队列为空,任务已完成 + time.sleep(0.5) # 避免频繁请求 + + # 3. 从历史记录中获取结果 + history = requests.get("http://localhost:8188/history").json() + # print("History:", history) + outputs = history[prompt_id]["outputs"] + for out in outputs: + if 'images' in outputs[out]: + for out_img in outputs[out]['images']: + if 'filename' in out_img: + out_img_file_name = out_img['filename'] + if out_img_name in out_img_file_name: + return out_img_file_name + return None + +def change(human_name, cloth_name, c_width, c_height, cloth_url, is_generated): json_str = GetPicDesc(cloth_url) json_data = json.loads(json_str) cloth_len = json_data['服装长度'] @@ -59,6 +113,17 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url): cloth_short = False if cloth_len == '难以辨认': return None, "get image type error" + + if not is_generated: + if cloth_len == '大腿' or cloth_len == '膝盖' or '小腿': + takeoff_file_name = takeoff_cloth_first(human_name) + if takeoff_file_name == None: + return None, f"takeoff_cloth_first error {human_name}" + else: + human_name = takeoff_file_name + takeoff_file_path_name = os.path.join('/home/szlc/code/ComfyUI/output', takeoff_file_name) + takeoff_file_path_name_input = os.path.join('/home/szlc/code/ComfyUI/input', takeoff_file_name) + shutil.copy(takeoff_file_path_name, takeoff_file_path_name_input) queue = requests.get("http://localhost:8188/queue").json() if queue["queue_running"] or queue["queue_pending"]: @@ -187,10 +252,10 @@ def image_to_base64(file_path, mime_type=None): # 组合成Data URI格式 return f"data:{mime_type};base64,{encoded_string}" -def process_change_cloth(human_filename, cloth_filename, output_format, img_url): +def process_change_cloth(human_filename, cloth_filename, output_format, img_url, is_generated): w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}') - out_put_name, msg = change(human_filename, cloth_filename, w, h, img_url) + out_put_name, msg = change(human_filename, cloth_filename, w, h, img_url, is_generated) if out_put_name == None: print(f'Failed to change cloth {msg}') return jsonify({"ret":-1, 'msg': f'Failed to change cloth {msg}'}), 200 @@ -246,7 +311,7 @@ def change_cloth_base64(): img_url = f"http://112.126.94.241:18888/static/imgs/{cloth_filename}" - return process_change_cloth(human_filename, cloth_filename, output_format, img_url) + return process_change_cloth(human_filename, cloth_filename, output_format, img_url, is_generated) except Exception as e: return jsonify({"ret":-1, 'error': str(e)}), 500 @@ -318,7 +383,7 @@ def change_cloth(): output_format = data.get('output_format') - return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url) + return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url, is_generated) if __name__ == '__main__': app.run(host="0.0.0.0", port=8888, debug=True) \ No newline at end of file diff --git a/static/index.html b/static/index.html index 29093c4..c82de39 100644 --- a/static/index.html +++ b/static/index.html @@ -159,7 +159,7 @@ human_img: humanDataURL, // 包含完整前缀的Base64 cloth_img: clothDataURL, // 包含完整前缀的Base64 output_format: "url", - is_generated: true, + is_generated: false, }; console.log("准备发送的数据:", data); // 调试用