From e53e65b6f43cafcd0304dea07ddb4a0edd8f8d5f Mon Sep 17 00:00:00 2001 From: colomi <1421901449@qq.com> Date: Mon, 21 Jul 2025 21:55:06 +0800 Subject: [PATCH] save code --- change_app.py | 134 +++++++++++++++++++++++++++++++++++----------- config.py | 4 +- static/index.html | 10 +++- worker.py | 1 + 4 files changed, 116 insertions(+), 33 deletions(-) diff --git a/change_app.py b/change_app.py index 518798c..ff66d49 100644 --- a/change_app.py +++ b/change_app.py @@ -97,6 +97,31 @@ def GetPicDesc(img_url): print(text) return text +def GetHumanDesDesc(img_url): + response = client.chat.completions.create( + # 指定您创建的方舟推理接入点 ID,此处已帮您修改为您的推理接入点 ID + model="doubao-1.5-vision-pro-250328", + messages=[ + { + "role": "user", + "content": [ + { + "type": "image_url", + "image_url": { + "url": img_url + }, + }, + {"type": "text", "text": "图片是一个人物的照片,返回为json格式, 第一个字段'性别', 判断一下性别,只要答案, 选项有(男、女). 第二个字段'人物',判断一下图片是否有人,只要答案,选项有(有人、没人)"}, + ], + } + ], + + ) + + text = response.choices[0].message.content + print(text) + return text + def takeoff_cloth_first(human_name): print('换泳装') queue = requests.get("http://localhost:8188/queue").json() @@ -152,7 +177,7 @@ def takeoff_cloth_first(human_name): return out_img_file_name return None -def generate_from_face(human_name, is_girl = True): +def generate_from_face(human_name, sex_girl): print('生成写真') queue = requests.get("http://localhost:8188/queue").json() if queue["queue_running"] or queue["queue_pending"]: @@ -166,7 +191,7 @@ def generate_from_face(human_name, is_girl = True): prompt["93"]["inputs"]["image"] = human_name - if is_girl: + if sex_girl: prompt["6"]["inputs"]["text"] = "full body shot(1.9),complete figure,front-facing,model pose, Hands are at the sides of the body, smile,clear skin glow,wearing black micro skirt,simple white background,clean composition,32k,high detail,professional beauty photography style," else: prompt["6"]["inputs"]["text"] = "full body shot(1.9),complete figure,front-facing,model pose,Hands are at the sides of the body,smile,clear skin glow,wearing fitted vest and tight shortst,simple white background,clean composition,32k,high detail,professional beauty photography style," @@ -204,7 +229,16 @@ def generate_from_face(human_name, is_girl = True): return out_img_file_name return None -def change(human_name, cloth_name, c_width, c_height, cloth_url, is_generated): +def change(human_name, cloth_name, c_width, c_height, cloth_url, human_url, no2): + human_json_str = GetHumanDesDesc(human_url) + print(f"human_json_str {human_json_str}") + human_json_data = json.loads(human_json_str) + sex_type = human_json_data['性别'] + sex_girl = True + if '男' in sex_type: + sex_girl = False + + json_str = GetPicDesc(cloth_url) json_data = json.loads(json_str) cloth_len = json_data['服装长度'] @@ -212,33 +246,37 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, is_generated): if '长袖' in json_data['衣袖']: cloth_short = False if cloth_len == '难以辨认': - return None, "get image type error" + return None, None, sex_girl, "get image type error" - thigh_visible = is_thigh_visible(f"/home/szlc/code/ComfyUI/input/{human_name}") - if thigh_visible: - #脱衣服 - 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) + if no2: + print(f'不用第二部 no2{no2}') else: - #生成写真 - generate_name = generate_from_face(human_name) - if generate_name == None: - return None, f"takeoff_cloth_first error {human_name}" + print(f'要第二部 no2{no2} 先搞第一步') + thigh_visible = is_thigh_visible(f"/home/szlc/code/ComfyUI/input/{human_name}") + if thigh_visible: + #脱衣服 + takeoff_file_name = takeoff_cloth_first(human_name) + if takeoff_file_name == None: + return None, None, sex_girl, 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) else: - human_name = generate_name - generate_name_file_path_name = os.path.join('/home/szlc/code/ComfyUI/output', generate_name) - generate_name_file_path_name_input = os.path.join('/home/szlc/code/ComfyUI/input', generate_name) - shutil.copy(generate_name_file_path_name, generate_name_file_path_name_input) + #生成写真 + generate_name = generate_from_face(human_name, sex_girl) + if generate_name == None: + return None, None, sex_girl, f"xiezhen error {human_name}" + else: + human_name = generate_name + generate_name_file_path_name = os.path.join('/home/szlc/code/ComfyUI/output', generate_name) + generate_name_file_path_name_input = os.path.join('/home/szlc/code/ComfyUI/input', generate_name) + shutil.copy(generate_name_file_path_name, generate_name_file_path_name_input) queue = requests.get("http://localhost:8188/queue").json() if queue["queue_running"] or queue["queue_pending"]: - return None, "cur gpu is busy" + return None, None, sex_girl, "cur gpu is busy, Shou not happen" print('开始换衣服') with open('/home/szlc/code/ComfyUI/change_cloth/change_new.json', 'r', encoding='utf-8') as file: @@ -287,8 +325,8 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, is_generated): 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, "success" - return None, "can not find output image" + return out_img_file_name, human_name, sex_girl,"success" + return None, human_name, sex_girl, "can not find output image" def save_base64_image(base64_str, prefix): """ @@ -396,14 +434,26 @@ def upload_to_oss(image_path, object_name=None): return None -def process_change_cloth(human_filename, cloth_filename, output_format, img_url, is_generated): +def process_change_cloth(human_filename, cloth_filename, output_format, img_url, human_url, no2): 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, is_generated) + out_put_name, out_human_name, is_girl, msg = change(human_filename, cloth_filename, w, h, img_url, human_url, no2) if out_put_name == None: print(f'Failed to change cloth {msg}') return jsonify({"ret":-1, 'msg': f'Failed to change cloth {msg}'}), 200 + if no2: + out_human_https_url = human_url + else: + out_human_image = Image.open(f'/home/szlc/code/ComfyUI/output/{out_human_name}') + out_human_jpg_name = out_human_name.replace(".png", ".jpg") + out_human_jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/imgs/{out_human_jpg_name}' + out_human_image.save(out_human_jpg_path_name, quality=95) + upload_to_oss(out_human_jpg_path_name, out_human_jpg_name) # 第二个参数可选,指定OSS上的路径 + out_human_https_url = f'https://llyz.oss-cn-beijing.aliyuncs.com/{out_human_jpg_name}' + print(f"生成的第一步图片 HTTPS URL: {out_human_https_url}") + + image = Image.open(f'/home/szlc/code/ComfyUI/output/{out_put_name}') jpg_name = out_put_name.replace(".png", ".jpg") jpg_path_name = f'/home/szlc/code/ComfyUI/change_cloth/static/imgs/{jpg_name}' @@ -419,6 +469,8 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url, "ret":0, "state": 0, "msg":"success", + "first_step_data":image_to_base64(out_human_jpg_path_name), + "is_girl":is_girl, "data":image_to_base64(jpg_path_name) }) else: @@ -426,6 +478,8 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url, "ret":0, "state": 0, "msg":"success", + "first_url":out_human_https_url, + "is_girl":is_girl, "url":https_url }) @@ -485,8 +539,17 @@ def do_change_cloth(): output_format = data.get('output_format') - return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url, False) - + no2 = data['no2'] + + try: + return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url, human_url, no2) + except: + return jsonify({ + "ret":-1, + "state": -1, + "msg":"process_change_cloth error" + }) + get_redis_conn().set(QUEUE_NAME, "[]") def pushStr2Queue(key, data_str): @@ -549,6 +612,10 @@ def change_cloth(): output_format = data.get('output_format') if not output_format: return jsonify({"state":-1, "error": "Missing 'output_format' parameter"}), 500 + + no2 = data.get('no2') + if not no2: + data['no2'] = False return queueCall(data) @@ -571,6 +638,7 @@ def change_cloth_base64(): human_filename = save_base64_image(human_img, 'human') if not human_filename: return jsonify({"ret":-1, 'msg': 'Failed to save human image'}), 500 + data['human_img'] = None human_url = f"http://112.126.94.241:18888/static/imgs/{human_filename}" @@ -578,11 +646,17 @@ def change_cloth_base64(): cloth_filename = save_base64_image(cloth_img, 'cloth') if not cloth_filename: return jsonify({"ret":-1, 'msg': 'Failed to save cloth image'}), 500 + data['cloth_img'] = None cloth_url = f"http://112.126.94.241:18888/static/imgs/{cloth_filename}" data["human_url"] = human_url data["cloth_url"] = cloth_url + + no2 = data.get('no2') + if not no2: + data['no2'] = False + return queueCall(data) diff --git a/config.py b/config.py index 7e6c19f..d13bedf 100644 --- a/config.py +++ b/config.py @@ -7,11 +7,11 @@ QUEUE_NAME = 'task_queue' REDIS_HOST = '127.0.0.1' REDIS_PORT = 6379 REDIS_DB = 0 -DEFAULT_TIMEOUT = (60*10) +DEFAULT_TIMEOUT = (60*20) GPU_SERVER_TIME_OUT = 180 GPU_SERVER_LIST = 'gpu_server_list_key' SERVER_LOG_ = "server_log" -SERVER_LOG_EVENT_LEN = 20 +SERVER_LOG_EVENT_LEN = 30 KEY_QUEUE_LOCK_NAME = 'KEY_QUEUE_LOCK_NAME' SERVER_LIST_LOCK_NAME = 'SERVER_LIST_LOCK_NAME' SERVER_LIST_LOG_LOCK_NAME = 'SERVER_LIST_LOG_LOCK_NAME' diff --git a/static/index.html b/static/index.html index c82de39..b3619a5 100644 --- a/static/index.html +++ b/static/index.html @@ -96,6 +96,11 @@ 衣服图片预览 +
+ +
@@ -139,6 +144,7 @@ document.getElementById('submitBtn').addEventListener('click', function() { const humanFile = document.getElementById('humanInput').files[0]; const clothFile = document.getElementById('clothInput').files[0]; + const no2 = document.getElementById("no2Checkbox").checked; if (!humanFile || !clothFile) { alert('请同时选择人体图片和衣服图片!'); @@ -159,7 +165,7 @@ human_img: humanDataURL, // 包含完整前缀的Base64 cloth_img: clothDataURL, // 包含完整前缀的Base64 output_format: "url", - is_generated: false, + no2: no2, }; console.log("准备发送的数据:", data); // 调试用 @@ -190,6 +196,8 @@

处理成功!点击查看结果:

${data.url}

+ ${data.url} +

`; } else { resultDiv.innerHTML = ` diff --git a/worker.py b/worker.py index 687b6b5..837ed4b 100644 --- a/worker.py +++ b/worker.py @@ -182,6 +182,7 @@ def call_remote_gpu_server(task_data_str, server=None): data = { "human_url": task_data['request']['human_url'], "cloth_url": task_data['request']["cloth_url"], + "no2":task_data['request']['no2'], "output_format":task_data['request']['output_format'] }