72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
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():
|
|
|
|
data = request.get_json()
|
|
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
|
|
|
|
human_img = data.get('human_img')
|
|
cloth_img = data.get('cloth_img')
|
|
|
|
if not human_img or not cloth_img:
|
|
return jsonify({"ret": -1, "state": -1, "msg": "Both human_img and cloth_img are required"}), 400
|
|
|
|
kuzi_img = data.get('kuzi_img')
|
|
|
|
try:
|
|
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_url')
|
|
|
|
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=28889, debug=True)
|