-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.py
281 lines (235 loc) · 9.95 KB
/
program.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
# 201805051 - Utku Enes Baki
# Importing necessary libraries
from tkinter import *
import tkinter.font as tkFont
import imutils
import cv2
from tkinter import filedialog as fd
import pytesseract
from tkinter import messagebox
from PIL import ImageTk, Image
from langdetect import detect
import pygame
from translate import Translator
# Initialize pygame
pygame.init()
# Creating array data structures with empty lists for further storage
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
translations = []
originalSentences = []
# For click sound, I've imported pygame above
def play_mp3():
# Load and play the MP3 file
pygame.mixer.music.load('sound/mouse-click.mp3')
pygame.mixer.music.play()
#Selecting the image
def open_selected_image():
play_mp3()
global selectedImage, textImage, translatedImage
try:
file_path = fd.askopenfilename(title='Choose Photograph',
filetypes=(("Image files", "*.jpg;*.jpeg;*.png"), ("All files", "*.*")))
if not file_path:
raise ValueError("No file selected")
print("Path Is: " + file_path)
selectedImage = cv2.imread(file_path)
selectedImage = imutils.resize(selectedImage, width=600)
textImage = selectedImage.copy()
translatedImage = selectedImage.copy()
originalSentences.clear()
translations.clear()
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"Error: {e}")
#Optical Character Recognition (OCR)-x,y,w,h represents coordinates and dimensions of a bounding box around text (with cv2.rectangle())
def ocr_image():
play_mp3()
try:
data=pytesseract.image_to_data(selectedImage)
for z,a in enumerate(data.splitlines()):
if z!=0:
a=a.split()
if(len(a)==12):
x,y,w,h=int(a[6]),int(a[7]),int(a[8]),int(a[9])
#Select text with rectangles
cv2.rectangle(selectedImage,(x,y),(x+w,y+h),(0,255,0),1)
#Showing the OCR image
cv2.imshow('Image To OCR',selectedImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
except:
print("No image found.")
# Showing the text
def from_image_to_text():
play_mp3()
try:
config = r'--oem 3 --psm 6 -l eng+tur+deu+fra+ita+spa'
text = pytesseract.image_to_string(textImage, config=config)
if len(originalSentences) == 0:
originalSentences.append(text)
#Printing the text with messagebox
messagebox.showinfo("Target Text", f"Text on Image : \n{originalSentences[0]}")
# Global language variable declaration for further usage
global language
language = detect(originalSentences[0])
print("Sentences added to list")
print(f"Detected language is: {language}")
except:
print("No image selected")
# From text to translation
def text_to_translate():
play_mp3()
if len(originalSentences) == 0:
print("No text to translate")
else:
# Define the target languages and the translation to Turkish ("tr")
target_languages = ['en', 'de', 'fr', 'es', 'it']
target_language_tr = 'tr'
translations = []
for sentence in originalSentences:
for lang in target_languages:
# English
if (language == 'en'):
try:
# Translate the text to Turkish
translator = Translator(from_lang='en', to_lang=target_language_tr)
translated = translator.translate(sentence)
translations.append(translated)
break # Exit the inner loop after the first successful translation
except Exception as e:
print("Error:", e)
if len(translations) > 0:
break # Exit the outer loop after the first successful translation
# German
if (language == 'de'):
try:
# Translate the text to Turkish
translator = Translator(from_lang='de', to_lang=target_language_tr)
translated = translator.translate(sentence)
translations.append(translated)
break # Exit the inner loop after the first successful translation
except Exception as e:
print("Error:", e)
if len(translations) > 0:
break # Exit the outer loop after the first successful translation
# French
if (language == 'fr'):
try:
# Translate the text to Turkish
translator = Translator(from_lang='fr', to_lang=target_language_tr)
translated = translator.translate(sentence)
translations.append(translated)
break # Exit the inner loop after the first successful translation
except Exception as e:
print("Error:", e)
if len(translations) > 0:
break # Exit the outer loop after the first successful translation
# Spanish
elif (language == 'es'):
try:
# Translate the text to Turkish
translator = Translator(from_lang='es', to_lang=target_language_tr)
translated = translator.translate(sentence)
translations.append(translated)
break # Exit the inner loop after the first successful translation
except Exception as e:
print("Error:", e)
if len(translations) > 0:
break # Exit the outer loop after the first successful translation
# Italian
elif (language == 'it'):
try:
# Translate the text to Turkish
translator = Translator(from_lang='it', to_lang=target_language_tr)
translated = translator.translate(sentence)
translations.append(translated)
break # Exit the inner loop after the first successful translation
except Exception as e:
print("Error:", e)
if len(translations) > 0:
break # Exit the outer loop after the first successful translation
# For further language addition, this conditional state will be empty for now...
else:
pass
# Displaying the translated text in a messagebox
if len(translations) > 0:
# Join the translations into a single string
translations_text = '\n'.join(translations)
messagebox.showinfo("Translated Text", f"Text on Image:\n{originalSentences[0]}\n\nTranslation to Turkish:\n{translations_text}")
originalSentences.clear()
translations.clear()
#-------------------------------------------------- GUI --------------------------------------------------
#Sepya-#bfab50
#Facebook_Blue-#0394fc
window=Tk()
window.geometry('1200x580')
window.resizable(0, 0)
window.config(background="#ebe8e8")
window.title('Recognition Of Text From Image')
icon = PhotoImage(file='icons/favicon.png')
window.iconphoto(True,icon)
fontSettings = tkFont.Font(family='Helvetica',weight='bold')
label1 = Label(window,
text="Recognition of Text from Image",
font=('Arial', 25,'bold'),
fg='white',
bg='#5D5326',
relief=RAISED,
bd=10,
padx=10,
pady=10)
selectPhotoButton = Button(window,
text='Choose Photograph',
fg='white',
bg='#9e8d3e',
command=open_selected_image,
font=('Courier New', 24,'bold'),
width=20,
borderwidth=4)
ocrButton = Button(window,
text='Optical Character Recognition',
fg='white',
bg='#9e8d3e',
command=ocr_image,
font=('Courier New', 22,'bold'),
width=30,
borderwidth=4)
textButton = Button(window,
text='Show Text',
fg='white',
bg='#9e8d3e',
command=from_image_to_text,
font=('Courier New', 24,'bold'),
width=20,
borderwidth=4)
translatedButton = Button(window,
text='Translated Text',
fg='white',
bg='#9e8d3e',
command=text_to_translate,
font=('Courier New', 24,'bold'),
width=20,
borderwidth=4)
exitButton = Button(window,
text='Quit',
fg='white',
bg='#FF605C',
command=window.destroy,
font=('Courier New', 24,'bold'),
width=20,
borderwidth=4)
# Button placements
selectPhotoButton.place(x=660, y=110)
ocrButton.place(x=660, y=202)
textButton.place(x=660, y=290)
translatedButton.place(x=660, y=380)
exitButton.place(x=660, y=470)
label1.place(x=660, y=15)
# Importing the GUI page image
image2 = ImageTk.PhotoImage(Image.open("icons/picture.png"))
# Create a Label Widget to display the GUI image
label = Label(window,
image = image2,)
label.place(x=-2, y=-2)
window.mainloop()