From c37e0e473dd861de8ae1bbf0ea6ebcd98134b958 Mon Sep 17 00:00:00 2001 From: colomi <1421901449@qq.com> Date: Sun, 20 Jul 2025 22:33:25 +0800 Subject: [PATCH] save code --- change_app.py | 84 +++++++--- config.py | 1 + monitor.py | 6 +- static/monitor.html | 382 ++++++++++++++++++++++++++++++++++++++++++++ worker.py | 13 +- 5 files changed, 451 insertions(+), 35 deletions(-) create mode 100644 static/monitor.html diff --git a/change_app.py b/change_app.py index 12d78d3..518798c 100644 --- a/change_app.py +++ b/change_app.py @@ -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__': diff --git a/config.py b/config.py index 55f56c6..7e6c19f 100644 --- a/config.py +++ b/config.py @@ -16,6 +16,7 @@ KEY_QUEUE_LOCK_NAME = 'KEY_QUEUE_LOCK_NAME' SERVER_LIST_LOCK_NAME = 'SERVER_LIST_LOCK_NAME' SERVER_LIST_LOG_LOCK_NAME = 'SERVER_LIST_LOG_LOCK_NAME' + # 创建 logger logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) # 设置 logger 的级别 diff --git a/monitor.py b/monitor.py index 88740a5..7940f54 100644 --- a/monitor.py +++ b/monitor.py @@ -72,13 +72,13 @@ def get_server_state(): @monitor.route('/') def serve_index(): - return send_from_directory('static', 'index.html') + return send_from_directory('static', 'monitor.html') if __name__ == '__main__': # 启动Flask应用,启用多线程处理 monitor.run( host='0.0.0.0', - port=5001, - threaded=True, # 启用多线程处理并发请求 + port=8018, + threaded=False, # 启用多线程处理并发请求 debug=False # 生产环境应设置为False ) diff --git a/static/monitor.html b/static/monitor.html new file mode 100644 index 0000000..f45055a --- /dev/null +++ b/static/monitor.html @@ -0,0 +1,382 @@ + + +
+ + +