save code

This commit is contained in:
xsl
2025-08-09 21:41:35 +08:00
parent a592c4a552
commit b92fd5e782
2 changed files with 24 additions and 22 deletions
+20 -19
View File
@@ -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/xsl/code/ComfyUI/change_cloth/change_new_debug.json', 'r', encoding='utf-8') as file:
with open(f'{APP_ROOT}/change_new_debug.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/xsl/code/ComfyUI/change_cloth/xiezhen_girl.json', 'r', encoding='utf-8') as file:
with open(f'{APP_ROOT}/xiezhen_girl.json', 'r', encoding='utf-8') as file:
prompt_text = file.read()
prompt = json.loads(prompt_text)
@@ -263,8 +263,8 @@ def change(human_name, cloth_name, c_width, c_height, cloth_url, human_url, no2)
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/xsl/code/ComfyUI/output', takeoff_file_name)
takeoff_file_path_name_input = os.path.join('/home/xsl/code/ComfyUI/input', takeoff_file_name)
takeoff_file_path_name = os.path.join(f'{APP_ROOT}/../output', takeoff_file_name)
takeoff_file_path_name_input = os.path.join(f'{APP_ROOT}/../input', takeoff_file_name)
shutil.copy(takeoff_file_path_name, takeoff_file_path_name_input)
else:
#生成写真
@@ -273,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/xsl/code/ComfyUI/output', generate_name)
generate_name_file_path_name_input = os.path.join('/home/xsl/code/ComfyUI/input', generate_name)
generate_name_file_path_name = os.path.join(f'{APP_ROOT}/../output', generate_name)
generate_name_file_path_name_input = os.path.join(f'{APP_ROOT}/../input', generate_name)
shutil.copy(generate_name_file_path_name, generate_name_file_path_name_input)
queue = requests.get("http://localhost:8188/queue").json()
@@ -282,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/xsl/code/ComfyUI/change_cloth/change_new.json', 'r', encoding='utf-8') as file:
with open(f'{APP_ROOT}/change_new.json', 'r', encoding='utf-8') as file:
prompt_text = file.read()
prompt = json.loads(prompt_text)
@@ -359,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/xsl/code/ComfyUI/change_cloth/static/imgs', filename)
filepath = os.path.join(f'{APP_ROOT}/static/imgs', filename)
# 解码并保存图片
with open(filepath, 'wb') as f:
f.write(base64.b64decode(data))
input_filepath = os.path.join('/home/xsl/code/ComfyUI/input', filename)
input_filepath = os.path.join(f'{APP_ROOT}/../input', filename)
# 解码并保存图片
with open(input_filepath, 'wb') as f:
f.write(base64.b64decode(data))
@@ -438,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/xsl/code/ComfyUI/input/{human_filename}')
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)
if out_put_name == None:
@@ -448,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/xsl/code/ComfyUI/output/{out_human_name}')
out_human_image = Image.open(f'{APP_ROOT}/../output/{out_human_name}')
out_human_jpg_name = out_human_name.replace(".png", ".jpg")
out_human_jpg_path_name = f'/home/xsl/code/ComfyUI/change_cloth/static/imgs/{out_human_jpg_name}'
out_human_jpg_path_name = f'{APP_ROOT}/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/xsl/code/ComfyUI/output/{out_put_name}')
image = Image.open(f'{APP_ROOT}/../output/{out_put_name}')
jpg_name = out_put_name.replace(".png", ".jpg")
jpg_path_name = f'/home/xsl/code/ComfyUI/change_cloth/static/imgs/{jpg_name}'
jpg_path_name = f'{APP_ROOT}/static/imgs/{jpg_name}'
image.save(jpg_path_name, quality=95)
@@ -509,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/xsl/code/ComfyUI/change_cloth/static/imgs', filename)
filepath = os.path.join(f'{APP_ROOT}/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/xsl/code/ComfyUI/input', filename)
input_filepath = os.path.join(f'{APP_ROOT}/../input', filename)
shutil.copy(filepath, input_filepath)
return filename
@@ -635,6 +635,7 @@ def change_cloth():
@app.route('/change_cloth_base64', methods=['POST'])
def change_cloth_base64():
remote_port = 28888
# 获取参数
data = request.get_json()
if not data:
@@ -654,7 +655,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:28888/static/imgs/{human_filename}"
human_url = f"http://112.126.94.241:{remote_port}/static/imgs/{human_filename}"
# 保存服装图片
cloth_filename = save_base64_image(cloth_img, 'cloth')
@@ -662,7 +663,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:28888/static/imgs/{cloth_filename}"
cloth_url = f"http://112.126.94.241:{remote_port}/static/imgs/{cloth_filename}"
data["human_url"] = human_url
data["cloth_url"] = cloth_url
@@ -674,7 +675,7 @@ def change_cloth_base64():
# 在内部调用第二个HTTP请求
try:
# 调用第二个API(可以是外部服务或自己的另一个端点)
response = requests.post('http://112.126.94.241:28888/do_change_cloth', json=data)
response = requests.post('http://112.126.94.241:{remote_port}/do_change_cloth', json=data)
return Response(
response=response.content,
+4 -3
View File
@@ -307,7 +307,7 @@ def main_worker():
serverindex = 0
def regServer(instance, ip):
global serverindex
registerGpuServer(f"s{serverindex}_1_{instance}", f"http://{ip}:8888", True)
registerGpuServer(f"s{serverindex}_1_{instance}", f"http://{ip}", True)
serverindex += 1
@@ -316,7 +316,8 @@ if __name__ == '__main__':
print(f"[Worker] Starting with PID: {os.getpid()}")
redis_conn.set(GPU_SERVER_LIST, "[]")
regServer("一号", "192.168.101.118")
regServer("", "192.168.101.221")
# regServer("5090", "112.126.94.241:28888")
regServer("", "192.168.101.118:8888")
# regServer("二号", "192.168.101.221:8888")
main_worker()