-
Notifications
You must be signed in to change notification settings - Fork 0
/
markrect.py
331 lines (254 loc) · 7.71 KB
/
markrect.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# USAGE
# python markrect.py -i imagefolder/ -j jsonfile.json
# 'S' - 'Save' key to save current box to elements list
# 'N' - 'Next' key to next image (appends current image info to JSON)
# saving a JSON object with image_path will override any previous JSON object with same image_path
# 'P' - 'Previous' key to backtrack image
# 'R' - 'Reset' key to reset the marking of current images
# 'Q' - 'Quit' key to quit
# Legend:
# 0 : lonely
# 1 : love
# 2 : meeting
# 3 : team
# 4 : party
# 5 : fighting
# 6 : singers
# 7 : wedding
from Tkinter import *
import argparse
import cv2
import numpy as np
import json
import glob
import os
# TODO: Wrap in class
# list of reference points
refPt = []
# list of elements in an image
elements = []
# name of current label
label = 0
chose = False
def printn(name):
global root, label, chose
if name == "lonely":
label = 0
elif name == "love":
label = 1
elif name == "meeting":
label = 2
elif name == "team":
label = 3
elif name == "party":
label = 4
elif name == "fighting":
label = 5
elif name == "singers":
label = 6
elif name == "wedding":
label = 7
print "Current label is {0}".format(name)
chose = True
# make window invisible
root.withdraw()
# make window visible
# root.deiconify
# destroy window
# root.destroy()
# logic to center the Tkinter button frame onto screen
def center(toplevel):
toplevel.update_idletasks()
w = toplevel.winfo_screenwidth()
h = toplevel.winfo_screenheight()
size = tuple(int(_) for _ in toplevel.geometry().split("+")[0].split('x'))
x = w/2 - size[0]/2
y = h/2 - size[1]/2
toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))
# logic for displaying the images one after the other
def slideshow(images, json_name):
global refPt, elements, label, image, clean, prev, root, chose
global root
root = Tk()
root.wm_title("Choose a button")
root.minsize(width=200, height=200)
frame = Frame(root)
frame.pack()
button1 = Button(frame, text="Lonely", command= lambda: printn("lonely"), width= 20)
button2 = Button(frame, text="Love", command= lambda: printn("love"), width= 20)
button3 = Button(frame, text="Meeting", command= lambda: printn("meeting"), width= 20)
button4 = Button(frame, text="Team", command= lambda: printn("team"), width= 20)
button5 = Button(frame, text="Party", command= lambda: printn("party"), width= 20)
button6 = Button(frame, text="Fighting", command= lambda: printn("fighting"), width= 20)
button7 = Button(frame, text="Singers", command= lambda: printn("singers"), width= 20)
button8 = Button(frame, text="Wedding", command= lambda: printn("wedding"), width= 20)
button1.pack()
button2.pack()
button3.pack()
button4.pack()
button5.pack()
button6.pack()
button7.pack()
button8.pack()
center(root)
# star
index = 0
image = cv2.imread(images[index])
# CLONE FOR RESETS
prev = image.copy()
clean = image.copy()
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", mark_poly)
#buttonPack()
# loops until "Q" pressed
while True:
#root.update_idletasks()
if not chose:
root.update()
# display image and wait for key
cv2.imshow("Image", image)
key = cv2.waitKey(1) & 0xFF
# go to next image
if key == ord("n") or key == ord("N"):
save_json(images[index]) # saves current
if index < len(images) - 1 and len(refPt) == 0:
index = index + 1
image = cv2.imread(images[index])
# prepare frame for new image
clean = image.copy()
prev = image.copy()
resetImage()
# RESET CLICKER FOR NEW IMAGE
else:
os.system("say 'No more pictures'")
# go to previous image
elif key == ord("p") or key == ord("P"):
if index > 0:
index = index - 1
image = cv2.imread(images[index])
# prepare frame for new image
clean = image.copy()
prev = image.copy()
resetImage()
# RESET CLICKER FOR NEW IMAGE
else:
os.system("say 'No previous pictures")
# save rectangle and label it
elif key == ord("s") or key == ord("S"):
if len(refPt) == 4 and chose:
element = {}
element["contours"] = order_coords_clockwise(refPt)
element["label"] = label
elements.append(element)
print elements
prev = image.copy()
del refPt[:]
label = ""
chose = False
root.deiconify()
else:
os.system("say 'Incomplete'")
# reset elements list for the current image
elif key == ord("r") or key == ord("R"):
resetImage()
image = clean.copy()
prev = image.copy()
# finish marking and dump JSON
elif key == ord("f") or key == ord("F"):
with open(json_name, 'w') as js:
json.dump(JSON, js, indent=2)
os.system("say 'Jayson saved'")
# quit
elif key == ord("q") or key == ord("Q"):
break
#root.mainloop()
def resetImage():
global refPt, elements, label, root, chose
del refPt[:]
del elements[:]
if chose:
root.deiconify()
chose = False
label = 0
# mark 4 coordinates
def mark_poly(event, x, y, flags, param):
global refPt, elements
global clean, image, prev
if event == cv2.EVENT_LBUTTONDOWN:
# if length of current coordinates is already 4
if len(refPt) == 4:
del refPt[:]
image = prev.copy()
refPt.append((x, y))
# draw a point
cv2.circle(image, (x, y), 3, (0, 0, 255), -1)
if len(refPt) > 1:
l = len(refPt)
cv2.line(image, refPt[l-1], refPt[l-2], (0, 0, 255), 2)
if len(refPt) == 4:
cv2.line(image, refPt[0], refPt[3], (0, 0, 255), 2)
cv2.imshow("Image", image)
# takes as input a list of elements
# each item in the list is a dictionary with
# label: name of the element ("fighting", "love", "meeting", etc.)
# contours: coordinates of the 4-sided polygon
def order_coords_clockwise(refPt):
pts = np.array(refPt)
# initialzie a list of coordinates that will be ordered
# such that the first entry in the list is the top-left,
# the second entry is the top-right, the third is the
# bottom-right, and the fourth is the bottom-left
contour = np.zeros((4, 2), dtype = "int32") #"float32")
# the top-left point will have the smallest sum, whereas
# the bottom-right point will have the largest sum
s = pts.sum(axis = 1)
contour[0] = pts[np.argmin(s)]
contour[2] = pts[np.argmax(s)]
# now, compute the difference between the points, the
# top-right point will have the smallest difference,
# whereas the bottom-left will have the largest difference
diff = np.diff(pts, axis = 1)
contour[1] = pts[np.argmin(diff)]
contour[3] = pts[np.argmax(diff)]
# return the ordered coordinates
return contour.tolist()
# takes as input an image path and pointers (a set of rectangle coordinates) to create a json item
def save_json(image_path):
global JSON, elements
# iterate through objects in JSON to see if current image was already appended
for i in xrange(len(JSON)):
if JSON[i]["image_path"] == image_path:
JSON.pop(i)
break
# create JSON object
# image_path: the path to a specific image
# elements: the labels and coordinates for elements inside the image
json_obj = {}
json_obj["image_path"] = image_path
el = list(elements)
json_obj["elements"] = el
JSON.append(json_obj)
def main():
global JSON
# parse command line arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to image files")
ap.add_argument("-j", "--json", required=True, help="Path to JSON file")
args = vars(ap.parse_args())
# create list of images
image_list = []
for type in ("*.jpg", "*.JPG", "*.jpeg", "*.JPEG", "*.png"):
for image_path in glob.glob(args["image"] + "/" + type):
image_list.append(image_path)
# get name of JSON output file
json_name = args["json"]
if not json_name.endswith(".json"):
json_name = json_name + ".json"
# create JSON
JSON = []
# slideshow of images to add rectangles
slideshow(image_list, json_name)
cv2.destroyAllWindows()
root.destroy()
if __name__ == "__main__":
main()