-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_reason.py
More file actions
159 lines (131 loc) · 5.1 KB
/
find_reason.py
File metadata and controls
159 lines (131 loc) · 5.1 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
153
154
155
156
157
158
159
import cv2
from glob import glob
import os
import matplotlib.pyplot as plt
import natsort
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from tqdm.auto import tqdm
import matplotlib
import gc
current_image_index = 0
name = 'detect_scen1-1'
image_cache = {}
image_name = None
mode = 'reason'
text_handle = None
iter_count = 0
camera_focues = '_camera6'
target_sheet = 9
ai_path = os.listdir(f'{name}/{camera_focues}_box_result/box{target_sheet}')
print("===========TARGET :{} ===============".format(target_sheet))
print("AI DETECT IMAGE LEN :", len(ai_path))
try:
user_path = os.listdir(f'inspect/{name}/{camera_focues}_image_raw/{target_sheet}')
print("User DETECT IMAGE LEN :", len(user_path))
except:
print("========user path not image=======")
exit(0)
def wrapper(event):
save_image(event, image)
def update_image(new_data, im):
im.set_data(new_data)
fig.canvas.draw_idle()
def save_image(event, image):
new_folder_1 = f"{mode}/{name}/{target_sheet}/1"
new_folder_2 = f"{mode}/{name}/{target_sheet}/2"
os.makedirs(new_folder_1, exist_ok=True)
os.makedirs(new_folder_2, exist_ok=True)
if event.keysym == 's':
image_path = os.path.join(new_folder_1, os.path.basename(file_list[current_image_index]))
compare_path = os.path.join(new_folder_2 , os.path.basename(file_list[current_image_index]))
if not os.path.isfile(image_path) and not os.path.isfile(compare_path):
print(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite(image_path, image)
next_image(None)
else:
print(f"{os.path.basename(file_list[current_image_index])} is aleary saved")
next_image(None)
elif event.keysym =='d':
image_path = os.path.join(new_folder_2 , os.path.basename(file_list[current_image_index]))
compare_path = os.path.join(new_folder_1 , os.path.basename(file_list[current_image_index]))
if not os.path.isfile(image_path) and not os.path.isfile(compare_path):
print(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite(image_path, image)
next_image(None)
else:
print(f"{os.path.basename(file_list[current_image_index])} is aleary saved")
next_image(None)
else:
raise KeyboardInterrupt(f"{event.keysym} key Error")
def load_image(image_path):
global text_handle, file_len, iter_count, text_file_count, image_name
if image_path in image_cache:
image = image_cache[image_path]
else:
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
if image is None:
raise FileExistsError(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_cache[image_path] = image
ax.cla()
ax.imshow(image, cmap='gray')
ax.axis('off')
image_name = os.path.basename(image_path)
ax.set_title(f"{os.path.basename(image_path)}")
if text_handle:
text_handle.remove()
text_file_count.remove()
file_count = (iter_count % file_len) + 1
text_handle = ax.text(0.5, 0.5, f"not detect -> s", fontsize=15, ha='center', color='r')
text_file_count = ax.text(500, 0.5, f"{file_count} / {file_len}", fontsize=15, ha='center', color='b')
iter_count += 1
return image
def next_image(event):
global current_image_index, image, text_file_count
current_image_index = (current_image_index + 1) % len(file_list)
image = load_image(file_list[current_image_index])
canvas.draw_idle()
gc.collect()
def previous_image(event):
global current_image_index, image, text_file_count, iter_count
iter_count -= 2
current_image_index = (current_image_index - 1) % len(file_list)
image = load_image(file_list[current_image_index])
canvas.draw_idle()
gc.collect()
def delete_image(event):
global image_name
image_path_ = os.path.join(f'{mode}', name, str(target_sheet), str(1), image_name)
image_path__ = os.path.join(f'{mode}', name, str(target_sheet), str(2), image_name)
if os.path.isfile(image_path_):
print(f"Deleted Reason 1 {image_name}")
os.remove(image_path_)
if os.path.isfile(image_path__):
print(f"Deleted Reason 2 {image_name}")
os.remove(image_path__)
diff_path = set(user_path) - set(ai_path)
# test_path = set(ai_path) - set(user_path)
print("AI 못찾은 개수 : ",len(diff_path))
file_list = natsort.natsorted([os.path.join(f'{name}/{camera_focues}_image_raw',i) for i in diff_path])
file_len = len(file_list)
if file_len == 0:
exit(0)
root = tk.Tk()
root.title("Image plot")
root.geometry('600x600+1000+200')
#matplotlib.use('TkAgg')
plt.ioff()
fig, ax = plt.subplots(dpi=80)
image = load_image(file_list[current_image_index])
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
root.bind('<s>',wrapper)
root.bind('<d>',wrapper)
root.bind('<Right>', next_image)
root.bind('<Left>',previous_image)
root.bind('<Delete>', delete_image)
root.mainloop()