-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvoice.py
314 lines (262 loc) · 8.69 KB
/
Evoice.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import os
from tkinter.filedialog import askopenfilename
import pyttsx3
import PyPDF2
from gtts import gTTS
from googletrans import Translator, constants
from PIL import Image
import pytesseract
import cv2,sys
from playsound import playsound
import platform
import time
import shutil
#language function
def lang():
dic = {'ta': 'tamil','en': 'english', 'te': 'telugu','hi': 'hindi','fr': 'french','es': 'spanish','af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu', 'fil': 'Filipino', 'he': 'Hebrew'}
count = 1
words = []
for key ,item in dic.items():
words.append(item)
for i in words:
print("Enter No {} : {}".format(count,(i[0].upper()+i[1:])))
count+=1
lists = []
for key,item in dic.items():
lists.append(key)
lists = [None]+lists
checker = True
while checker:
num =int(input("\nselect the language using above option: "))
if num <= len(dic):
return lists[num]
checker =False
else:
print('\nWrong Number Entered!')
# speak function
def speak(text,lang):
translator = Translator()
translation = translator.translate(text,dest=lang)
text = translation.text
print(text)
tts = gTTS(text,lang=lang)
tts.save('evoice-speaker.mp3')
playsound('evoice-speaker.mp3')
os.remove('evoice-speaker.mp3')
# save function
def save(text,lang):
translator = Translator()
translation = translator.translate(text,dest=lang)
text = translation.text
tts = gTTS(text,lang=lang)
checker =True
while checker:
filename = str(input('Enter the filename: '))
if (os.path.isfile(filename+'.mp3') == False):
tts.save(filename+'.mp3')
checker =False
print('\nsucessfully saved')
else:
print('\nfile already exist!')
# main function
def audio():
if(platform.system() == "Windows"):
print(os.system('cls'))
else:
print(os.system('clear'))
global bye
#Printing exit Message For This Program
bye = """
------------------------------------------------------
|======================================================|
|========== Thank You ==========|
|======================================================|
------------------------------------------------------
"""
#Printing Welcome Message For This Program
print("""
------------------------------------------------------
|======================================================|
|========== Welcome To Evoice ==========|
|======================================================|
------------------------------------------------------
Select The Method
Enter 1 : Select pdf
Enter 2 : Select photo
Enter 3 : Custom Text
Enter 4 : Using camera
""")
try:
userInput = int(input("\nPlease Select An Above Option: "))
except ValueError:
print("\nHy! That's Not A Number")
time.sleep(2)
audio()
else:
print("\n")
if(userInput<=4):
LANG = lang()
# .............................................................
# pdf to speak
if (userInput == 1):
print('please select file ')
time.sleep(2)
pdf = askopenfilename()
pdfReader = PyPDF2.PdfFileReader(pdf)
pages=pdfReader.numPages
for p in range(pages):
page = pdfReader.getPage(p)
text = page.extractText()
cv2.destroyAllWindows()
print('''
Enter 1: To lisen evoice
Enter 2: To save mp3
''')
if p > 0:
speak('for next page',LANG)
checker =True
while checker:
option1 = int(input('\nPlease Select An Above Option: '))
if (option1 == 1):
speak(text,LANG)
checker = False
elif(option1 ==2):
save(text,LANG)
checker = False
else:
print('\nError: Wrong Number Entered!')
# .............................................................
# img to speak
elif (userInput == 2):
print('please select file ')
time.sleep(2)
# get image
image = Image.open(askopenfilename())
# get text from image
text = pytesseract.image_to_string(image, lang="eng")
# translate
translator = Translator()
translation = translator.translate(text=text,dest=LANG)
text = translation.text
print('''
Enter 1: To lisen evoice
Enter 2: To save mp3
''')
checker = True
while checker:
option2 = int(input('\nPlease Select An Above Option: '))
if (option2 == 1):
speak(text,LANG)
checker = False
elif(option2 ==2):
save(text,LANG)
checker = False
else:
print('\nError: Wrong Number Entered!')
# ..............................................................
# custom speak
elif(userInput == 3):
print('\n')
text = str(input('\nEnter the text you want to speak: '))
print('''
Enter 1: To lisen evoice
Enter 2: To save mp3
''')
checker = True
while checker:
option3 = int(input('\nPlease Select An Above Option: '))
if (option3 == 1):
speak(text,LANG)
checker = False
elif(option3 ==2):
save(text,LANG)
checker = False
else:
print('\nError: Wrong Number Entered!')
# .............................................................
elif(userInput == 4):
# create dir
cwd = os.getcwd()
dir = "CoNvErTeR_DiR"
parent_dir = cwd
path = os.path.join(parent_dir, dir)
# initialization
img_count =1
dir_count = 1
file_count = 0
# open camera
camera = cv2.VideoCapture(0)
while True:
_,frame = camera.read()
cv2.imshow('Camera',frame)
k = cv2.waitKey(1)
if k%256 == 27:
print('Escape hit,close')
break
elif k%256 == 32:
if dir_count == 1:
os.mkdir(path)
dir_count+=1
img_name = "{}/{}/img{}.jpg".format(cwd,dir,img_count)
cv2.imwrite(img_name,frame)
print('screenshot taken')
img_count+=1
camera.release()
cv2.destroyAllWindows()
# get path of the file_dir
file_dir = '{}/{}/'.format(cwd,dir)
width ,height = 0,0
cdir = "{}/".format(file_dir)
# count the files
for pat in os.listdir(cdir):
if os.path.isfile(os.path.join(cdir, pat)):
file_count += 1
print(file_count)
time.sleep(20)
i = 1
# loop using get text
while i <= file_count:
file_img = Image.open(file_dir +'img%.d.jpg' % i)
# time.sleep(10)
text = pytesseract.image_to_string(file_img, lang="eng")
text = text.strip()
i +=1
print('\n')
if i > 2:
speak('for next page',LANG)
print('''
Enter 1: To lisen evoice
Enter 2: To save mp3
''')
checker =True
while checker:
option1 = int(input('\nPlease Select An Above Option: '))
if (option1 == 1):
speak(text,LANG)
checker = False
elif(option1 ==2):
save(text,LANG)
checker = False
else:
print('\nError: Wrong Number Entered!')
shutil.rmtree(file_dir)
# check greater than 4 are lesser than 1
elif(userInput < 1 or userInput > 4):
print("\nPlease Enter Valid Option")
time.sleep(2)
audio()
audio()
# repeat main function
def runAgain():
runAgn = input("\nwant To Run Again Y/n: ")
if(runAgn.lower() == 'y'):
if(platform.system() == "Windows"):
print(os.system('cls'))
else:
print(os.system('clear'))
audio()
runAgain()
else:
# declared global bye
print(bye)
runAgain()