From 7c8acfb4180d3ac34692213568f0285b62a5c8d3 Mon Sep 17 00:00:00 2001 From: Karl Hudgell Date: Sat, 23 Nov 2024 09:44:12 +0000 Subject: [PATCH] add local ocr support --- app.py | 26 ++++++++++++++++++++++- requirements.txt | Bin 532 -> 2712 bytes templates/add_account.html | 41 ++++++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/app.py b/app.py index 3674424..14d0786 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ # app.py -from flask import Flask, render_template, request, redirect, url_for, session, flash, send_file +from flask import Flask, render_template, request, redirect, url_for, session, send_file, jsonify from flask_caching import Cache import requests.auth import os @@ -10,6 +10,10 @@ import requests import base64 from flask import Flask from config import DevelopmentConfig +from paddleocr import PaddleOCR +from PIL import Image +import numpy as np + app = Flask(__name__) app.config.from_object( @@ -17,6 +21,8 @@ app.config.from_object( ) cache = Cache(app, config={"CACHE_TYPE": "SimpleCache"}) +ocr = PaddleOCR(use_angle_cls=True, lang='en') # Adjust language if needed + app.config['SESSION_COOKIE_SECURE'] = True # Only send cookie over HTTPS app.config['SESSION_COOKIE_HTTPONLY'] = True # Prevent JavaScript access app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # Adjust for cross-site requests @@ -156,5 +162,23 @@ def delete_account(): return redirect(url_for("user_accounts")) +@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/requirements.txt b/requirements.txt index abb21832f8cb6dd117614555512a034b66804a9f..ec69c2116dfd54a101cbbdf215749cfa040452c4 100644 GIT binary patch literal 2712 zcmb7`OK%fF5QOK9#7{B8HVNUt0Y%CI5edNoaf+PvYZJd>Z%CXU57alc?O6vnAj^*9 z=|^>SbAhaB)2saMr(t@TUZk*D>U@<&pO58f)u4yy%1MqA zIVR~*HWMKT#mt1qeIX`@=(9PWs^YE&i$X4hb0h}MMj&w~%ULg-w_T&Nvy7VPwuhH# zuBOR&;m~S?w-ddB`?+xSv$yLlU75?XQ8nFZp$6-j3}Nwj@E>HQyTV$tuoA}LGbX0a zeVmCY=899}W)7?eEN1e&Z|fps!Bq>@PO(S1ZaNCbuFm{MoR7+|N;k3`bWjir^f&VJ zWLWXkMi!&LmG6VzOx7a(I4CEqtWG-hB<)yEL6?7ohrPo8W1d`n5919 zfL}}RE){Dl1u)ZJ^?jOOy8e*SFu`Wb^RrQW6oJ0)rJX>`&JI<;A{((*UOH6n1=m&XqtiSs z*n8&KZdHy6DtF6U^102G?8;5+*<{ZZYwF~-D6Ekk%c!JivzPNX!s?sPbAzq#se<_L zzVL4S!j^Rko1azL_w*rMrcb#l@1fx7ix6&vZ=5ca_erl{vs*cv{7&cSiR~0U8|GTP zyLZZ8OC{{Do<5mgwjj3d3wkm3V_S3zPrjQ#&bIJ$+Y;u90XPkBodo9?)K@x(#zCV4+b&5lf;xcSTd9p~ll;l_nIW`n06HZ6O} zoh4V-)@Z;3AC4B!Al`Q74&Ad=<2l8>jl`=dE37d-w%k!VP hwU%L!XAWkOeKu@SS;6l5H^pFc^6V4$nOO`be*>L8icSCk delta 34 scmV+-0NwwX6_f-5|NfEs-jk>TNRw~_8k5QcMw6@rCX-wUB$HMOLJlqsvH$=8 diff --git a/templates/add_account.html b/templates/add_account.html index 271f27f..076cbce 100644 --- a/templates/add_account.html +++ b/templates/add_account.html @@ -6,16 +6,16 @@ Add Account - KTVManager - + -