-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
120 lines (103 loc) · 3.02 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
import os, csv
import tkinter as tk
from tkinter import PhotoImage, filedialog
import yaml
with open('config.yaml', 'r') as f:
params = yaml.load(f, Loader=yaml.loader.SafeLoader)
if params['directory'] is None:
directory = filedialog.askdirectory()
else:
directory = params['directory']
csv_output = ''
counter = 0
images = os.listdir(directory)
try:
with open('output.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
try:
images.remove(row['image path'])
except:
pass
except FileNotFoundError:
pass
window = tk.Tk()
# Function to display the next image
def next_image():
global counter
global images
global window
global label
counter += 1
if counter >= len(images):
counter = 0
print(counter)
img = PhotoImage(file=os.path.join(directory, images[counter]))
label.configure(image=img)
label.image = img# Function to display the next image
def prev_image():
global counter
global images
global window
global label
counter -= 1
if counter <0:
counter = len(images) - 1
print(counter)
img = PhotoImage(file=os.path.join(directory, images[counter]))
label.configure(image=img)
label.image = img
def classify_image(classification):
global counter
global images
global window
global label
global csv_output
csv_output += f"{images[counter]},{classification}\n"
next_image()
top_btns = tk.Frame(window)
button = tk.Button(top_btns, text='Next', command=next_image)
button.grid(column=0, row=0)
button = tk.Button(top_btns, text='Previous', command=prev_image)
button.grid(column=1, row=0)
top_btns.pack()
classes_btns = tk.Frame(window)
for i,c in enumerate(params['classes']):
bt = tk.Button(classes_btns, text=c, command=lambda :classify_image(c))
bt.grid(column=i, row=0)
try:
img = PhotoImage(file=os.path.join(directory, images[0]))
except:
print("No images in directory")
exit()
label = tk.Label(window, image=img)
label.pack()
classes_btns.pack()
# keyboard shortcuts
window.bind_all('g', func=lambda e:classify_image("glasses"))
window.bind_all('n', func=lambda e:classify_image("no glasses"))
window.bind_all('u', func=lambda e:classify_image("unclear"))
window.bind_all('b', func=lambda e:classify_image("bad image"))
window.bind_all('<Left>', func=lambda e: prev_image())
window.bind_all('<Right>', func=lambda e: next_image())
# Run the Tkinter event loop
window.mainloop()
# export csv
filename = 'output.csv'
try:
f = open(filename, 'x')
except FileExistsError:
# File already exists
# export csv
with open('output.csv', 'a') as f:
f.write(csv_output)
else:
print(f"File {filename} created successfully.")
f.write("image path,classification\n")
f.write(csv_output)
f.close()
# drop duplicates
import pandas as pd
df = pd.read_csv('output.csv')
df_deduped = df.drop_duplicates(subset=['image path'], keep='last')
df_deduped.to_csv('output.csv', index=False)