diff --git a/ocr.py b/ocr.py index 8725d7ba955b4a250bff8a2bca7630757431fe80..e43a5052f8618ba8b8477ad94d196c86f7f16e23 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