-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.py
More file actions
234 lines (183 loc) · 5.85 KB
/
editor.py
File metadata and controls
234 lines (183 loc) · 5.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
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
import re
from tkinter import *
from PIL import Image, ImageTk
import glob
import os
import sys
import getopt
from helper import *
imageDirectoryPath = ""
labelOutPath = ""
#parse arguments
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:o:",["help","input=","output="])
except getopt.GetoptError:
print("create_sorted_image.py -i <input> -o <output>")
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', "--help"):
print("create_sorted_image.py -i <input> -o <output>")
sys.exit(1)
elif opt in ("-i", "--input"):
imageDirectoryPath = arg
elif opt in ("-o", "--output"):
labelOutPath = arg
print(imageDirectoryPath)
print(labelOutPath)
#recursively get all files in the directory
def loadLabels():
global labelNames
global labelFiles
global rawLabelNames
print(labelOutPath)
labelFiles = getFilesRecursively(labelOutPath, ".txt")
labelNames = getBaseNames(labelFiles)
rawLabelNames = dropExtension(labelNames)
#check if all files are unique
if len(rawLabelNames) != len(set(rawLabelNames)):
print("ERROR: Labels are not unique")
sys.exit(1)
#count number of lines in each labelFile
labelCount = 0
for labelFile in labelFiles:
with open(labelFile, "r") as f:
labelCount += len(f.readlines())
#print labelCounts
print(f"Found {len(rawLabelNames)} labeled files with {labelCount} labels")
imagePaths = getFilesRecursively(imageDirectoryPath)
imageNames = getBaseNames(imagePaths)
rawImageNames = dropExtension(imageNames)
loadLabels()
imageBuffer = None
photoImageBuffer = None
currentSize = None
currentImageId = None
currentImageName = None
currentLabelBuffer = []
crossHairH = None
crossHairV = None
#check if all files are unique
if len(rawImageNames) != len(set(rawImageNames)):
print("ERROR: Images are not unique")
sys.exit(1)
#check if all files are unique
if len(rawLabelNames) != len(set(rawLabelNames)):
print("ERROR: Labels are not unique")
sys.exit(1)
print(f"Found {len(rawImageNames)} images and {len(rawLabelNames)} labels")
#selectionState
startPosition = None
rectID = None
def pressed(event):
global startPosition
startPosition = (event.x, event.y)
print(f"pressed {event.x}, {event.y}")
def released(event):
global startPosition
if startPosition == None:
return
global currentLabelBuffer
startX = max(startPosition[0] / currentSize[0], 0)
startY = max(startPosition[1] / currentSize[1], 0)
endX = min(event.x / currentSize[0], 1)
endY = min(event.y / currentSize[1], 1)
currentLabelBuffer.append(f"{min(startX, endX)},{min(startY, endY)},{max(endX, startX)},{max(endY, startY)}")
print(f"added label: {currentLabelBuffer[-1]}")
global rectID
global canvas
if rectID:
canvas.delete(rectID)
def drag(event):
global canvas
global rectID
global startPosition
mouseMove(event)
if rectID:
canvas.delete(rectID)
rectID = canvas.create_rectangle(startPosition[0], startPosition[1], event.x, event.y)
def key(event):
print(f"key pressed: {event.char}")
if event.char == "f":
saveFullLabel()
elif event.char == "n":
loadNextImage()
def saveLabelBuffer(imageName):
global currentLabelBuffer
global labelOutPath
#save labels
labelPath = os.path.join(labelOutPath, dropExtension_file(imageName) + ".txt")
print(labelPath)
with open(labelPath, "w") as f:
for label in currentLabelBuffer:
f.write(label + "\n")
currentLabelBuffer = []
return None
def loadNextImage():
global imageNames
global labelNames
global imageBuffer
global photoImageBuffer
global currentSize
global canvas
global currentImageId
global currentImageName
#save current labels
if currentImageName != None:
saveLabelBuffer(currentImageName)
loadLabels()
#find imageName that is not in labelNames
nextImage = None
for imageName in rawImageNames:
if imageName not in rawLabelNames:
nextImage = imageName
break
if nextImage == None:
print("ERROR: No images left to label")
sys.exit(1)
else:
print(f"Next image: {nextImage}")
#load image
if currentImageId != None:
canvas.delete(currentImageId)
imagePath = getPathForName(nextImage, imagePaths)
if imagePath == None:
print("ERROR: Could not find image")
sys.exit(1)
currentImageName = getBaseName(imagePath)
imageBuffer = Image.open(imagePath)
imageBuffer = imageBuffer.resize((1200,900))
photoImageBuffer = ImageTk.PhotoImage(imageBuffer)
currentSize = imageBuffer.size
currentImageId = canvas.create_image(0, 0, image=photoImageBuffer, anchor=NW)
canvas.pack()
def saveFullLabel():
global currentLabelBuffer
currentLabelBuffer.append(f"{0},{0},{1},{1}")
loadNextImage()
def mouseMove(event):
global canvas
global crossHairH
global crossHairV
print(f"mouse move: {event.x}, {event.y}")
if crossHairH:
canvas.delete(crossHairH)
if crossHairV:
canvas.delete(crossHairV)
crossHairH = canvas.create_line(event.x, 0, event.x, canvas.winfo_height(), fill="red")
crossHairV = canvas.create_line(0, event.y, canvas.winfo_width(), event.y, fill="red")
root = Tk()
root.title("GANfitti AOI-Tool")
root.geometry("1200x900")
canvas = Canvas(root, width=1200, height=800)
canvas.bind("<Button-1>", pressed)
canvas.bind("<ButtonRelease-1>", released)
canvas.bind("<B1-Motion>", drag)
canvas.bind("<Motion>", mouseMove)
root.bind("<Key>", key)
next = Button(root, text="Save Labels and load Next (N)", command=loadNextImage)
fullImage = Button(root, text="Save Full Image Label and load Next (F)", command=saveFullLabel)
next.pack()
fullImage.pack()
canvas.pack()
loadNextImage()
root.mainloop()