-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
46 lines (38 loc) · 1.52 KB
/
app.py
File metadata and controls
46 lines (38 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from flask import Flask, render_template, request, jsonify
import pytesseract
from PIL import Image
import os
import logging
# Configure logging
logging.basicConfig(level=logging.DEBUG)
# Log Tesseract path
logging.debug(f"Tesseract path: {pytesseract.pytesseract.tesseract_cmd}")
logging.debug(f"TESSDATA_PREFIX: {os.environ.get('TESSDATA_PREFIX')}")
app = Flask(__name__)
# Set TESSDATA_PREFIX environment variable
os.environ["TESSDATA_PREFIX"] = "/usr/share/tesseract-ocr/4.00/tessdata"
@app.route("/")
def home():
return render_template("index.html")
@app.route("/upload", methods=["POST"])
def upload():
if "image" not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files["image"]
if file.filename == "":
return jsonify({"error": "No file selected"}), 400
try:
image = Image.open(file)
try:
# Extract text using Tesseract
extracted_text = pytesseract.image_to_string(image, lang="eng+hin+mar")
logging.debug("Text extraction successful!")
except Exception as e:
logging.error(f"Error during OCR processing: {e}")
extracted_text = "Error processing image."
return jsonify({"text": extracted_text})
except Exception as e:
logging.error(f"Error opening image: {e}")
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)), debug=False)