-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (93 loc) · 3.59 KB
/
main.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
from fileinput import filename
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.tix import IMAGE
from PIL import ImageTk, Image
from matplotlib import image
from img_to_text import image_to_text, find_closest_subject, find_grade
import config
import numpy as np
from tkinter.messagebox import askyesno, showerror, showinfo
def load_image_tk(filename, size):
img = Image.open(filename).resize(size)
img_tk = ImageTk.PhotoImage(img)
return img_tk
IMAGE_SIZE = (400, 600)
FONT = ('Arial', 25)
root = Tk()
root.title("Book Detection")
root.geometry("1200x900")
filename = ''
closest_sub = 'Không xác định'
grade = 'Không xác định'
def btn_choose_action():
global filename, closest_sub, grade
filename = askopenfilename()
if not filename:
return
new_img_tk = load_image_tk(filename, IMAGE_SIZE)
lbl_image.config(image=new_img_tk)
lbl_image.image = new_img_tk
text = image_to_text(filename)
print(text)
closest_sub, distance = find_closest_subject(text, config.SUBJECTS)
grade = find_grade(text, config.GRADES)
if distance > len(closest_sub) // 2:
closest_sub = 'Không xác định'
if grade == '0':
grade = 'Không xác định'
index_subject = subject_list.index(closest_sub)
cbx_subject.current(index_subject)
index_grade = grade_list.index(grade)
cbx_grade.current(index_grade)
def btn_reset_action():
global filename, closest_sub, grade
img_tk = ImageTk.PhotoImage(Image.fromarray(np.ones(IMAGE_SIZE[::-1]) * 111))
lbl_image.config(image=img_tk)
lbl_image.image = img_tk
cbx_subject.current(0)
cbx_grade.current(0)
filename = ''
closest_sub = 'Không xác định'
grade = 'Không xác định'
def btn_save_action():
global filename, closest_sub, grade
if filename == '':
showerror("Erorr", "You have to choose book's picture!")
return
if closest_sub == 'Không xác định':
showerror("Erorr", "You have to choose book's name!")
return
if grade == 'Không xác định':
showerror("Erorr", "You have to choose book's grade!")
return
confirm = askyesno(title='confirmation', message='Do you want to save to the database?')
if confirm:
with open('db.csv', 'a') as f:
f.write(f"{filename},{closest_sub},{grade}\n")
showinfo('Success', 'You book has been saved successfully!')
btn_reset_action()
btn_choose = Button(root, text='Choose Image', command=btn_choose_action, font=FONT)
btn_choose.place(x=175, y=100)
img_tk = ImageTk.PhotoImage(Image.fromarray(np.ones(IMAGE_SIZE[::-1]) * 111))
# image = PhotoImage('images/tieng_viet_1.jpg')
lbl_image = Label(root, image=img_tk)
lbl_image.place(x=100, y=200)
lbl_subject_name = Label(root, text="Subject", font=FONT)
lbl_subject_name.place(x=675, y=50)
subject_list = ['Không xác định'] + config.SUBJECTS
cbx_subject = ttk.Combobox(root, values=subject_list, state='readonly', font=FONT)
cbx_subject.current(0)
cbx_subject.place(x=675, y=100)
lbl_grade_name = Label(root, text="Grade", font=FONT)
lbl_grade_name.place(x=675, y=350)
grade_list = ['Không xác định'] + config.GRADES
cbx_grade = ttk.Combobox(root, values=grade_list, state='readonly', font=FONT)
cbx_grade.current(0)
cbx_grade.place(x=675, y=400)
btn_save = Button(root, text='Save', command=btn_save_action, font=FONT)
btn_save.place(x=700, y=700)
btn_reset = Button(root, text='Reset', command=btn_reset_action, font=FONT)
btn_reset.place(x=875, y=700)
root.mainloop()