-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (39 loc) · 1.32 KB
/
app.py
File metadata and controls
48 lines (39 loc) · 1.32 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
from flask import Flask, render_template, request
import pytesseract as tess
#tess.pytesseract.tesseract_cmd = r'C:\Users\<Username>\AppData\Local\Programs\Tesseract-OCR\tesseract.exe'
tess.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract'
import requests
from PIL import Image
app = Flask(__name__)
def image_to_text(url):
img = Image.open(requests.get(url, stream=True).raw)
text = tess.image_to_string(img)
return text
@app.route('/')
def root():
#Homepage to take input
return render_template("index.html")
@app.route('/result', methods=["POST", "GET"])
def result():
url = ""
results = ""
#getting url ussing a get request
if request.method == "GET":
url = request.args.get("url"," ")
else:
#Getting from the POST method
url = request.form.get("url")
#Error Handling
if url == " " or not url :
return render_template("error.html", error = "No url Found!")
final_text = []
try:
text = image_to_text(url)
results = text.split('\n')
for r in results:
if r != "" and r !=" " and r !=" " and r !=" ":
final_text.append(r)
except:
return render_template("error.html", error = "Invalid Url")
print(text)
return render_template("result.html", result = text, url = url)