This repository has been archived by the owner on Nov 26, 2019. It is now read-only.
forked from ishita27/Printed-Text-recognition-and-conversion
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtkinter_gui.py
135 lines (97 loc) · 3.74 KB
/
tkinter_gui.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
import sys
import cv2
import os
import time
from PIL import Image as Img
from PIL import ImageTk
from tkinter import filedialog
from ocr import perform_ocr
import gtts as gTTS
from tkinter import *
import tkinter.messagebox
# from tkinter import Tk, Label, Button, BOTTOM, LEFT, X
import webbrowser
def openHelp():
webbrowser.open('https://github.com/NJACKWinterOfCode/Printed-Text-recognition-and-conversion/blob/master/README.md')
class Window:
global imageWindow
global path
def __init__(self, master):
master.title("Optical Character Recognition")
master.configure(background='black')
master.geometry("800x800+500+300")
# image window
self.imageWindow = None
# input image path
self.path = ''
# Load image button
self.load_button = Button(master, text="Load image", command=self.select_image, bg="orange", relief=RAISED)
self.load_button.pack(side=TOP, anchor=E)
# Extract Text button
self.extract_button = Button(master, text="Extract text", command=self.extract_text, bg="orange", relief=RAISED)
self.extract_button.pack(side=TOP, anchor=E, expand="yes")
# Play Text button
self.playText_button = Button(master, text="Play text", bg="orange", relief=RAISED)
self.playText_button.pack(side=TOP, anchor=E)
# Quit Button
self.quit_button = Button(master, text="Quit", command=master.quit, bg="orange", relief=RAISED)
self.quit_button.pack(side=TOP, anchor=E, expand="yes")
# function to extract text from input image
def extract_text(self):
perform_ocr(self.path)
# pop-up to show extraction is completed and text file is saved
tkinter.messagebox.showinfo('Success', 'Text file saved')
# function to select and load image
def select_image(self):
# get image path
self.path = filedialog.askopenfilename()
if len(self.path) > 0:
image = cv2.imread(self.path) #read image
image = cv2.resize(image, (500, 600)) #resize image
cv2.imwrite("original_image.jpg", image) #save original image
# swap channels
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert image to PIL format
image = Img.fromarray(image)
# convert image to ImageTk format
image = ImageTk.PhotoImage(image)
# if the image window is None, initialize it
def __init__(self, master):
# frame = Frame(master, width=600, height=600)
# frame.pack()
# imageWindow = Label()
# imageWindow.pack(side=TOP, padx=10, pady=10, anchor=S)
master.title("Optical Character Recognition")
master.configure(background='black')
master.geometry("800x800+500+300")
self.imageWindow = None
self.load_button = Button(master, text="Load image", command=self.select_image, bg="orange", relief=RAISED)
self.load_button.pack(side=TOP, anchor=E)
self.extract_button = Button(master, text="Extract text", bg="orange", relief=RAISED)
self.extract_button.pack(side=TOP, anchor=E, expand="yes")
self.playText_button = Button(master, text="Play text", bg="orange", relief=RAISED)
self.playText_button.pack(side=TOP, anchor=E)
self.quit_button = Button(master, text="Quit", command=master.quit, bg="orange", relief=RAISED)
self.quit_button.pack(side=TOP, anchor=E, expand="yes")
def select_image(self):
path = filedialog.askopenfilename()
# imageWindow
if len(path) > 0:
image = cv2.imread(path)
image = cv2.resize(image, (500, 600))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = Img.fromarray(image)
image = ImageTk.PhotoImage(image)
if self.imageWindow is None:
self.imageWindow = Label(image=image)
self.imageWindow.image = image
self.imageWindow.pack(side=LEFT)
else:
self.imageWindow.configure(image=image)
self.imageWindow.image = image
root = Tk()
menu = Menu(root)
root.config(menu=menu)
menu.add_command(label="Help", command=openHelp)
b = Window(root)
root.mainloop()