-
Notifications
You must be signed in to change notification settings - Fork 95
/
main.py
260 lines (216 loc) · 8.2 KB
/
main.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
from pathlib import Path
from create_annotations import (
create_image_annotation,
create_annotation_from_yolo_format,
create_annotation_from_yolo_results_format,
coco_format,
)
import cv2
import argparse
import json
import numpy as np
import imagesize
#################################################
# Change the classes depend on your own dataset.#
# Don't change the list name 'Classes' #
#################################################
YOLO_DARKNET_SUB_DIR = "YOLO_darknet"
classes = [
"matricula",
"cara"
]
def get_images_info_and_annotations(opt):
path = Path(opt.path)
annotations = []
images_annotations = []
if path.is_dir():
file_paths = sorted(path.rglob("*.jpg"))
file_paths += sorted(path.rglob("*.jpeg"))
file_paths += sorted(path.rglob("*.png"))
else:
with open(path, "r") as fp:
read_lines = fp.readlines()
file_paths = [Path(line.replace("\n", "")) for line in read_lines]
image_id = 0
annotation_id = 1 # In COCO dataset format, you must start annotation id with '1'
for file_path in file_paths:
# Check how many items have progressed
print("\rProcessing " + str(image_id) + " ...", end='')
# Build image annotation, known the image's width and height
w, h = imagesize.get(str(file_path))
image_annotation = create_image_annotation(
file_path=file_path, width=w, height=h, image_id=image_id
)
images_annotations.append(image_annotation)
label_file_name = f"{file_path.stem}.txt"
if opt.yolo_subdir:
annotations_path = file_path.parent / YOLO_DARKNET_SUB_DIR / label_file_name
else:
annotations_path = file_path.parent / label_file_name
if annotations_path.exists(): # The image may not have any applicable annotation txt file.
with open(str(annotations_path), "r") as label_file:
label_read_line = label_file.readlines()
# yolo format - (class_id, x_center, y_center, width, height)
# coco format - (annotation_id, x_upper_left, y_upper_left, width, height)
for line1 in label_read_line:
label_line = line1
category_id = (
int(label_line.split()[0]) + 1
) # you start with annotation id with '1'
x_center = float(label_line.split()[1])
y_center = float(label_line.split()[2])
width = float(label_line.split()[3])
height = float(label_line.split()[4])
float_x_center = w * x_center
float_y_center = h * y_center
float_width = w * width
float_height = h * height
min_x = int(float_x_center - float_width / 2)
min_y = int(float_y_center - float_height / 2)
width = int(float_width)
height = int(float_height)
if opt.results == True: #yolo_result to Coco_result (saves confidence)
conf = float(label_line.split()[5])
annotation = create_annotation_from_yolo_results_format(
min_x,
min_y,
width,
height,
image_id,
category_id,
conf
)
else:
annotation = create_annotation_from_yolo_format(
min_x,
min_y,
width,
height,
image_id,
category_id,
annotation_id,
segmentation=opt.box2seg,
)
annotations.append(annotation)
annotation_id += 1
image_id += 1 # if you finished annotation work, updates the image id.
return images_annotations, annotations
def debug(opt):
path = opt.path
color_list = np.random.randint(low=0, high=256, size=(len(classes), 3)).tolist()
# read the file
file = open(path, "r")
read_lines = file.readlines()
file.close()
for line in read_lines:
print("Image Path : ", line)
# read image file
img_file = cv2.imread(line[:-1])
# read .txt file
label_path = line[:-4] + "txt"
label_file = open(label_path, "r")
label_read_line = label_file.readlines()
label_file.close()
for line1 in label_read_line:
label_line = line1
category_id = label_line.split()[0]
x_center = float(label_line.split()[1])
y_center = float(label_line.split()[2])
width = float(label_line.split()[3])
height = float(label_line.split()[4])
int_x_center = int(img_file.shape[1] * x_center)
int_y_center = int(img_file.shape[0] * y_center)
int_width = int(img_file.shape[1] * width)
int_height = int(img_file.shape[0] * height)
min_x = int_x_center - int_width / 2
min_y = int_y_center - int_height / 2
width = int(img_file.shape[1] * width)
height = int(img_file.shape[0] * height)
print("class name :", classes[int(category_id)])
print("x_upper_left : ", min_x, "\t", "y_upper_left : ", min_y)
print("width : ", width, "\t", "\t", "height : ", height)
print()
# Draw bounding box
cv2.rectangle(
img_file,
(int(int_x_center - int_width / 2), int(int_y_center - int_height / 2)),
(int(int_x_center + int_width / 2), int(int_y_center + int_height / 2)),
color_list[int(category_id)],
3,
)
cv2.imshow(line, img_file)
delay = cv2.waitKeyEx()
# If you press ESC, exit
if delay == 27 or delay == 113:
break
cv2.destroyAllWindows()
def get_args():
parser = argparse.ArgumentParser("Yolo format annotations to COCO dataset format")
parser.add_argument(
"-p",
"--path",
type=str,
help="Absolute path for 'train.txt' or 'test.txt', or the root dir for images.",
)
parser.add_argument(
"--debug",
action="store_true",
help="Visualize bounding box and print annotation information",
)
parser.add_argument(
"--output",
default="train_coco.json",
type=str,
help="Name the output json file",
)
parser.add_argument(
"--yolo-subdir",
action="store_true",
help="Annotations are stored in a subdir not side by side with images.",
)
parser.add_argument(
"--box2seg",
action="store_true",
help="Coco segmentation will be populated with a polygon "
"that matches replicates the bounding box data.",
)
parser.add_argument(
"--results",
action="store_true",
help="Saves confidence scores of the results"
"yolo results to Coco results.",
)
args = parser.parse_args()
return args
def main(opt):
output_name = opt.output
output_path = "output/" + output_name
print("Start!")
if opt.debug is True:
debug(opt)
print("Debug Finished!")
else:
(
coco_format["images"],
coco_format["annotations"],
) = get_images_info_and_annotations(opt)
for index, label in enumerate(classes):
categories = {
"supercategory": "Defect",
"id": index + 1, # ID starts with '1' .
"name": label,
}
coco_format["categories"].append(categories)
if opt.results == True:
dict_list = []
for l in coco_format["annotations"]:
dict_list.append(l[0])
with open(output_path, "w") as outfile:
str = json.dump(dict_list, outfile, indent=4)
else:
with open(output_path, "w") as outfile:
json.dump(coco_format, outfile, indent=4)
print("Finished!")
if __name__ == "__main__":
options = get_args()
main(options)