save code

This commit is contained in:
Your Name
2026-03-08 16:02:37 +00:00
parent 339e7dc1f6
commit 01fa717f5f
40 changed files with 196 additions and 22 deletions
+78 -12
View File
@@ -66,7 +66,9 @@ def get_redis_conn():
"""获取 Redis 连接"""
return redis.Redis(connection_pool=redis_pool)
app = Flask(__name__)
app = Flask(__name__, static_folder='static', static_url_path='/static')
from flask_cors import CORS
CORS(app)
client = Ark(
# 此为默认路径,您可根据业务所在地域进行配置
@@ -225,10 +227,16 @@ def generate_from_face(human_name, sex_girl):
prompt["93"]["inputs"]["image"] = human_name
# if sex_girl:
# prompt["6"]["inputs"]["text"] = "asian girl,knee-length shot ,model pose,hands are at the sides of the body,smile,wearing black tube top and micro skirt,simple ,white background,32k,high detail,and face is illuminated by soft side light and natural light. The sunlight spills over the white wall behind."
# else:
# prompt["6"]["inputs"]["text"] = "asian man,tall,knee-length shot,model pose,hands are at the sides of the body,smile,wearing black short pants,simple white background,The sunlight spills over the white wall behind,32k."
if sex_girl:
prompt["6"]["inputs"]["text"] = "asian girl,knee-length shot ,model pose,hands are at the sides of the body,smile,wearing black tube top and micro skirt,simple ,white background,32k,high detail,and face is illuminated by soft side light and natural light. The sunlight spills over the white wall behind."
prompt["6"]["inputs"]["text"] = "asian girl,knee-length shot,model pose,hands are at the sides of the body,smile,wearing black tube top and micro skirt,solid very dark gray background,32k,high detail,and face is illuminated by soft side light and natural light."
else:
prompt["6"]["inputs"]["text"] = "asian man,tall,knee-length shot,model pose,hands are at the sides of the body,smile,wearing black short pants,simple white background,The sunlight spills over the white wall behind,32k."
prompt["6"]["inputs"]["text"] = "asian man,tall,knee-length shot,model pose,hands are at the sides of the body,smile,shirtless,wearing black short pants,very dark gray background,solid dark gray background,32k,"
#out put name
out_img_name = str(uuid.uuid4())[:8]
@@ -639,9 +647,61 @@ def process_change_banana(human_filename, cloth_filename, output_format):
"msg":"failure"
})
def process_change_cloth(human_filename, cloth_filename, output_format, img_url, human_url, no2, tuodi, kuzi_url, cloth_len):
def process_change_cloth(human_filename, cloth_filename, output_format, img_url, human_url, no2, tuodi, kuzi_url, cloth_len, kuzi_img=None):
w,h = get_image_dimensions(f'{APP_ROOT}/../input/{human_filename}')
if kuzi_url:
print(f'换裤子 kuzi_url:{kuzi_url}')
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
new_data = {
"model_image": image_to_base64(f'{APP_ROOT}/../input/{human_filename}', False),
"shirt_image": image_to_base64(f'{APP_ROOT}/../input/{cloth_filename}', False),
"pants_image": kuzi_img if kuzi_img else image_to_base64(save_image_from_url(kuzi_url), False),
}
response = requests.post(f'http://117.50.44.174:47698/try-on', json=new_data)
result_image_data = response.json().get("result_image", "")
if result_image_data:
print(f"result_image_data: {result_image_data[:30]}...") # 打印前30个字符以验证数据格式
if 'base64' in output_format:
return jsonify({
"ret":0,
"state": 0,
"msg":"success",
"is_girl":sex_girl,
"second_step_data":new_data["model_image"],
"first_step_data":new_data["model_image"],
"data":result_image_data
})
else:
result_image_bytes = base64.b64decode(result_image_data.split(',')[1])
result_image_stream = BytesIO(result_image_bytes)
image = Image.open(result_image_stream)
jpg_name = f"{uuid.uuid4()}.png"
jpg_path_name = f'{APP_ROOT}/static/imgs/{jpg_name}'
image.save(jpg_path_name, quality=90)
upload_to_oss(jpg_path_name, jpg_name)
https_url = f'https://xiangsilian.oss-cn-beijing.aliyuncs.com/{jpg_name}'
return jsonify({
"ret":0,
"state": 0,
"msg":"success",
"second_url":https_url,
"first_url":https_url,
"is_girl":sex_girl,
"url":https_url
})
out_put_name, out_human_name, is_girl, msg = change(human_filename, cloth_filename, w, h, img_url, human_url, no2, tuodi, cloth_len)
if out_put_name == None:
print(f'Failed to change cloth {msg}')
@@ -748,7 +808,13 @@ def save_image_from_url(image_url):
except Exception as e:
print(f"Error saving image from URL: {e}")
return None
@app.route('/')
def index():
"""返回主页"""
from flask import send_from_directory
return send_from_directory('static', 'index.html')
@app.route('/do_change_cloth', methods=['POST'])
def do_change_cloth():
@@ -801,7 +867,7 @@ def do_change_cloth():
if 'cloth_len' in data:
cloth_len = data['cloth_len']
try:
return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url, human_url, no2, tuodi, kuzi_url, cloth_len)
return process_change_cloth(human_filename, cloth_filename, output_format, cloth_url, human_url, no2, tuodi, kuzi_url, cloth_len, data.get("kuzi_img") )
except Exception as e:
traceback.print_exc()
print(f"错误详情:{e}")
@@ -912,7 +978,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:{base64_test_port}/static/imgs/{human_filename}"
human_url = f"http://117.50.44.174:{base64_test_port}/static/imgs/{human_filename}"
# 保存服装图片
cloth_filename = save_base64_image(cloth_img, 'cloth')
@@ -920,7 +986,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:{base64_test_port}/static/imgs/{cloth_filename}"
cloth_url = f"http://117.50.44.174:{base64_test_port}/static/imgs/{cloth_filename}"
data["human_url"] = human_url
data["cloth_url"] = cloth_url
@@ -928,8 +994,8 @@ def change_cloth_base64():
kuzi_img = data.get('kuzi_img')
if kuzi_img:
kuzi_filename = save_base64_image(kuzi_img, 'kuzi')
data['kuzi_img'] = None
kuzi_url = f"http://112.126.94.241:{base64_test_port}/static/imgs/{kuzi_filename}"
# data['kuzi_img'] = None
kuzi_url = f"http://117.50.44.174:{base64_test_port}/static/imgs/{kuzi_filename}"
data['kuzi_url'] = kuzi_url
no2 = data.get('no2')
@@ -953,7 +1019,7 @@ def change_cloth_base64():
# 在内部调用第二个HTTP请求
try:
# 调用第二个API(可以是外部服务或自己的另一个端点)
response = requests.post(f'http://112.126.94.241:{base64_test_port}/do_change_cloth', json=data)
response = requests.post(f'http://117.50.44.174:{base64_test_port}/do_change_cloth', json=data)
return Response(
response=response.content,
@@ -967,4 +1033,4 @@ def change_cloth_base64():
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8888, debug=True)
app.run(host="0.0.0.0", port=base64_test_port, debug=True)