-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (53 loc) · 2.26 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
61
62
63
64
65
# pip install flask
import os
from flask import Flask, render_template, request
import cv2
import pytesseract
import matplotlib.pyplot as plt
from PIL import Image
# import our OCR function
# from ocr_core import ocr_core
pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract"
def ocr_core(filename):
"""This function will handle the core OCR processing of images"""
text = pytesseract.image_to_string(Image.open(filename))
# We'll use Pillow's Image class to open the image and pytesseract to detect the string in the image
return text
# define a folder to store and later serve the images
UPLOAD_FOLDER = 'static/uploads/'
# allow files of a specific type
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
app = Flask(__name__)
# function to check the file extension
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# route and function to handle the home page
@app.route('/') # localhots:5000
def home_page():
return render_template('index.html')
# {% Python_syntax %}
# {{ data }}
# route and function to handle the upload page
@app.route('/upload', methods=['GET', 'POST'])
def upload_page():
if request.method == 'POST':
# check if there is a file in the request
if 'file' not in request.files:
return render_template('upload.html', msg='No file selected')
file = request.files['file']
# if no file is selected
if file.filename == '':
return render_template('upload.html', msg='No file selected')
if file and allowed_file(file.filename):
# if file is uploaded and its extension is .jpg, .png or .jpeg
# call the OCR function on it
extracted_text = ocr_core(file)
# extract the text and display it
return render_template('upload.html',
msg='Successfully processed',
extracted_text=extracted_text,
img_src=UPLOAD_FOLDER + file.filename)
elif request.method == 'GET':
return render_template('upload.html')
if __name__ == '__main__':
app.run()