-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvoc2jsonCOCO.py
executable file
·187 lines (146 loc) · 6.48 KB
/
voc2jsonCOCO.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 9 17:13:57 2021
@author: sharib
"""
import os
import json
import glob
import cv2
import argparse
from misc import EndoCV_misc
coco_format = {
"images": [
{
}
],
"categories": [
],
"annotations": [
{
}
]
}
def create_image_annotation(file_name, width, height, image_id):
file_name = file_name.split('/')[-1]
images = {
'file_name': file_name,
'height': height,
'width': width,
'id': image_id
}
return images
def create_annotation_coco_format(min_x, min_y, width, height, score, image_id, category_id, annotation_id, args):
bbox = (min_x, min_y, width, height)
area = width * height
if args.type == 'GT':
annotation = {
'id': annotation_id,
'image_id': image_id,
'bbox': bbox,
'area': area,
'iscrowd': 0,
'category_id': category_id,
'segmentation': []
}
else:
annotation = {
'id': annotation_id,
'image_id': image_id,
'bbox': bbox,
'area': area,
'iscrowd': 0,
'category_id': category_id,
'segmentation': [],
'score': float(score)
}
return annotation
def images_annotations_info(args):
root_path = args.root_path
dataset = {'categories': [], 'annotations': [], 'images': []}
with open( 'obj.names') as f:
classes = f.read().strip().split()
for i, cls in enumerate(classes, 1):
dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'mark'})
global count, annot_count
count = 0
annot_count = 1
bboxFolder = args.txtFiles_path
ground_truth_files_list = glob.glob(os.path.join(bboxFolder,'*.txt'))
ground_truth_files_list.sort()
images = []
annotations=[]
score = []
for txt_file in ground_truth_files_list:
file_id = txt_file.split(".txt",1)[0]
file_id = os.path.basename(os.path.normpath(file_id))
lines_list = EndoCV_misc.file_lines_to_list(txt_file)
# im = cv2.imread(os.path.join(root_path, 'images/') + file_id+'.jpg')
im = cv2.imread(root_path + '/' +file_id+'.jpg')
height, width, _ = im.shape
images.append(create_image_annotation(os.path.join(root_path, '/') + file_id+'.jpg', width, height, count))
for line in lines_list:
try:
annot_count+=1
if args.type == 'GT':
cls_id, x1, y1, x2, y2 = line.split()
else:
cls_id, score, x1, y1, x2, y2 = line.split()
width_box = max(0, float(x2) - float(x1))
height_box = max(0, float(y2)- float(y1))
except ValueError:
# error_msg = "Error: File " + txt_file + " in the wrong format.\n"
# EndoCV_misc.error(error_msg)
# handle masks that are zero
if args.type == 'GT':
cls_id, x1, y1, x2, y2 = ('polyp', '-1', '-1', '-1', '-1')
else:
cls_id, x1, y1, x2, y2 = ('polyp', '-1', '-1', '-1', '-1')
score = 1.0
width_box = max(0, float(x2) - float(x1))
height_box = max(0, float(y2)- float(y1))
annotations.append(create_annotation_coco_format(float(x1), float(y1), width_box, height_box, score, count, 1, annot_count, args))
count = count+1
return images, annotations
# def get_args():
# parser = argparse.ArgumentParser('VOC format annotations to COCO dataset format')
# parser.add_argument('--root_path', default='/media/sharib/development/EndoCV2021-test_analysis/endocv2021-test-noCopyAllowed-v1/EndoCV_DATA1', type=str, help='Absolute path for \'train.txt\' or \'test.txt\'')
# parser.add_argument('--txtFiles_path', default='/media/sharib/development/EndoCV2021-test_analysis/codes-det/EndoCV2021/detection/EndoCV_DATA1_pred', type=str, help='Absolute')
# parser.add_argument('--type', default='pred', type=str, help='Name the output json file')
# args = parser.parse_args()
# return args
def get_args():
parser = argparse.ArgumentParser('VOC format annotations to COCO dataset format')
parser.add_argument('--root_path', default='/media/sharib/development/EndoCV2021-test_analysis/endocv2021-test-noCopyAllowed-v1/EndoCV_DATA1', type=str, help='Absolute path for \'train.txt\' or \'test.txt\'')
parser.add_argument('--txtFiles_path', default='/media/sharib/development/EndoCV2021-test_analysis/codes-det/EndoCV2021/detection/EndoCV_DATA1_pred', type=str, help='Absolute')
parser.add_argument('--type', default='pred', type=str, help='Name the output json file, your voc files must have polyp, score, x1, y1, x2, y2 in voc format')
parser.add_argument('--phase', default='EndoCV_DATA1', type=str, help='just put the data you are infering for...{EndoCV_DATA1, EndoCV_DATA2, EndoCV_DATA3}')
args = parser.parse_args()
return args
# def get_args():
# parser = argparse.ArgumentParser('Yolo format annotations to COCO dataset format')
# parser.add_argument('--root_path', default='/Volumes/myPC/EndoCV2021/EndoCV2021-polyp_det_seg_gen/example/', type=str, help='Absolute path for \'train.txt\' or \'test.txt\'')
# parser.add_argument('--txtFiles_path', default='/Volumes/myPC/EndoCV2021/EndoCV2021-polyp_det_seg_gen/example/predicted/', type=str, help='Absolute')
# parser.add_argument('--type', default='Pred', type=str, help='Name the output json file')
# args = parser.parse_args()
# return args
if __name__ == '__main__':
args = get_args()
phase = args.phase
classes = ['polyp']
# folder = os.path.join(args.root_path, 'annotations')
folder = 'annotations'
if not os.path.exists(folder):
os.makedirs(folder)
coco_format['images'], coco_format['annotations'] = images_annotations_info(args)
json_name = os.path.join('annotations/{}.json'.format(phase))
for index, label in enumerate(classes):
ann = {
"supercategory": "none",
"id": index + 1, # Index starts with '1' .
"name": label
}
coco_format['categories'].append(ann)
with open(json_name, 'w') as f:
json.dump(coco_format, f)