-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelstudio_yolo_converter.py
52 lines (43 loc) · 1.68 KB
/
labelstudio_yolo_converter.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
import json
import os
import shutil
import random
class_map = {'Larb': 0, 'Pidan': 1, 'Balut': 2, 'Souffle': 3}
with open('data/raw/raw_label.json', 'r') as f:
raw = json.load(f)
for item in raw:
f_name = item['file_upload'].split('.')[0] + '.txt'
boxes = []
for box in item['annotations'][0]['result']:
x = box['value']['x']
y = box['value']['y']
assert x >= 0
assert y >= 0
width = box['value']['width']
height = box['value']['height']
width = min(width, 100 - x)
height = min(height, 100 - y)
assert x + width <= 100
assert y + height <= 100
duck = class_map[box['value']['rectanglelabels'][0]]
boxes.append(
' '.join(map(str, [duck, (x + width / 2) / 100, (y + height / 2) / 100, width / 100, height / 100])))
with open(f'data/all_labels/{f_name}', 'w') as f:
f.write('\n'.join(boxes))
shutil.rmtree('data/images')
shutil.rmtree('data/labels')
all_images = os.listdir('data/all_images')
random.seed(111)
random.shuffle(all_images)
os.makedirs('data/images/val')
os.makedirs('data/labels/val')
os.makedirs('data/images/train')
os.makedirs('data/labels/train')
for img in all_images[:30]:
label = img.split('.')[0] + '.txt'
shutil.copy(os.path.join('data/all_images', img), os.path.join('data/images/val', img))
shutil.copy(os.path.join('data/all_labels', label), os.path.join('data/labels/val', label))
for img in all_images[30:]:
label = img.split('.')[0] + '.txt'
shutil.copy(os.path.join('data/all_images', img), os.path.join('data/images/train', img))
shutil.copy(os.path.join('data/all_labels', label), os.path.join('data/labels/train', label))