save code

This commit is contained in:
xsl
2025-07-31 11:22:40 +08:00
parent e051ccb35f
commit b9f85fc0df
9 changed files with 53 additions and 41 deletions
+34 -22
View File
@@ -1,7 +1,7 @@
import os
import uuid
import requests
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, Response
from urllib.parse import urlparse
import json
import base64
@@ -128,7 +128,7 @@ def takeoff_cloth_first(human_name, is_girl):
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:
with open('/home/xsl/code/ComfyUI/change_cloth/change_new.json', 'r', encoding='utf-8') as file:
prompt_text = file.read()
prompt = json.loads(prompt_text)
@@ -186,7 +186,7 @@ def generate_from_face(human_name, sex_girl):
if queue["queue_running"] or queue["queue_pending"]:
return None, "cur gpu is busy"
with open('/home/szlc/code/ComfyUI/change_cloth/xiezhen_girl.json', 'r', encoding='utf-8') as file:
with open('/home/xsl/code/ComfyUI/change_cloth/xiezhen_girl.json', 'r', encoding='utf-8') as file:
prompt_text = file.read()
prompt = json.loads(prompt_text)
@@ -256,16 +256,15 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, human_url, no2)
print(f'不用第二部 no2{no2}')
else:
print(f'要第二部 no2{no2} 先搞第一步')
# thigh_visible = is_thigh_visible(f"/home/szlc/code/ComfyUI/input/{human_name}")
if sgwzd >= 0.5:
if sgwzd >= 0.55:
#脱衣服
takeoff_file_name = takeoff_cloth_first(human_name, sex_girl)
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)
takeoff_file_path_name = os.path.join('/home/xsl/code/ComfyUI/output', takeoff_file_name)
takeoff_file_path_name_input = os.path.join('/home/xsl/code/ComfyUI/input', takeoff_file_name)
shutil.copy(takeoff_file_path_name, takeoff_file_path_name_input)
else:
#生成写真
@@ -274,8 +273,8 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, human_url, no2)
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)
generate_name_file_path_name = os.path.join('/home/xsl/code/ComfyUI/output', generate_name)
generate_name_file_path_name_input = os.path.join('/home/xsl/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()
@@ -283,7 +282,7 @@ 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"
print('开始换衣服')
with open('/home/szlc/code/ComfyUI/change_cloth/change_new.json', 'r', encoding='utf-8') as file:
with open('/home/xsl/code/ComfyUI/change_cloth/change_new.json', 'r', encoding='utf-8') as file:
prompt_text = file.read()
prompt = json.loads(prompt_text)
@@ -360,13 +359,13 @@ def save_base64_image(base64_str, prefix):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
unique_id = str(uuid.uuid4())[:8]
filename = f"{prefix}_{timestamp}_{unique_id}{file_ext}"
filepath = os.path.join('/home/szlc/code/ComfyUI/change_cloth/static/imgs', filename)
filepath = os.path.join('/home/xsl/code/ComfyUI/change_cloth/static/imgs', filename)
# 解码并保存图片
with open(filepath, 'wb') as f:
f.write(base64.b64decode(data))
input_filepath = os.path.join('/home/szlc/code/ComfyUI/input', filename)
input_filepath = os.path.join('/home/xsl/code/ComfyUI/input', filename)
# 解码并保存图片
with open(input_filepath, 'wb') as f:
f.write(base64.b64decode(data))
@@ -439,7 +438,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):
w,h = get_image_dimensions(f'/home/szlc/code/ComfyUI/input/{human_filename}')
w,h = get_image_dimensions(f'/home/xsl/code/ComfyUI/input/{human_filename}')
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:
@@ -449,18 +448,18 @@ def process_change_cloth(human_filename, cloth_filename, output_format, img_url,
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_image = Image.open(f'/home/xsl/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_jpg_path_name = f'/home/xsl/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}')
image = Image.open(f'/home/xsl/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}'
jpg_path_name = f'/home/xsl/code/ComfyUI/change_cloth/static/imgs/{jpg_name}'
image.save(jpg_path_name, quality=95)
@@ -510,13 +509,13 @@ def save_image_from_url(image_url):
# 获取原始图片格式
ext = get_file_extension(image_url)
filename = f"{uuid.uuid4()}{ext}"
filepath = os.path.join('/home/szlc/code/ComfyUI/change_cloth/static/imgs', filename)
filepath = os.path.join('/home/xsl/code/ComfyUI/change_cloth/static/imgs', filename)
with open(filepath, "wb") as f:
for chunk in response.iter_content(1024):
f.write(chunk)
input_filepath = os.path.join('/home/szlc/code/ComfyUI/input', filename)
input_filepath = os.path.join('/home/xsl/code/ComfyUI/input', filename)
shutil.copy(filepath, input_filepath)
return filename
@@ -655,7 +654,7 @@ def change_cloth_base64():
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}"
human_url = f"http://112.126.94.241:28888/static/imgs/{human_filename}"
# 保存服装图片
cloth_filename = save_base64_image(cloth_img, 'cloth')
@@ -663,7 +662,7 @@ def change_cloth_base64():
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}"
cloth_url = f"http://112.126.94.241:28888/static/imgs/{cloth_filename}"
data["human_url"] = human_url
data["cloth_url"] = cloth_url
@@ -672,7 +671,20 @@ def change_cloth_base64():
if not no2:
data['no2'] = False
return queueCall(data)
# 在内部调用第二个HTTP请求
try:
# 调用第二个API(可以是外部服务或自己的另一个端点)
response = requests.post('http://112.126.94.241:28888/do_change_cloth', json=data)
return Response(
response=response.content,
status=response.status_code,
headers=dict(response.headers)
)
except requests.exceptions.RequestException as e:
data['second_api_error'] = str(e)
return jsonify("error"), 500
if __name__ == '__main__':