-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
55 lines (40 loc) · 1.4 KB
/
main.py
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
import datetime
import io
import pytesseract
from PIL import Image
from flask import Flask, request, render_template, redirect, url_for, session
app = Flask(__name__)
# Secret key for sessions encryption
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
@app.route('/')
def home():
return render_template("index.html", title="Image Reader")
@app.route('/scanner', methods=['GET', 'POST'])
def scan_file():
if request.method == 'POST':
start_time = datetime.datetime.now()
image_data = request.files['file'].read()
scanned_text = pytesseract.image_to_string(Image.open(io.BytesIO(image_data)))
print("Found data:", scanned_text)
session['data'] = {
"text": scanned_text,
"time": str((datetime.datetime.now() - start_time).total_seconds())
}
return redirect(url_for('result'))
@app.route('/result')
def result():
if "data" in session:
data = session['data']
return render_template(
"result.html",
title="Result",
time=data["time"],
text=data["text"],
words=len(data["text"].split(" "))
)
else:
return "Wrong request method."
if __name__ == '__main__':
# Setup Tesseract executable path
pytesseract.pytesseract.tesseract_cmd = r'D:\TesseractOCR\tesseract'
app.run(debug=True)