save code

This commit is contained in:
Your Name
2026-03-12 12:37:19 +00:00
parent 8221c8be65
commit 80404217aa
5 changed files with 503 additions and 65 deletions
+57 -63
View File
@@ -1,77 +1,71 @@
import requests
from flask import Flask, jsonify, request, send_from_directory
app = Flask(__name__, static_folder='static')
@app.route('/')
def index():
return send_from_directory(app.static_folder, 'index.html')
@app.route('/static/<path:filename>')
def static_files(filename):
return send_from_directory(app.static_folder, filename)
@app.route('/change_cloth_base64', methods=['POST'])
def change_cloth_base64():
log_message("change_cloth_base64 called")
# 获取参数
data = request.get_json()
print(f"change_cloth_base64 input data:{data}")
print(f"change_cloth_base64 input data keys:{list(data.keys()) if data else None}")
if not data:
return jsonify({"ret":-1, "state":-1, 'msg': 'No JSON data provided'}), 400
return jsonify({"ret": -1, "state": -1, "msg": "No JSON data provided"}), 400
human_img = data.get('human_img')
cloth_img = data.get('cloth_img')
output_format = data.get('output_format')
if not output_format:
return jsonify({"state":-1, "error": "Missing 'output_format' parameter"}), 500
if not human_img or not cloth_img:
return jsonify({"ret":-1, 'msg': 'Both human_img and cloth_img are required'}), 400
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://117.50.44.174:{base64_test_port}/static/imgs/{human_filename}"
# 保存服装图片
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://117.50.44.174:{base64_test_port}/static/imgs/{cloth_filename}"
data["human_url"] = human_url
data["cloth_url"] = cloth_url
return jsonify({"ret": -1, "state": -1, "msg": "Both human_img and cloth_img are required"}), 400
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://117.50.44.174:{base64_test_port}/static/imgs/{kuzi_filename}"
data['kuzi_url'] = kuzi_url
no2 = data.get('no2')
if not no2:
data['no2'] = False
tuodi = data.get('tuodi')
if not tuodi:
data['tuodi'] = False
if not data.get('suit'):
data['suit'] = False
if data.get('cloth_len'):
print(f'客户端传入了衣服长度: {data['cloth_len']}')
else:
print('客户端传入了衣服长度,需要ai 判断')
# 在内部调用第二个HTTP请求
try:
# 调用第二个API(可以是外部服务或自己的另一个端点)
response = requests.post(f'http://127.0.0.1:{base64_test_port}/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 kuzi_img:
payload = {
'model_image': human_img,
'shirt_image': cloth_img,
'pants_image': kuzi_img,
}
response = requests.post('http://127.0.0.1:12223/try-on', json=payload, timeout=360)
else:
payload = {
'model_image': human_img,
'shirt_image': cloth_img,
}
response = requests.post('http://127.0.0.1:12222/try-on', json=payload, timeout=360)
response.raise_for_status()
result = response.json()
result_image = result.get('result_image')
if not result_image:
return jsonify({"ret": -1, "state": -1, "msg": "Backend returned no image"}), 500
except requests.exceptions.Timeout:
return jsonify({"ret": -1, "state": -1, "msg": "Backend service timeout"}), 504
except requests.exceptions.ConnectionError:
return jsonify({"ret": -1, "state": -1, "msg": "Cannot connect to backend service"}), 502
except requests.exceptions.HTTPError as e:
return jsonify({"ret": -1, "state": -1, "msg": f"Backend error: {e}"}), 502
return jsonify({
"ret": 0,
"state": 0,
"msg": "success",
"data": result_image,
})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=28888, debug=False)