-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_my_dataset.py
164 lines (136 loc) · 6.59 KB
/
get_my_dataset.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
import os
import csv
import json
import random
basePath = '/media/young/MyPassport/跨越险阻'
categoryDic = {"H":1, "V":2, "B":3}
def get_csv_dic():
dataFolders = [x for x in os.listdir(basePath) if x.split('.')[-1] != 'zip']
all_data = {}
for folder in dataFolders:
gtPath = basePath + '/' + folder + '/' + folder + '-' + 'Groundtruth' + '/gt_detection.csv'
if ( not os.path.exists(gtPath)):
continue
all_data[folder] = {}
f = open(gtPath, "r")
csv_list = []
for row in f:
if row.split(",")[3].replace(" ", "") == 'C':
csv_list.append(row)
f.close()
all_timestamp = []
for row in csv_list:
all_timestamp.append(row.split(",")[1].replace(" ", ""))
all_timestamp = list(set(all_timestamp))
all_timestamp.sort(key = int)
for timestamp in all_timestamp:
all_data[folder][timestamp] = []
for row in csv_list:
# print(row)
if (row.split(",")[1].replace(" ", "") == timestamp):
row_dic = {}
row_dic["category"] = row.split(",")[4].replace(" ", "")
row_dic["box"] = [float(x) for x in row.split("\"")[1][1:-1].split(",")]
row_dic["position"] = [float(x) for x in row.split("\"")[3][1:-1].split(",")]
all_data[folder][timestamp].append(row_dic)
return all_data
#2391 2044 1863 1572 2855 2839 = 13565
#2221 1671 1303 1282 1415 2031 = 9923
def timeStampToImg(result_folder):
timeStampToImg_dic = {}
csvpath = basePath + '/' + result_folder + '/' + result_folder + "-Camera-Timestamp.csv"
with open(csvpath) as f:
tsp = []
for line in f:
timestamp = line.split(',')[0]
tsp.append(timestamp)
img_name = line.split(',')[1].replace(" ", "")
timeStampToImg_dic[timestamp] = img_name[0:-1]
# print(len(set(tsp)) == len(tsp))
return timeStampToImg_dic
def get_coco_json(all_data):
all_json_data = {}
all_json_data["train"] = {}
all_json_data["test"] = {}
#info
all_json_data["train"]["info"] = {"year": 2021, "version": "1.0", "description": "For object detection", "date_created": "2021"}
all_json_data["test"]["info"] = {"year": 2021, "version": "1.0", "description": "For object detection", "date_created": "2021"}
#licenses
all_json_data["train"]["licenses"] = [{"id": 1, "name": "GNU General Public License v3.0", "url": "https://github.com/zhiqwang/yolov5-rt-stack/blob/master/LICENSE"}]
all_json_data["test"]["licenses"] = [{"id": 1, "name": "GNU General Public License v3.0", "url": "https://github.com/zhiqwang/yolov5-rt-stack/blob/master/LICENSE"}]
#type
all_json_data["train"]["type"] = "instances"
all_json_data["test"]["type"] = "instances"
#category
all_json_data["train"]["categories"] = [
{"id": 1, "name": "H", "supercategory": "1"},
{"id": 2, "name": "V", "supercategory": "2"},
{"id": 3, "name": "B", "supercategory": "3"},
]
all_json_data["test"]["categories"] = [
{"id": 1, "name": "H", "supercategory": "1"},
{"id": 2, "name": "V", "supercategory": "2"},
{"id": 3, "name": "B", "supercategory": "3"}
]
#images and annotations
all_json_data["train"]["images"] = []
all_json_data["train"]["annotations"] = []
all_json_data["test"]["images"] = []
all_json_data["test"]["annotations"] = []
img_num = {"train":1, "test":1}
det_num = {"train":1, "test":1}
for k in all_data.keys():
timeStampToImg_dic = timeStampToImg(k)
used_timestamp = []
tsp_list = list(all_data[k].keys())
random.shuffle(tsp_list)
for timestamp in tsp_list:
x = random.uniform(0,1)
dataset_type = "train" if x > 0.2 else "test"
image_info = {}
image_info["date_captured"] = "2021"
image_info["file_name"] = str(img_num[dataset_type]) + ".png"
image_info["id"] = img_num[dataset_type]
image_info["height"] = 768
image_info["width"] = 1024
src_img = basePath + '/' + k + '/' + k+"-Camera" + '/' + timeStampToImg_dic[timestamp]
dst_img = basePath + '/' + 'coco_format_dataset' + '/' + dataset_type + '/' + image_info["file_name"]
command = "cp" + " " + src_img + " " + dst_img
print(command)
os.system(command)
all_json_data[dataset_type]["images"].append(image_info)
for det in all_data[k][timestamp]:
annotation = {}
annotation["segmentation"] = det["box"]
annotation["area"] = (det["box"][2]- det["box"][0]) * (det["box"][5] - det["box"][1])
annotation["iscrowd"] = 0
annotation["image_id"] = img_num[dataset_type]
annotation["bbox"] = [det["box"][0], det["box"][1], det["box"][2]- det["box"][0], det["box"][5] - det["box"][1]]
annotation["category_id"] = categoryDic[det["category"]]
annotation["id"] = det_num[dataset_type]
all_json_data[dataset_type]["annotations"].append(annotation)
det_num[dataset_type] += 1
img_num[dataset_type] += 1
pass
return all_json_data
if __name__ == "__main__":
all_data = get_csv_dic()
all_json_data = get_coco_json(all_data)
with open(basePath + "/coco_format_dataset/annotations/instances_train2021.json", "w") as f:
json.dump(all_json_data["train"],f)
with open(basePath + "/coco_format_dataset/annotations/instances_test2021.json", "w") as f:
json.dump(all_json_data["test"],f)
annos = all_json_data["train"]["annotations"]
category_nums_train = [0, 0, 0] #H V B
for anno in annos:
category_nums_train[anno["category_id"]-1] += 1
annos = all_json_data["test"]["annotations"]
category_nums_test = [0, 0, 0] #H V B
for anno in annos:
category_nums_test[anno["category_id"]-1] += 1
print("training set info:")
print("img num: {0}, det num: {1}".format(len(all_json_data["train"]["images"]), len(all_json_data["train"]["annotations"])))
print("Human: {0}, Vehicle: {1}, Box: {2}}".format(category_nums_train[0], category_nums_train[1], category_nums_train[2]))
print("test set info: ")
print("img num: {0}, det num: {1}".format(len(all_json_data["test"]["images"]), len(all_json_data["test"]["annotations"])))
print("Human: {0}, Vehicle: {1}, Box: {2}".format(category_nums_test[0], category_nums_test[1], category_nums_test[2]))