add local ocr support

This commit is contained in:
Karl Hudgell 2024-11-23 09:44:12 +00:00
parent 71f08f01d6
commit 7c8acfb418
3 changed files with 50 additions and 17 deletions

26
app.py
View File

@ -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"])

Binary file not shown.

View File

@ -6,16 +6,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Account - KTVManager</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}" />
<style>
/* Hide the spinner by default */
#loadingSpinner {
#loadingSpinner,
#ocrLoadingSpinner {
display: none;
}
</style>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="/">KTVManager</a>
@ -52,18 +52,17 @@
</div>
<!-- Main Content -->
<main div class="container mt-5">
<main class="container mt-5">
<h1>Add Account</h1>
<br>
<div>
<form action="/accounts/add" method="POST" onsubmit="showLoading()">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" required>
<input type="text" class="form-control" id="username" name="username" value="{{ username }}" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="text" class="form-control" id="password" name="password" required>
<input type="text" class="form-control" id="password" name="password" value="{{ password }}" required>
</div>
<div class="form-group">
<label for="stream">Stream Name</label>
@ -74,24 +73,34 @@
<span id="buttonText">Add Account</span>
</button>
</form>
</div>
</main>
<hr>
<h4>Load Details Via OCR</h2>
<form action="/OCRupload" method="POST" enctype="multipart/form-data" onsubmit="showLoadingOCR()">
<div class="form-group">
<label for="image">Select Image</label>
<input type="file" class="form-control-file" id="image" name="image" accept="image/*" required>
</div>
<button type="submit" class="btn btn-success" id="ocrButton">
<span class="spinner-border spinner-border-sm" id="ocrLoadingSpinner" role="status" aria-hidden="true"></span>
<span id="ocrButtonText">Load Details</span>
</button>
</form>
</main>
<footer class="bg-dark text-white text-center py-3 mt-5">
<p></p>
</footer>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.0.7/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
function showLoading() {
// Disable the button to prevent multiple submissions
document.getElementById("submitButton").disabled = true;
// Show spinner and change button text
document.getElementById("loadingSpinner").style.display = "inline-block";
document.getElementById("buttonText").textContent = "Working...";
}
function showLoadingOCR() {
document.getElementById("ocrButton").disabled = true;
document.getElementById("ocrLoadingSpinner").style.display = "inline-block";
document.getElementById("ocrButtonText").textContent = "Processing...";
}
</script>
</body>
</html>