-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrontend.py
135 lines (101 loc) · 3.96 KB
/
frontend.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
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import PIL.ImageGrab as ImageGrab
from recogScript import *
pointer="black"
erase="white"
pointer_size=17.5
def pen(event):
root.configure(cursor="pencil")
global pointer
pointer="black"
pointer_size=17.5
def eraser(event):
root.configure(cursor="circle")
global pointer
pointer="white"
pointer_size=22
def clearScreen(event):
sketch.delete('all')
outbox.config(text="RESULT TEXT HERE ...")
def output(word):
if word == "" :
outbox.config(text="RESULT TEXT HERE ...")
else :
outbox.config(text=word)
def paint(event) :
x1, y1 = (event.x - 2), (event.y - 2)
x2, y2 = (event.x + 2), (event.y + 2)
sketch.create_oval(x1, y1, x2, y2, fill=pointer, outline = pointer, width = pointer_size)
def rec_drawing(event):
# Get the coordinate values of the canvas
x = root.winfo_rootx() + frame2.winfo_x()
y = root.winfo_rooty() + frame2.winfo_y()
x1 = x + frame2.winfo_width()
y1 = y + frame2.winfo_height()
# Screenshot the whole display and then crop out the canvas
img = ImageGrab.grab(all_screens= True).crop((x + 24 , y + 17, x1 - 28, y1 - 198))
res = recog(img)
output(res)
# defining the window
root = Tk()
root.title("Handwritten Text Recognition")
rooticon=PhotoImage(file= ".\icons\icons8-home-screen-100.png")
root.iconphoto(False, rooticon)
root.configure(background="#D2DAFF" )
Grid.rowconfigure(root,0,weight=1)
Grid.columnconfigure(root,0,weight=1)
# defining the main frame to contain all contents
mainframe=Frame(root, background='#D2DAFF')
mainframe.grid(column=0, row=0, sticky=(N,E,W,S))
# Frame to contain buttons
frame1= Frame(mainframe, background='#D2DAFF')
frame1.grid(column=0, row=0,sticky=(N,E,W,S))
# opening and resizing images of buttons
pen_img=Image.open(".\Buttons\pen-light-final.png")
pen_img_resized=pen_img.resize((271,53))
pen_img1=ImageTk.PhotoImage(pen_img_resized)
eraser_img=Image.open(".\Buttons\eraser-light-final.png")
eraser_img_resized=eraser_img.resize((271,53))
eraser_img1=ImageTk.PhotoImage(eraser_img_resized)
clear_img=Image.open(".\Buttons\clear-light-final.png")
clear_img_resized=clear_img.resize((271,53))
clear_img1=ImageTk.PhotoImage(clear_img_resized)
# creating buttons
pen_button = Button(frame1, image=pen_img1, borderwidth=0, cursor="arrow", background='#D2DAFF')
pen_button.bind("<Button-1>", pen)
eraser_button = Button(frame1, image=eraser_img1, borderwidth=0, cursor="arrow", background='#D2DAFF')
eraser_button.bind("<Button-1>", eraser)
clear_button = Button(frame1, image=clear_img1, borderwidth=0, cursor="arrow", background='#D2DAFF')
clear_button.bind("<Button-1>", clearScreen)
# placing buttons on grid
pen_button.grid(row=0, column=0, padx=120, pady=30)
eraser_button.grid(row=0, column=1, padx=120, pady=30)
clear_button.grid(row=0, column=2, padx=120, pady=30)
# Frame to contain the Canvas and Result text Box
frame2= Frame(mainframe, background='#D2DAFF')
frame2.grid(row=1, column=0,rowspan=2, sticky=(S))
# placing image under canvas
whiteboard=Image.open(".\Buttons\whiteboard.png")
whiteboard_resized=whiteboard.resize((1400,500))
whiteboard=ImageTk.PhotoImage(whiteboard_resized)
#label to display the image
label = Label(frame2, image = whiteboard)
label.grid(row=1, column=0)
sketch = Canvas(frame2)
sketch.grid(column=0, row=1,columnspan=3)
sketch.configure(background='White', height=465, width=1353, relief="raised")
sketch.bind("<B1-Motion>", paint)
sketch.bind("<ButtonRelease-1>", rec_drawing)
# Output Box
outbox = Label(frame2, text="RESULT TEXT HERE ...", font=("Kristen ITC",20,"bold"), fg='black', bg='#99E6EC', borderwidth= 5, relief= 'solid')
outbox.grid(row=2, column=0, sticky=(E,W), pady=30)
outbox.configure(height=3, width=16)
col_no=0
weight_val=1
button_list=[pen_button, eraser_button, clear_button]
for buttons in button_list:
Misc.grid_columnconfigure(mainframe,col_no,weight=weight_val)
col_no+=1
root.mainloop()