forked from Ignatella/slow-down-or-mandat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
152 lines (127 loc) · 4.96 KB
/
app.py
File metadata and controls
152 lines (127 loc) · 4.96 KB
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
import customtkinter as ctk
import os
import detector
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image
from menu import Menu
from scrollbarlist import ScrollbarList
from imagenavigation import ImageNavigation
import csv
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
class App(ctk.CTk):
def __init__(self):
super().__init__()
self.title("Slow down or mandat - AO")
self.geometry(f"{800}x{600}")
self.all_images = []
self.all_res = {}
self.current = 0
self.size = (400, 300)
def set_image(self, path):
im = Image.open(path)
im.thumbnail(self.size, Image.Resampling.LANCZOS)
self.img = ctk.CTkImage(im, size=im.size)
if self.main_img:
self.main_img.grid_forget()
self.main_img = ctk.CTkLabel(self, text=None, image=self.img)
self.main_img.grid(row=1, column=1)
self.set_res(self.all_res.get(path, ""))
def set_res(self, value):
self.main_img_result.grid_forget()
self.main_img_result = ctk.CTkLabel(self, text=value)
self.main_img_result.grid(row=2, column=1)
def load_file(self):
self.main_img.grid_forget()
self.file = filedialog.askopenfilename(title="Open file", initialdir=os.getcwd(), filetypes=[
("PNG files", "*.png"),
("JPG files", "*.jpg")])
try:
self.set_image(self.file)
except Exception as e:
if len(self.file) != 0:
messagebox.showerror(
"Error", "An error has occured while opening the file")
def load_files_from_dir(self):
dir = filedialog.askdirectory(initialdir=os.getcwd())
fi = filter(lambda f: f.endswith(("png", "jpg")), os.listdir(dir))
for f in fi:
f_path = f'{dir}/{f}'
self.all_images.append(f_path)
res = detector.perform_detection(f_path)
self.all_res[f_path] = res
self.scrollbar_list.add_image(
self.set_image, f_path, res)
def add_to_scrollbar(self):
fi = self.file
try:
if fi not in self.all_images:
res = detector.perform_detection(fi)
self.all_images.append(fi)
self.all_res[fi] = res
self.scrollbar_list.add_image(
self.set_image, fi, res)
self.set_res(res)
except:
messagebox.showerror(
"Error", "An error has occured while performing operation")
def remove_from_scrollbar(self):
self.all_images = []
self.all_res = {}
self.scrollbar_list.grid_forget()
self.scrollbar_list = ScrollbarList(self, width=20)
self.scrollbar_list.grid(
row=0, column=2, rowspan=3, sticky="nwse")
self.main_img.grid_forget()
self.main_img = ctk.CTkLabel(self, text="Main image")
self.main_img.grid(row=1, column=1)
self.set_res("")
def arrow_click(self, mode):
if len(self.all_images) > 0:
if mode == "L":
self.current -= 1
if self.current < 0:
self.current = len(self.all_images) - 1
elif mode == "R":
self.current += 1
if self.current >= len(self.all_images):
self.current = 0
self.set_image(self.all_images[self.current])
self.set_res(self.all_res[self.all_images[self.current]])
else:
messagebox.showerror("Error", "No images loaded")
def export_to_csv(self):
filename = filedialog.asksaveasfilename(filetypes=[("Plik csv", "*.csv")], defaultextension="*.csv")
if filename:
with open(filename, "w", newline="") as file:
writer = csv.writer(file)
header = ("image", "result")
writer.writerow(header)
for row in self.all_res.items():
writer.writerow(row)
def draw(self):
# grid layout settings
self.grid_columnconfigure(1, weight=7)
self.grid_columnconfigure(2, weight=1)
self.grid_rowconfigure((0, 1, 2), weight=1)
# menu
self.menu = Menu(self, width=140)
self.menu.grid(row=0, column=0, rowspan=4, sticky="nwse")
# main image
self.main_img = ctk.CTkLabel(self, text="Main image")
self.main_img.grid(row=1, column=1)
self.main_img_result = ctk.CTkLabel(self, text="")
self.main_img_result.grid(row=2, column=1)
self.image_navigation = ImageNavigation(
self, self, fg_color="transparent")
self.image_navigation.grid(row=3, column=1)
# scrollbar list
self.scrollbar_list = ScrollbarList(self, width=20)
self.scrollbar_list.grid(
row=0, column=2, rowspan=3, sticky="nwse")
# start event loop
self.mainloop()
if __name__ == "__main__":
app = App()
app.draw()