save code

This commit is contained in:
colomi
2025-07-21 21:55:06 +08:00
parent c37e0e473d
commit e53e65b6f4
4 changed files with 116 additions and 33 deletions
+104 -30
View File
@@ -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 bodysmile,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)