-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelte_file.py
More file actions
108 lines (87 loc) · 2.85 KB
/
delte_file.py
File metadata and controls
108 lines (87 loc) · 2.85 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
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 update_image(new_data, im):
im.set_data(new_data)
fig.canvas.draw_idle()
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_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 , file_list , current_image_index
image_name = file_list[current_image_index]
if os.path.isfile(image_name):
print(f"Deleted {image_name}")
os.remove(image_name)
next_image(None)
# Configuration
name = 'detect_scen4.1/_camera8_image_raw' # Folder name
target_sheet = 4 # Target seat number
target = 'reason/detect_scen4.1/_camera8/4/2'
file_list = natsort.natsorted(glob(os.path.join(target , '*.jpg')))
file_len = len(file_list)
# 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>', delete_image)
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()