save code
This commit is contained in:
+59
-25
@@ -506,6 +506,33 @@ def pushStr2Queue(key, data_str):
|
||||
else:
|
||||
logger.error(f"pushStr2Queue get lock error {data_str}")
|
||||
|
||||
def queueCall(data):
|
||||
key = str(uuid.uuid4())
|
||||
redis_conn = get_redis_conn()
|
||||
|
||||
task_data = {}
|
||||
task_data['key'] = key
|
||||
task_data['api'] = "/do_change_cloth"
|
||||
task_data['request'] = data
|
||||
logger.info(f"request do_change_cloth img:{data['human_url'][:64]}, output_format:{data['output_format']}")
|
||||
task_data_str = json.dumps(task_data)
|
||||
pushStr2Queue(key, task_data_str)
|
||||
start_time = datetime.now()
|
||||
result_key = f"result_{key}"
|
||||
result_status_key = f"result_status_code_{key}"
|
||||
while (datetime.now() - start_time).seconds < DEFAULT_TIMEOUT:
|
||||
if redis_conn.exists(result_key):
|
||||
result_str = redis_conn.get(result_key)
|
||||
return result_str, int(redis_conn.get(result_status_key)), {'Content-Type': 'application/json'}
|
||||
time.sleep(0.1)
|
||||
|
||||
logger.error(f"request hairColor time out ")
|
||||
return jsonify({
|
||||
'msg': f'Timeout after time out'
|
||||
, "state":-1, "data":""
|
||||
}), 408
|
||||
|
||||
|
||||
@app.route('/change_cloth', methods=['POST'])
|
||||
def change_cloth():
|
||||
"""从 URL 下载图片"""
|
||||
@@ -521,35 +548,42 @@ def change_cloth():
|
||||
|
||||
output_format = data.get('output_format')
|
||||
if not output_format:
|
||||
return jsonify({"state":-1, "error": "Missing 'cloth_url' parameter"}), 500
|
||||
return jsonify({"state":-1, "error": "Missing 'output_format' parameter"}), 500
|
||||
|
||||
return queueCall(data)
|
||||
|
||||
@app.route('/change_cloth_base64', methods=['POST'])
|
||||
def change_cloth_base64():
|
||||
# 获取参数
|
||||
data = request.get_json()
|
||||
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')
|
||||
output_format = data.get('output_format')
|
||||
if not output_format:
|
||||
return jsonify({"state":-1, "error": "Missing 'output_format' parameter"}), 500
|
||||
|
||||
key = str(uuid.uuid4())
|
||||
redis_conn = get_redis_conn()
|
||||
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
|
||||
|
||||
human_url = f"http://112.126.94.241:18888/static/imgs/{human_filename}"
|
||||
|
||||
task_data = {}
|
||||
task_data['key'] = key
|
||||
task_data['api'] = "/do_change_cloth"
|
||||
task_data['request'] = data
|
||||
logger.info(f"request do_change_cloth img:{data['human_url'][:64]}, output_format:{data['output_format']}")
|
||||
task_data_str = json.dumps(task_data)
|
||||
pushStr2Queue(key, task_data_str)
|
||||
timeout = DEFAULT_TIMEOUT
|
||||
start_time = datetime.now()
|
||||
result_key = f"result_{key}"
|
||||
result_status_key = f"result_status_code_{key}"
|
||||
while (datetime.now() - start_time).seconds < timeout:
|
||||
if redis_conn.exists(result_key):
|
||||
result_str = redis_conn.get(result_key)
|
||||
return result_str, int(redis_conn.get(result_status_key)), {'Content-Type': 'application/json'}
|
||||
time.sleep(0.1)
|
||||
|
||||
logger.error(f"request hairColor time out ")
|
||||
return jsonify({
|
||||
'msg': f'Timeout after {timeout} seconds, http time out'
|
||||
, "state":-1, "data":""
|
||||
}), 408
|
||||
# 保存服装图片
|
||||
cloth_filename = save_base64_image(cloth_img, 'cloth')
|
||||
if not cloth_filename:
|
||||
return jsonify({"ret":-1, 'msg': 'Failed to save cloth image'}), 500
|
||||
|
||||
cloth_url = f"http://112.126.94.241:18888/static/imgs/{cloth_filename}"
|
||||
|
||||
data["human_url"] = human_url
|
||||
data["cloth_url"] = cloth_url
|
||||
return queueCall(data)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user