-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaugmentation_candidate_selector.py
63 lines (51 loc) · 2.09 KB
/
augmentation_candidate_selector.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
import os
import shutil
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from PIL import Image
class AugmentationSelector:
def __init__(self, directory):
self.directory = directory
self.image_files = [f for f in os.listdir(directory) if f.endswith(('.png', '.jpg', '.jpeg', '.bmp'))]
self.index = 0
self.selected_images = []
if os.path.exists(os.path.join(directory, 'selected')):
shutil.rmtree(os.path.join(directory, 'selected'))
os.makedirs(os.path.join(directory, 'selected'))
self.fig, self.ax = plt.subplots()
self.ax_select = plt.axes([0.3, 0.03, 0.3, 0.075])
self.ax_skip = plt.axes([0.61, 0.03, 0.3, 0.075])
self.btn_select = Button(self.ax_select, 'Select for Augmentation')
self.btn_select.on_clicked(self.select_image)
self.btn_skip = Button(self.ax_skip, 'Do not Augment')
self.btn_skip.on_clicked(self.skip_image)
self.show_image()
def show_image(self):
img = Image.open(os.path.join(self.directory, self.image_files[self.index]))
print(f'Showing image: {self.index + 1} of {len(self.image_files)}')
self.ax.axis('off')
self.ax.imshow(img)
plt.draw()
def select_image(self, event):
selected_image = self.image_files[self.index]
self.selected_images.append(selected_image)
shutil.copy(os.path.join(self.directory, selected_image), os.path.join(self.directory, 'selected'))
self.next_image()
def skip_image(self, event):
self.next_image()
def next_image(self):
self.index += 1
if self.index < len(self.image_files):
self.ax.clear()
self.show_image()
else:
with open('selected_for_augmentation.txt', 'w') as f:
for image in self.selected_images:
f.write(f'{image}\n')
plt.close()
print('Done')
def show(self):
plt.show()
if __name__ == '__main__':
selector = AugmentationSelector('augmentation_candidates')
selector.show()