From 71ec26c7dead8077f1b3b74fc990e8edd7820e52 Mon Sep 17 00:00:00 2001 From: xsl Date: Sat, 4 Oct 2025 17:14:04 +0800 Subject: [PATCH] save code --- change_app.py | 19 +++++++++++-------- static/index.html | 48 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/change_app.py b/change_app.py index e8993ad..08f579c 100644 --- a/change_app.py +++ b/change_app.py @@ -292,11 +292,11 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, human_url, no2, return None, None, sex_girl, "cur gpu is busy, Shou not happen" if tuodi: - print(f'开始换衣服 {change_tuodi_input} cloth_len:{cloth_len} cloth_short:{cloth_short} kuzi:{kuzi}') + print(f'开始换衣服 {change_tuodi_input} cloth_len:{cloth_len} cloth_short:{cloth_short}') with open(f'{APP_ROOT}/{change_tuodi_input}', 'r', encoding='utf-8') as file: prompt_text = file.read() else: - print(f'开始换衣服 {change_only_name} cloth_len:{cloth_len} cloth_short:{cloth_short} kuzi:{kuzi}') + print(f'开始换衣服 {change_only_name} cloth_len:{cloth_len} cloth_short:{cloth_short}') with open(f'{APP_ROOT}/{change_only_name}', 'r', encoding='utf-8') as file: prompt_text = file.read() @@ -509,7 +509,7 @@ def upload_to_oss(image_path, object_name=None): def process_change_cloth(human_filename, cloth_filename, output_format, img_url, human_url, no2, tuodi, kuzi_url): w,h = get_image_dimensions(f'{APP_ROOT}/../input/{human_filename}') - out_put_name, out_human_name, is_girl, msg = change(human_filename, cloth_filename, w, h, img_url, human_url, no2, tuodi, kuzi_url) + out_put_name, out_human_name, is_girl, msg = change(human_filename, cloth_filename, w, h, img_url, human_url, no2, tuodi) if out_put_name == None: print(f'Failed to change cloth {msg}') return jsonify({"ret":-1, 'msg': f'Failed to change cloth {msg}'}), 200 @@ -540,8 +540,13 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url, out_befor_kuzi_url = https_url if kuzi_url: + output_path_name = os.path.join(f'{APP_ROOT}/../output', out_put_name) + output_path_name_input = os.path.join(f'{APP_ROOT}/../input', out_put_name) + shutil.copy(output_path_name, output_path_name_input) + + kuzi_filename = save_image_from_url(kuzi_url) - kuzi_out_put_name = change_kuzi(human_filename, kuzi_filename) + kuzi_out_put_name, msg = change_kuzi(out_put_name, kuzi_filename) image = Image.open(f'{APP_ROOT}/../output/{kuzi_out_put_name}') jpg_name = kuzi_out_put_name.replace(".png", ".jpg") jpg_path_name = f'{APP_ROOT}/static/imgs/{jpg_name}' @@ -558,7 +563,7 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url, "ret":0, "state": 0, "msg":"success", - "second_step_datea":image_to_base64(out_befor_kuzi_url), + "second_step_data":image_to_base64(out_befor_kuzi_url), "first_step_data":image_to_base64(out_human_jpg_path_name), "is_girl":is_girl, "data":image_to_base64(jpg_path_name) @@ -644,12 +649,10 @@ def do_change_cloth(): tuodi = data['tuodi'] print(f"tuodi:{tuodi}") + kuzi_url = None if 'kuzi_url' in data: kuzi_url = data.get("kuzi_url") - kuzi_filename = save_image_from_url(kuzi_url) print(f"要弄裤子 kuzi:{kuzi_url}") - - try: return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url, human_url, no2, tuodi, kuzi_url) diff --git a/static/index.html b/static/index.html index 144196a..3267d29 100644 --- a/static/index.html +++ b/static/index.html @@ -96,17 +96,25 @@ 衣服图片预览 +
+
+

裤子图片

+ + 裤子图片预览 +
+
+
- -
@@ -146,14 +154,28 @@ reader.readAsDataURL(file); } }); + + document.getElementById('kuziInput').addEventListener('change', function(e) { + const file = e.target.files[0]; + if (file) { + const reader = new FileReader(); + reader.onload = function(event) { + const img = document.getElementById('kuziPreview'); + img.src = event.target.result; + img.style.display = 'block'; + }; + reader.readAsDataURL(file); + } + }); // 提交处理 document.getElementById('submitBtn').addEventListener('click', function() { const humanFile = document.getElementById('humanInput').files[0]; const clothFile = document.getElementById('clothInput').files[0]; + const kuziFile = document.getElementById('kuziInput').files[0]; const no2 = document.getElementById("no2Checkbox").checked; const tuodi = document.getElementById("tuodiCheckbox").checked; - const kuzi = document.getElementById("kuziCheckbox").checked; + if (!humanFile || !clothFile) { alert('请同时选择人体图片和衣服图片!'); @@ -167,17 +189,25 @@ // 读取图片并转换为Base64(包含完整前缀) Promise.all([ readFileAsDataURL(humanFile), - readFileAsDataURL(clothFile) - ]).then(([humanDataURL, clothDataURL]) => { + readFileAsDataURL(clothFile), + kuziFile ? readFileAsDataURL(kuziFile) : Promise.resolve(null) + ]).then(([humanDataURL, clothDataURL, kuziDataURL]) => { + + // 准备请求数据 const data = { human_img: humanDataURL, // 包含完整前缀的Base64 cloth_img: clothDataURL, // 包含完整前缀的Base64 output_format: "url", no2: no2, - tuodi:tuodi, - kuzi:kuzi + tuodi:tuodi }; + + if(kuziDataURL) + { + data.kuzi_img = kuziDataURL; + } + console.log("准备发送的数据:", data); // 调试用 @@ -209,6 +239,8 @@

${data.url}

+ ${data.url} +

`; } else { resultDiv.innerHTML = `