-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelcls.py
254 lines (216 loc) · 7.77 KB
/
labelcls.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import tkinter
from easydict import EasyDict as edict
from tkinter.filedialog import askdirectory,askopenfilename,asksaveasfilename
import os
from PIL import Image,ImageTk
import numpy as np
from concurrent.futures import ThreadPoolExecutor as tpe
from functools import lru_cache
pool=tpe(4)
tk=tkinter
shape=(1024,768)
args=edict({
'input' : None,
'output' : '',
'cls_list':['0','1','2','3','4'],
'filelist':[],
'filelabel':{},
'curlabel':-1,
'curselect':0,
'im':None,
'imname':None,
'imidx':-1,
'clsidx':-1
})
class labeled(object):
num=0
def clsbarGen():
clsLB.delete(0,tk.END)
for name in args.cls_list:
clsLB.insert(tk.END,name)
def clsSelect(path=''):
if len(path)==0:
path=askopenfilename()
if path is None or len(path)==0:
return
with open(path,encoding='utf-8') as f:
args.cls_list=[i.strip() for i in f.readlines() if len(i.strip())>0]
clsbarGen()
initlistbox()
def readLabel(path=''):
if len(path)==0:
path=askopenfilename()
if not os.path.exists(path):
return
with open(path,encoding='utf-8') as f:
for r in f.readlines():
if len(r)>1:
k,v=r.strip().split(',')
args.filelabel[k]=v
def saveLabel(path=''):
labeled.num=0
if len(path)==0:
path=asksaveasfilename()
if path is None or len(path)==0:
return
with open(path,'w',encoding='utf-8') as f:
f.write('\n'.join(('{},{}'.format(k,v) for k,v in args.filelabel.items())))
args.output=path
def getfilelist(path):
args.filelist=[]
for name in sorted(os.listdir(path)):
_,ext=os.path.splitext(name)
if ext.lower() in {'.jpg','.jpeg','.bmp','.png','.tif','.tiff'}:
args.filelist.append(name)
labeled.num = 0
def initlistbox():
theLB.delete(0,tk.END)
for idx,name in enumerate(args.filelist):
if name in args.filelabel and args.filelabel[name] in args.cls_list:
#theLB.insert(tk.END,'*_'+name)
theLB.insert(tk.END, name)
theLB.itemconfigure(idx,bg='#FFFFFF')
else:
#theLB.insert(tk.END,'__'+name)
theLB.insert(tk.END, name)
theLB.itemconfigure(idx, bg='#FFBBBB')
theLB.selection_set(0)
drawImage()
def inputSelect():
input=askdirectory()
if len(input)==0:
return
args.input=input
getfilelist(args.input)
initlistbox()
if os.path.exists(os.path.join(args.input,'classes.txt')):
clsSelect(os.path.join(args.input,'classes.txt'))
def outputSelect():
readLabel()
initlistbox()
def saveSelect():
saveLabel()
def resetCls(event=None):
idx=args.imidx
if idx >= 0:
filename=args.filelist[idx]
clsLB.selection_clear(0,tk.END)
if filename in args.filelabel and args.filelabel[filename] in args.cls_list:
clsLB.selection_set(args.cls_list.index(args.filelabel[filename]))
clslabel.config(text=args.filelabel.get(filename,''))
def onClsChange(event=None):
seidxs=clsLB.curselection()
if args.imidx >= 0 and len(seidxs)>0:
cls=args.cls_list[seidxs[0]]
filename=args.filelist[args.imidx]
#theLB.delete(args.imidx)
#theLB.insert(args.imidx,'*_'+filename)
theLB.itemconfigure(args.imidx, bg='#FFFFFF')
theLB.selection_set(args.imidx)
args.filelabel[filename]=cls
clslabel.config(text=cls)
labeled.num += 1
if labeled.num > 100 and len(args.output) > 0:
try:
saveLabel(args.output)
except Exception:
pass
def loadImage(filepath):
im = Image.open(filepath)
ratio = max(float(im.width) / shape[0], float(im.height) / shape[1])
im = im.resize((int(im.width / ratio), int(im.height / ratio)), resample=Image.BILINEAR)
return im
@lru_cache(maxsize=64)
def loadImageBg(filepath):
return pool.submit(loadImage,filepath)
def drawImage(event=None):
idxs=theLB.curselection()
if len(idxs)>0:
idx=idxs[0]
args.imidx=idx
filename=args.filelist[idx]
filepath=os.path.join(args.input,filename)
if filepath != args.imname:
fut=loadImageBg(filepath)
if fut.done():
im=fut.result()
else:
im=loadImage(filepath)
args.im=ImageTk.PhotoImage(im)
Label.config(image=args.im)
args.imname=filepath
for i in range(idx-4,idx+6):
filename = args.filelist[i % len(args.filelist)]
filepath = os.path.join(args.input, filename)
loadImageBg(filepath)
resetCls()
def changeFocus(event=None):
binput.focus_set()
def onKeyDown(event=None):
if event.keysym == 'Left' or event.keysym == 'Right':
idx=args.imidx
if idx>=0:
if event.keysym == 'Left':
new_idx=idx-1
elif event.keysym == 'Right':
new_idx=idx+1
else:
raise NotImplementedError
new_idx=new_idx % len(args.filelist)
theLB.selection_clear(0,tk.END)
theLB.selection_set(new_idx)
theLB.yview_scroll(new_idx-idx,'unit')
drawImage()
elif event.keysym == 'Up' or event.keysym == 'Down' or 96 <= event.keycode < 96 + len(args.cls_list):
if 96 <= event.keycode < 96 + len(args.cls_list):
new_idx=event.keycode-96
else:
seidxs = clsLB.curselection()
if len(seidxs) == 0:
new_idx=0 if event.keysym=='Down' else -1
else:
new_idx= seidxs[0] +1 if event.keysym=='Down' else seidxs[0]-1
new_idx = new_idx % len(args.cls_list)
clsLB.selection_clear(0,tk.END)
clsLB.selection_set(new_idx)
onClsChange()
root=tkinter.Tk()
root.title('图片类别标注工具,快捷键为方向键上下左右和小键盘数字键0-9')
# left: listbox
fm1 = tkinter.Frame(root)
scrolly=tkinter.Scrollbar(fm1)
theLB = tkinter.Listbox(fm1,width=18,height=15,yscrollcommand=scrolly.set,exportselection=False)
scrolly.config(command=theLB.yview)
# canvas
fmc = tkinter.Frame(root)
args.im= ImageTk.PhotoImage(Image.fromarray(np.zeros((shape[1],shape[0],3),dtype='uint8')+200))
Label = tk.Label(fmc,image=args.im, width=shape[0],height=shape[1],font=('System', 32))
clslabel = tk.Label(fmc,text='',font=(None,32),width=8,bg='#FFFFEE')
clsLB=tkinter.Listbox(root,width=10,height=12,exportselection=False,font=(None, 16))
#right:
fm2 = tkinter.Frame(root)
binput=tk.Button(fm2, text='选择图片路径',command=inputSelect)
bcls=tk.Button(fm2, text='载入类别信息',command=clsSelect)
boutput=tk.Button(fm2, text='载入标注文件',command=outputSelect)
bsave=tk.Button(fm2, text='保存当前标注',command=saveSelect)
# fm1
fm1.pack(side=tkinter.LEFT,fill=tkinter.BOTH,expand=tkinter.YES)
theLB.pack(side=tkinter.LEFT,fill=tkinter.BOTH,expand=tkinter.YES)
scrolly.pack(side=tkinter.LEFT,fill=tkinter.Y)
fmc.pack(side=tkinter.LEFT,fill=tkinter.NONE,expand=tkinter.NO)
clslabel.pack(side=tk.TOP,fill=tkinter.X,ipadx=5,ipady=5,padx=5,pady=5)
Label.pack(side=tk.TOP,fill=tk.NONE)
clsLB.pack(side=tk.TOP,fill=tkinter.NONE)
# fm2
fm2.pack(side=tkinter.TOP,fill=tkinter.Y,expand=tkinter.YES,ipadx=5,ipady=5,padx=5,pady=5)
binput.pack(side=tk.TOP, fill=tk.BOTH,ipadx=3,ipady=3,padx=3,pady=3,expand=tk.YES)
bcls.pack(side=tk.TOP, fill=tk.BOTH,ipadx=3,ipady=3,padx=3,pady=3,expand=tk.YES)
boutput.pack(side=tk.TOP, fill=tk.BOTH,ipadx=3,ipady=3,padx=3,pady=3,expand=tk.YES)
bsave.pack(side=tk.TOP, fill=tk.BOTH,ipadx=3,ipady=3,padx=3,pady=3,expand=tk.YES)
theLB.bind('<<ListboxSelect>>',drawImage)
clsLB.bind('<<ListboxSelect>>',onClsChange)
root.bind('<Key>',onKeyDown)
theLB.bind('<FocusIn>',changeFocus)
clsLB.bind('<FocusIn>',changeFocus)
clsbarGen()
root.mainloop()