This repository was archived by the owner on Apr 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathocr.py
More file actions
88 lines (63 loc) · 2.44 KB
/
ocr.py
File metadata and controls
88 lines (63 loc) · 2.44 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import pathlib
import pdf2image
import pytesseract
from flask import Flask, jsonify, render_template, request
from langcodes import Language
from PIL import Image
from werkzeug.utils import secure_filename
__author__ = "Santhosh Thottingal <santhosh.thottingal@gmail.com>"
__source__ = "https://github.com/santhoshtr/tesseract-web"
app = Flask(__name__)
UPLOAD_FOLDER = "./static/uploads"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["MAX_CONTENT_LENGTH"] = 10 * 1024 * 1024
app.config["SUPPORTED_FORMATS"] = ["png", "jpeg", "jpg", "bmp", "pnm", "gif", "tiff", "webp", "pdf"]
def pdf_to_img(pdf_file):
return pdf2image.convert_from_path(pdf_file)
def ocr_core(image: Image, language="en"):
text = pytesseract.image_to_string(image, lang=Language.get(language).to_alpha3())
return text
def pdf_to_text(pdf_file_path: str, language="en") -> str:
texts = []
images = pdf_to_img(pdf_file_path)
for _pg, img in enumerate(images):
texts.append(ocr_core(img, language))
return "\n".join(texts)
def get_languages() -> dict:
languages = {}
alpha3codes = pytesseract.get_languages()
for code in alpha3codes:
language = Language.get(code)
languages[language.language] = language.autonym()
return languages
@app.route("/", methods=["GET", "POST"])
def index():
return render_template("index.html", languages=get_languages())
@app.route("/api/languages", methods=["GET"])
def listSupportedLanguages():
return jsonify(languages=get_languages())
@app.route("/api/ocr", methods=["POST"])
def ocr():
f = request.files["file"]
language = request.form.get("language", default="en")
# create a secure filename
filename = secure_filename(f.filename)
file_extension = pathlib.Path(filename).suffix.split(".")[1]
if file_extension not in app.config["SUPPORTED_FORMATS"]:
return jsonify(error="File format not supported")
# save file to /static/uploads
filepath = os.path.join(app.config["UPLOAD_FOLDER"], filename)
f.save(filepath)
if file_extension == "pdf":
# perform OCR on PDF
text = pdf_to_text(filepath, language)
else:
# perform OCR on the processed image
text = ocr_core(Image.open(filepath), language)
# remove the processed image
os.remove(filepath)
return jsonify(text=text)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host="0.0.0.0", port=port)