-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
102 lines (89 loc) · 2.88 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from flask import Flask, render_template, jsonify, request
import os
import PyPDF2
from googletrans import Translator
import googletrans
import cv2
import pytesseract
from gtt import nischay
app = Flask(__name__)
#checking if images folder exist
#if not then make one
if not os.path.isdir(os.path.join(os.getcwd(),'images')):
os.mkdir(os.path.join(os.getcwd(),'images'))
#setting congfig of tesseract
custom_config = r'--oem 3 --psm 6'
#setting the correct path
#for Windows find it in the Programmne files
#This path is given for heroku dyno
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
#homepage route
@app.route("/", methods = ['POST', 'GET'])
def front():
if request.method=='POST':
#importing text from thr form
text = request.form['text-to-translate']
#importing lang from the form
lang = request.form['select-language']
#using google trans to convert
translator = Translator()
translated = translator.translate(text,dest=lang)
text=translated.text
#calling nischay function
#from gtt.py
#for converting text to speech and playing it
nischay(text,lang)
return render_template('index.html', text=text)
else:
return render_template('index.html')
# display route
@app.route("/display", methods=['POST'])
def display():
#requasting form data
text=request.form['lan']
file = request.files['myfile']
pathOfFile = os.path.join(os.getcwd(), 'images', file.filename)
file.save(pathOfFile)
#using PyPDF2 to read pdf
a=PyPDF2.PdfFileReader(os.path.join(os.getcwd(),'images',file.filename))
stri=""
#counting the pages in pdf
count_page=a.getNumPages()
if count_page>10:
for i in range(0,5):
stri+=a.getPage(i).extractText()
else:
for i in range(0,count_page):
stri+=a.getPage(i).extractText()
m=text
#google trans to convert
translator = Translator()
translated = translator.translate(stri,dest=m)
p=translated.text
app_data={
"TRANSLATED":p
}
#removing the pdf
os.remove(pathOfFile)
return render_template('display.html',app_data=app_data)
@app.route("/nischay", methods=["POST"])
def img_text():
#requesting form data
image=request.files['myfile']
w=request.form['lan']
pathofFile = os.path.join(os.getcwd(), 'images', image.filename)
image.save(pathofFile)
#using opencv to read the image
img = cv2.imread(pathofFile)
# text in the images converted to text using TESSERACT-OCR
text=pytesseract.image_to_string(img)
translator=Translator()
translation1=translator.translate(text,dest=w)
app_data={
"trn":translation1.text
}
os.remove(pathofFile)
return render_template('nischay.html',app_data=app_data)
#Running the Flask app
if __name__=="__main__":
app.run(debug=False)