-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_tf_to_coco.py
98 lines (74 loc) · 2.7 KB
/
convert_tf_to_coco.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
"""
Use this to convert csv -> df -> coco
"""
import numpy as np
import json
import pandas as pd
import os
# use this method to convert csv to json
def convert_to_coco(data, save_json_path):
#path = 'tensorflow2csv.csv'
#data = pd.read_csv(path)
images = []
categories = []
annotations = []
category = {}
category["supercategory"] = 'none'
category["id"] = 0
category["name"] = 'None'
categories.append(category)
data['fileid'] = data['filename'].astype('category').cat.codes
data['categoryid']= pd.Categorical(data['class'],ordered= True).codes
data['categoryid'] = data['categoryid']+1
data['annid'] = data.index
def image(row):
image = {}
image["height"] = row.height
image["width"] = row.width
image["id"] = row.fileid
image["file_name"] = row.filename
return image
def category(row):
category = {}
category["supercategory"] = 'None'
category["id"] = row.categoryid
category["name"] = row[2]
return category
def annotation(row):
annotation = {}
area = (row.xmax -row.xmin)*(row.ymax - row.ymin)
annotation["segmentation"] = []
annotation["iscrowd"] = 0
annotation["area"] = area
annotation["image_id"] = row.fileid
annotation["bbox"] = [row.xmin, row.ymin, row.xmax -row.xmin,row.ymax-row.ymin ]
annotation["category_id"] = row.categoryid
annotation["id"] = row.annid
return annotation
for row in data.itertuples():
annotations.append(annotation(row))
imagedf = data.drop_duplicates(subset=['fileid']).sort_values(by='fileid')
for row in imagedf.itertuples():
images.append(image(row))
catdf = data.drop_duplicates(subset=['categoryid']).sort_values(by='categoryid')
for row in catdf.itertuples():
categories.append(category(row))
data_coco = {}
data_coco["images"] = images
data_coco["categories"] = categories
data_coco["annotations"] = annotations
json.dump(data_coco, open(save_json_path, "w"), indent=4)
def main():
#dddd
root = os.getcwd()
print(root)
train_csv_path = r'C:\Users\endle\Desktop\pytorch-retinanet\train_labels - org.csv'
test_csv_path = r'C:\Users\endle\Desktop\pytorch-retinanet\test_labels - org.csv'
df_train = pd.read_csv(train_csv_path)
df_test = pd.read_csv(test_csv_path)
save_json_path_train = r'C:\Users\endle\Desktop\pytorch-retinanet\train_new.json'
save_json_path_val = r'C:\Users\endle\Desktop\pytorch-retinanet\val_new.json'
convert_to_coco(df_train, save_json_path_train)
convert_to_coco(df_test, save_json_path_val)
if __name__ == "__main__":
main()