From bcab963b991e8e255b8e3078350ce90a100f4f73 Mon Sep 17 00:00:00 2001 From: Karl Date: Mon, 14 Jul 2025 11:22:24 +0100 Subject: [PATCH] feat(ocr): make OCR functionality optional via configuration The OCR feature can now be enabled or disabled using a new `OCR_ENABLED` configuration flag. This allows for more flexible deployments where the OCR feature is not required, reducing the application's resource footprint and dependency overhead. The backend now conditionally initializes the OCR engine and its associated route. The frontend UI for OCR uploads is also rendered conditionally based on this setting. --- app.py | 41 ++++++++++++++++++++------------------ templates/add_account.html | 2 ++ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app.py b/app.py index 982e000..d5307b9 100644 --- a/app.py +++ b/app.py @@ -26,7 +26,8 @@ else: app.config.from_object(DevelopmentConfig) cache = Cache(app, config={"CACHE_TYPE": "SimpleCache"}) -ocr = PaddleOCR(use_angle_cls=True, lang='en') # Adjust language if needed +if app.config.get("OCR_ENABLED"): + ocr = PaddleOCR(use_angle_cls=True, lang='en') # Adjust language if needed app.config["SESSION_COOKIE_SECURE"] = not app.config["DEBUG"] app.config['SESSION_COOKIE_HTTPONLY'] = True # Prevent JavaScript access @@ -85,6 +86,7 @@ def home(): accounts=count, current_month_accounts=current_month_accounts, expired_accounts=expired_accounts, + ocr_enabled=app.config.get("OCR_ENABLED"), ) return render_template("index.html") @@ -163,7 +165,7 @@ def add_account(): return redirect(url_for("user_accounts")) return render_template("add_account.html") - return render_template("add_account.html") + return render_template("add_account.html", ocr_enabled=app.config.get("OCR_ENABLED")) @app.route("/accounts/delete", methods=["POST"]) @@ -187,23 +189,24 @@ def stream_names(): return jsonify(stream_names) -@app.route('/OCRupload', methods=['POST']) -def OCRupload(): - if 'image' not in request.files: - return jsonify({"error": "No image file found"}), 400 - # Get the uploaded file - file = request.files['image'] - try: - image = Image.open(file.stream) - image_np = np.array(image) - result = ocr.ocr(image_np) - # Extract text - extracted_text = [] - for line in result[0]: - extracted_text.append(line[1][0]) - return render_template("add_account.html", username=extracted_text[2], password=extracted_text[3]) - except Exception as e: - return jsonify({"error": str(e)}), 500 +if app.config.get("OCR_ENABLED"): + @app.route('/OCRupload', methods=['POST']) + def OCRupload(): + if 'image' not in request.files: + return jsonify({"error": "No image file found"}), 400 + # Get the uploaded file + file = request.files['image'] + try: + image = Image.open(file.stream) + image_np = np.array(image) + result = ocr.ocr(image_np) + # Extract text + extracted_text = [] + for line in result[0]: + extracted_text.append(line[1][0]) + return render_template("add_account.html", username=extracted_text[2], password=extracted_text[3]) + except Exception as e: + return jsonify({"error": str(e)}), 500 if __name__ == "__main__": app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"]) diff --git a/templates/add_account.html b/templates/add_account.html index 7408101..e4641a4 100644 --- a/templates/add_account.html +++ b/templates/add_account.html @@ -74,6 +74,7 @@ Add Account + {% if ocr_enabled %}

Load Details Via OCR

@@ -86,6 +87,7 @@ Load Details
+ {% endif %}