From dbaff73d1c1dbb3e67e4c4e6ba82621d4d6d23d0 Mon Sep 17 00:00:00 2001 From: wuzekai <3025054974@qq.com> Date: Tue, 8 Jul 2025 05:23:48 +0000 Subject: [PATCH] Update ocr.py --- ocr.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ocr.py b/ocr.py index 8725d7b..e43a505 100644 --- a/ocr.py +++ b/ocr.py @@ -84,4 +84,26 @@ def ocr_inference(): except subprocess.CalledProcessError as e: return jsonify({"error": f"OCR subprocess failed: {e}"}), 500 except Exception as e: - return jsonify({"error": f"Unexpected error: {e}"}), 500 \ No newline at end of file + return jsonify({"error": f"Unexpected error: {e}"}), 500 + +@ocr.route("/text/", methods=["GET"]) +def get_text(uuid_str): + if not is_valid_uuid(uuid_str): + return jsonify({"error": "Invalid UUID format"}), 400 + + json_file = os.path.join(output_dir, f"{uuid_str}.json") + if not os.path.exists(json_file): + return jsonify({"error": "JSON file not found"}), 404 + + try: + with open(json_file, "r", encoding="utf-8") as f: + data = json.load(f) + # 提取所有 text 字段 + text_list = [item.get("text", "") for item in data if "text" in item] + + return jsonify({ + "uuid": uuid_str, + "content": text_list + }), 200 + except Exception as e: + return jsonify({"error": f"Failed to read JSON file: {str(e)}"}), 500 \ No newline at end of file -- 2.22.0