-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
60 lines (45 loc) · 1.7 KB
/
app.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
56
57
58
59
60
from flask import Flask, render_template, request
from werkzeug import secure_filename
import os
import sys
from PIL import Image
import pytesseract
import argparse
import cv2
__author__ = 'Rick Torzynski <[email protected]>'
__source__ = ''
app = Flask(__name__)
UPLOAD_FOLDER = './static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
# create a secure filename
filename = secure_filename(f.filename)
# save file to /static/uploads
filepath = os.path.join(app.config['UPLOAD_FOLDER'],filename)
f.save(filepath)
# load the example image and convert it to grayscale
image = cv2.imread(filepath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# apply thresholding to preprocess the image
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# apply median blurring to remove any blurring
gray = cv2.medianBlur(gray, 3)
# save the processed image in the /static/uploads directory
ofilename = os.path.join(app.config['UPLOAD_FOLDER'],"{}.png".format(os.getpid()))
cv2.imwrite(ofilename, gray)
# perform OCR on the processed image
text = pytesseract.image_to_string(Image.open(ofilename))
# remove the processed image
os.remove(ofilename)
return render_template("uploaded.html", displaytext=text, fname=filename)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')