-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeepLearningLabeler.py
132 lines (106 loc) · 4.26 KB
/
DeepLearningLabeler.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
121
122
123
124
125
126
127
128
129
130
131
132
import sys
import numpy as np
import matplotlib.pyplot as plt
import shutil
import datetime
from PIL import Image
from pathlib import Path
from matplotlib.widgets import Button
class manual_classifier(object):
"""The class uses only matplotlib functions to realize a convenient manual classification for images
that sit in the specified folder. It will copy the images to the desired image class folder (e.g.
class_1 and class_2)
Input:
The path of images that are mixed and that need to be classified into two classes.
Usage:
manualLabeler (str)/path/to/your/images
author: Bin Wang
email: alexwang911217@gmail.com"""
def __init__(self, path, name="*", folder="*", iformat="tiff"):
self.classified_path = Path(path)
self.particularName = name
self.particularFolder = folder
self.img_format = iformat
self.now = datetime.datetime.now().strftime("%H:%M:%S")
self.now = self.now.replace(":","_")
self.ind = 0
self.img_enumerator = self.image_enumerator(path=self.classified_path)
self.class_1_path = path.parent / "class_1"
self.class_1_path.mkdir(parents=True, exist_ok=True)
self.class_2_path = path.parent / "class_2"
self.class_2_path.mkdir(parents=True, exist_ok=True)
self.fig = plt.figure()
self.b1 = Button(self.fig.add_axes([0, 0.75, 0.1, 0.04]), 'Class_1')
self.b1.on_clicked(self.Classify_to_1)
self.b2 = Button(self.fig.add_axes([0, 0.50, 0.1, 0.04]), 'Class_2')
self.b2.on_clicked(self.Classify_to_2)
self.b3 = Button(self.fig.add_axes([0, 0.25, 0.1, 0.04]), 'Skip')
self.b3.on_clicked(self.next_img)
self.canvas = self.fig.add_axes([0, 0, 1, 1])
self.canvas.get_xaxis().set_visible(False)
self.canvas.get_yaxis().set_visible(False)
self.current_img_path = next(self.img_enumerator)[1]
img0 = self.read_img(self.current_img_path)
self.img_to_update = self.plot_img(ax = self.canvas, imgarray=img0)
plt.show()
def next_img(self, event=None):
try:
self.current_img_path = next(self.img_enumerator)[1]
imgnew = self.read_img(self.current_img_path)
self.img_to_update.set_data(imgnew)
plt.draw()
except StopIteration:
print("Images have been exhausted!")
pass
def read_img(self, img_path):
return np.array(Image.open(img_path))
def Classify_to_1(self, path):
path = self.class_1_path / f"{self.now}_{self.ind}.{self.img_format}"
self.ind += 1
print(f"copying file to {path}...")
try:
shutil.copy(self.current_img_path, path)
except shutil.SameFileError:
print(f"Image {self.current_img_path} has already been copied!")
pass
finally:
self.next_img()
def Classify_to_2(self, path):
path = self.class_2_path / f"{self.now}_{self.ind}.{self.img_format}"
self.ind += 1
print(f"copying file to {path}...")
try:
shutil.copy(self.current_img_path, path)
except shutil.SameFileError:
print(f"Image {self.current_img_path} has already been copied!")
pass
finally:
self.next_img()
def plot_img(self, ax, imgarray):
return ax.imshow(imgarray, vmin=imgarray.min(), vmax=imgarray.max())
def image_enumerator(self, path, particularFolder = None, particularName="*", iformat = "tiff"):
particularFolder = self.particularFolder
particularName = self.particularName
if particularFolder == "*":
print(f"{particularName}.{iformat}")
imagefiles = path.rglob(f"{particularName}.{iformat}")
else:
print(f"{particularFolder}\\{particularName}.{iformat}")
imagefiles = path.rglob(f"{particularFolder}\\{particularName}.{iformat}")
return enumerate(imagefiles)
def main():
try:
path = Path(sys.argv[1])
except IndexError:
path = Path(r".\tiff_image")
try:
pn = sys.argv[2]
except IndexError:
pn = "*"
try:
pf = sys.argv[3]
except IndexError:
pf = "*"
manual_classifier(path, pn, pf)
if __name__ == "__main__":
main()