-
Notifications
You must be signed in to change notification settings - Fork 17
/
generate_json.py
90 lines (67 loc) · 2.99 KB
/
generate_json.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
import os
import cv2
import json
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
def line_select_callback(clk, rls):
global tl_list
global br_list
tl_list.append((int(clk.xdata), (int(clk.ydata))))
br_list.append((int(rls.xdata), (int(rls.ydata))))
def toggle_selector(event):
toggle_selector.RS.set_active(True)
def onkeypress(event):
global tl_list
global br_list
if event.key == 'q':
generate_json(tl_list, br_list)
tl_list = []
br_list = []
def generate_json(tl_list, br_list):
image_dict = {"image":'', "annotations":[]}
label_dict = {"label":'', "coordinates":{}}
coord_dict = {"x":int, "y":int, "width":int, "height":int}
center_x = int(abs((tl_list[0][0] - br_list[0][0])/2)) + int(tl_list[0][0])
center_y = int(abs((tl_list[0][1] - br_list[0][1])/2)) + int(tl_list[0][1])
width = int(abs(tl_list[0][0] - br_list[0][0]))
height = int(abs(tl_list[0][1] - br_list[0][1]))
coord_dict['x'] = center_x
coord_dict['y'] = center_y
coord_dict['width'] = width
coord_dict['height'] = height
label_dict['label'] = name_class
label_dict['coordinates'] = coord_dict
image_dict['image'] = file_name
image_dict['annotations'].append(label_dict)
annotations.append(image_dict)
#Main
image_folder = 'path_to_image_folder'
file_name = ''
name_class = ''
rotation = ''
annotations = []
tl_list = []
br_list = []
file_names = os.listdir(image_folder)
for file_name in file_names:
if file_name[0] != '.':
name_class, sep, tail = file_name.partition('_')
dir_file = image_folder + '/' + file_name
fig, ax = plt.subplots(1)
image = cv2.imread(dir_file)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
ax.imshow(image)
toggle_selector.RS = RectangleSelector(
ax, line_select_callback,
drawtype='box', useblit=True,
button=[1], minspanx=5, minspany=5,
spancoords='pixels', interactive=True
)
bbox = plt.connect('key_press_event', toggle_selector)
key = plt.connect('key_press_event', onkeypress)
plt.show()
print('Number of Processed Images:', len(annotations))
json_file = json.dumps(annotations)
with open('save_directory/Annotations.json', 'w') as f:
f.write(json_file)