[Yolo V3] BDD, COCO, KITTI dataset to Yolo

데이터 셋 조합하기

Kitti dataset을 Yolo dataset 형태로, BDD100K dataset -> COCO dataset -> Yolo dataset 형태로 변환, BDD100K dataset에서는 traffic light class만 추출해 따로 txt파일을 생성.

 

1. KITTI -> YOLO

$ git clone https://github.com/packyan/PyTorch-YOLOv3-kitti.git
$ cd PyTorch-YOLOv3-kitti/
$ sudo pip3 install -r requirements.txt
$ cd label_transform


 

2. BDD -> COCO

https://github.com/ucbdrive/bdd-data

BDD json file을 COCO json file로 변환

KITTI.names : car, van, truck, pedestrian, person_sitting, cyclist, tram, misc
BDD.names : person, rider, car, bus, truck, bike, motor, traffic light, traffic sign, train

겹치는 category들은 같은 class 번호를 공유하게 하고 KITTI categories 뒤에 BDD categories를 붙였다.

KITTI_BDD.names : car, van, truck, pedestrian, person_sitting, cyclist, tram, misc, person, rider, bus, bike, motor, traffic light, traffic sign, train

 

3. COCO -> YOLO

https://github.com/ssaru/convert2Yolo

BDD dataset의 class에서 뽑아낼 객체는 traffic light이고 names파일에 작성된 class index 순서로 맞춰야 하기 때문에 COCO json으로 변환하면서 BDD category index를 바꿔주는 코드를 추가하였다.

$ python3 example.py --datasets COCO --img_path <image_path> --label <label path or annotation file> --convert_output_path <output path> --img_type “.jpg” --manipast_path <output manipast file path> --cls_list_file <*.names file path>

 

이후 traffic_light[13]만 추출해 jpg파일 이름.txt 파일을 생성 후 좌표값을 저장하고, 해당하는 jpg 파일을 특정 경로에 copy(import shutil)하는 코드를 작성하였다.

import shutil

filename = '/home/moon/darknet/data/bdd_train.txt'
dst_file = '/home/moon/darknet/data/traffic_light'

txt = []
jpg = []

with open(filename, 'r') as f:
	for line in f:
    	jpg.append(line.strip())
        txt.append(line.strip().replace('jpg', 'txt'))
flag = 0

for text in txt:
	with open(text, 'r') as t:
    	src = text
        for line in t:
        	_class = line.strip().split()
            if _class[0] == '13':
            	if flag == 0:
                	shutil.copy(src.replace('txt', 'jpg'), dst_file)
                    flag = 1
                    print(src.replace('txt', 'jpg'))
                    print(dst_file)
                d = open(text[29:], 'a')
                d.write(line+'\n')
                d.close()
        _class = []
        flag = 0
        print("Writing Done!!")

 

결과 이미지 :

 

문제점:

txt 파일을 생성하고 append하는 코드를 작성하였으나 새로 생길 txt가 저장될 경로를 지정해주었더니 txt파일이 없다고 나왔다. append옵션은 없는 txt파일을 생성하는 기능도 있는데 왜 경로를 지정해주면 error가 나오는지 모르겠다. python project경로에 생성 후 저장되는 코드는 잘 돌아갔다. txt 파일들을 저장해 준 후 terminal창에서 파일에 옮기는 명령을 해줘야 했다는 점이 아쉬웠다.

 

'Project > Project' 카테고리의 다른 글

[Yolo V3] 편의점 물품 객체 인식  (1) 2020.06.04