-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex22.py
More file actions
132 lines (111 loc) · 3.78 KB
/
ex22.py
File metadata and controls
132 lines (111 loc) · 3.78 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
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
import shutil
# Global variables
save_count = 0
current_image_index = 0
text_handle = None
text_file_count = None
iter_count = 0
image_cache = {}
image_name = None
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):
global save_count
new_folder = f"inspect/{name}/{target_sheet}"
os.makedirs(new_folder, exist_ok=True)
image_path = os.path.join(new_folder, os.path.basename(file_list[current_image_index]))
if not os.path.isfile(image_path):
print(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite(image_path, image)
save_count += 1
next_image(None)
def load_image(image_path):
global text_handle, file_len, iter_count, text_file_count, image_name, save_count
if image_path in image_cache:
image = image_cache[image_path]
else:
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
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"{save_count}", 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, save_count
image_path = os.path.join('inspect', name, str(target_sheet), image_name)
if os.path.isfile(image_path):
print(f"Deleted {image_name}")
os.remove(image_path)
save_count = len(os.listdir(os.path.join('inspect', name, str(target_sheet))))
# Configuration
name = 'detect_scen1.1/_camera8_image_raw' # Folder name
target_sheet = 8 # Target seat number
file_list = natsort.natsorted(glob(os.path.join(name, '*.jpg')))
file_len = len(file_list)
# Starting point
try:
save_count = len(os.listdir(os.path.join('inspect', name, str(target_sheet))))
except:
save_count = 0
# target_dir = os.path.join('inspect', name, str(target_sheet))
# os.makedirs(os.path.join(target_dir) , exist_ok= True)
# for i in file_list:
# shutil.copy(i,os.path.join(target_dir , os.path.basename(i)))
# else:
# exit(0)
# Tkinter setup
root = tk.Tk()
root.title("Image plot")
root.geometry('800x800+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)
# Event bindings
root.bind('<Return>', wrapper)
root.bind('<d>', next_image)
root.bind('<a>', previous_image)
root.bind('<Right>', next_image)
root.bind('<Left>', previous_image)
root.bind('<Delete>', delete_image)
root.mainloop()