정우진

Upload new file

1 +import numpy as np
2 +import json
3 +import pandas as pd
4 +
5 +path = '../dataset/test/test1/annotations.csv'
6 +classpath = '../dataset/e_label2.csv'
7 +save_json_path = 'testcoco.json'
8 +
9 +
10 +data = pd.read_csv(path, names=['filename', 'width', 'height', 'xmin', 'ymin', 'xmax', 'ymax', 'class'])
11 +
12 +images = []
13 +categories = []
14 +annotations = []
15 +
16 +category = {}
17 +category["supercategory"] = 'none'
18 +category["id"] = 0
19 +category["name"] = 'None'
20 +categories.append(category)
21 +
22 +data['fileid'] = data['filename'].astype('category').cat.codes
23 +data['categoryid']= pd.Categorical(data['class'],ordered= True).codes
24 +data['categoryid'] = data['categoryid']+1
25 +data['annid'] = data.index
26 +
27 +def image(row):
28 + image = {}
29 + image["height"] = row.height
30 + image["width"] = row.width
31 + image["id"] = row.fileid
32 + image["file_name"] = row.filename
33 + return image
34 +
35 +def category(row):
36 + category = {}
37 + category["supercategory"] = 'None'
38 + category["id"] = row.categoryid
39 + category["name"] = row[2]
40 + return category
41 +
42 +def annotation(row):
43 + annotation = {}
44 + area = (row.xmax -row.xmin)*(row.ymax - row.ymin)
45 + annotation["segmentation"] = []
46 + annotation["iscrowd"] = 0
47 + annotation["area"] = area
48 + annotation["image_id"] = row.fileid
49 +
50 + annotation["bbox"] = [row.xmin, row.ymin, row.xmax -row.xmin,row.ymax-row.ymin ]
51 +
52 + annotation["category_id"] = row.categoryid
53 + annotation["id"] = row.annid
54 + return annotation
55 +
56 +for row in data.itertuples():
57 + annotations.append(annotation(row))
58 +
59 +imagedf = data.drop_duplicates(subset=['fileid']).sort_values(by='fileid')
60 +for row in imagedf.itertuples():
61 + images.append(image(row))
62 +
63 +catdf = data.drop_duplicates(subset=['categoryid']).sort_values(by='categoryid')
64 +for row in catdf.itertuples():
65 + categories.append(category(row))
66 +
67 +data_coco = {}
68 +data_coco["images"] = images
69 +data_coco["categories"] = categories
70 +data_coco["annotations"] = annotations
71 +
72 +
73 +json.dump(data_coco, open(save_json_path, "w"), indent=4)
...\ No newline at end of file ...\ No newline at end of file