-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYOLO2COCO.py
196 lines (164 loc) · 5.77 KB
/
YOLO2COCO.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
"""
Script: YOLO2COCO.py
Author: Kennedy Mota
Date: 15/01/2024
Description: Convert dataset from YOLO format to COCO format
"""
import os
import cv2
import json
import shutil
class Dataset:
def __init__(self, dataset_name:str, image_path:str, label_path:str):
self.name = dataset_name
self.images_path = image_path
self.labels_path = label_path
self.info = self.Info()
self.licenses = self.Licenses()
self.categories = []
data = Load_YOLO_dataset(image_path, label_path)
self.yolo_images = data["images"]
self.yolo_annotations = data["annotations"]
self.images = []
self.annotations = []
def setCategories(self,cat:list):
for i in cat:
self.categories.append(
{
"id": int(i['id']),
"name": i['name']
}
)
class Info:
def __init__(self):
self.description = ""
self.version = ""
self.year = 2023
self.contributor = ""
self.date_created = ""
class Licenses:
def __init__(self):
self.id = 0
self.name = "License 1.0"
self.url = "http://www"
def extract_info(id:int,image:str,label:str):
img = cv2.imread(image,cv2.IMREAD_UNCHANGED)
height, width = img.shape[:2]
bboxes = []
annotations_info = []
with open(label,'r') as f:
lines = f.readlines()
f.close()
for i in lines:
i = i.replace('\n','')
if len(i.split(' ')) > 3:
obj_class = int(i.split(' ')[0])
x = float(i.split(' ')[1])
y = float(i.split(' ')[2])
w = float(i.split(' ')[3])
h = float(i.split(' ')[4])
bboxes.append(
{
"class": obj_class,
"x": int(x * width - 0.5 * w * width),
"y": int(y * height - 0.5 * h * height),
"w": int(w * width),
"h": int(h * height)
}
)
for i in bboxes:
annotations_info.append({
"id": -1,
"image_id": id,
"category_id": i['class'],
"segmentation": [],
"bbox": [i['x'],i['y'],i['w'],i['h']],
"area": i['w']*i['h'],
"iscrowd": 0
})
img_info = {
"id": id,
"width": height,
"height": width,
"file_name": os.path.basename(image),
"license": 0,
"date_captured": "2023"
}
return img_info, annotations_info
def Load_YOLO_dataset(images_path:str,labels_path:str):
images_files = os.listdir(images_path)
labels_files = os.listdir(labels_path)
images = []
annotations = []
id=0
for i in images_files:
if os.path.splitext(i)[0]+'.txt' in labels_files:
img, ann = extract_info(id,os.path.join(images_path,i),os.path.join(labels_path,os.path.splitext(i)[0]+'.txt'))
images.append(img)
for j in ann:
annotations.append(j)
id = id + 1
id=0
for i in annotations:
i['id'] = id
id = id + 1
data = {
"images" : images,
"annotations" : annotations
}
return data
def ConvertDataset(dataset_name:str, image_path:str, label_path:str, dst_folder:str):
"""
dataset_name:str -> new name for the dataset
image_path:str -> path of images
label_path:str -> path of labels
dst_folder:str -> destination folder
"""
print("Converting dataset",dataset_name,"to folder",dst_folder)
dataset = Dataset(dataset_name, image_path, label_path)
dataset.categories = [{"id": 1,"name": "person"}]
try:
os.makedirs(os.path.join(dst_folder,dataset.name,'images'))
os.makedirs(os.path.join(dst_folder,dataset.name,'annotations'))
except:
pass
#LOAD IMAGES AND ANNOTATIONS
data = Load_YOLO_dataset(image_path,label_path)
dataset.images = data['images']
dataset.annotations = data['annotations']
info = {
"description": dataset.info.description,
"version": dataset.info.version,
"year": dataset.info.year,
"contributor": dataset.info.contributor,
"date_created": dataset.info.date_created
}
categories = dataset.categories
licenses = [{"id": 0,"name": "License 1.0","url": "http://www"}]
images = dataset.images
annotations = dataset.annotations
JSON = {
"info": info,
"categories": categories,
"licenses": licenses,
"images": images,
"annotations": annotations
}
JSON = json.dumps(JSON)
with open(os.path.join(dst_folder,dataset.name,'annotations','annotations.json'), 'w') as f:
f.write(str(JSON))
for i in images:
shutil.copy(os.path.join(dataset.images_path,i['file_name']),os.path.join(dst_folder,dataset.name,'images'))
if __name__ == "__main__":
#USAGE
#ConvertDataset( dataset_name , images_path , labels_path , destination_folder )
# DEI DATASET AUGMENTATION
ConvertDataset('depth',r'DEI\depth\images',r'DEI\depth\labels','DEI-COCO')
ConvertDataset('intensity',r'DEI\intensity\images',r'DEI\intensity\labels','DEI-COCO')
ConvertDataset('rgb',r'DEI\rgb\images',r'DEI\rgb\labels','DEI-COCO')
ConvertDataset('thermal',r'DEI\thermal\images',r'DEI\thermal\labels','DEI-COCO')
# DEEC DATASET AUGMENTATION
ConvertDataset('depth',r'DEEC\depth\images',r'DEEC\depth\labels','DEEC-COCO')
ConvertDataset('intensity',r'DEEC\intensity\images',r'DEEC\intensity\labels','DEEC-COCO')
ConvertDataset('rgb',r'DEEC\rgb\images',r'DEEC\rgb\labels','DEEC-COCO')
ConvertDataset('thermal',r'DEEC\thermal\images',r'DEEC\thermal\labels','DEEC-COCO')