【数据集】LISA Traffic Light Dataset

 前言

 

LISA数据集简介(有交通标志数据集)

 

LISA转COCO格式

 

LISA转COCO128格式

 lisa2coco128.sh

#!/bin/bash
# Example usage: sh LISA2COCO128.sh -f ../../LISA_Traffic_Light_Dataset
# parent
# ├── new_dataset_coco128
# │   ├── images
# │   └── labels
# ├── LISA_Traffic_Light_Dataset
# │   ├── Annotations
# │   ├── daySequence1
# │   ├── daySequence2
# │   ├── dayTrain
# │   ├── script


usage() { echo "Usage: $0 -f <dataset main folder RELATIVE path>" 1>&2; exit 1; }

if [ "$#" -eq 0 ]
then
  usage
  exit 2
fi

while getopts "f:" o; do
    case "${o}" in
        f)
            dataset_folder=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

if [ -z "${dataset_folder}" ]; then
    usage
fi

script_path="$(pwd)"
new_dataset="Lisa2coco128"
echo "Converting Lisa dataset from folder ${dataset_folder}"
cd "${dataset_folder}"
mkdir ../${new_dataset}
cd ../${new_dataset}
mkdir allimg labels images
cd labels
mkdir train valid
cd ../images
mkdir train valid
cd "${dataset_folder}"

# Just day image.
# daySequence1 and nightSequence1 are the testing images
for f in `find . -type f -path '*daySequence1*/*' -name '*jpg'`
do
   # echo "Moving ${f} to test folder"
   # mv $f images/valid
   # echo $f
   cp $f ../${new_dataset}/allimg
done
echo "Copy to test folder"
# Everything else is for training
for f in `find . -type f -path '*dayTrain*/*' -name '*jpg'`
do
   # echo "Moving ${f} to training folder"
   # mv $f images/train
   cp $f ../${new_dataset}/allimg
done
echo "Copy to test folder"
for f in `find . -type f -path '*daySequence2*/*' -name '*jpg'`
do
   # echo "Moving ${f} to training folder"
   # mv $f images/train
   cp $f ../${new_dataset}/allimg
done
echo "Copy to test folder"

echo "Merging all annotations into a single file..."
cd "$script_path"
python3 merge_csv_data.py "${dataset_folder}"
mv training_data.csv "${dataset_folder}"
mv test_data.csv "${dataset_folder}"
python csv2coco128.py "${dataset_folder}"
cd "${dataset_folder}"
mv training_data.csv ../${new_dataset}/labels
mv test_data.csv     ../${new_dataset}/labels

# rm -rf ../${new_dataset}/allimg
echo 'Finished!'
View Code

merge_csv_data.py

import pandas as pd
import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument('folder_path', type=str, help='Dataset main folder RELATIVE path')
args = parser.parse_args()
DATASET_PATH = args.folder_path

def get_folders(annotations=False):
    # Getting dataset root path
    dir_path = os.path.dirname(os.path.realpath(__file__))
    lisa_path = os.path.join(dir_path, DATASET_PATH)
    # print('dir_path: ', dir_path)
    # print('lisa_path: ', lisa_path)

    # Image folders names
    # Not selecting night images to speed up training
    #day_train_folders   = [os.path.join('dayTrain', f'dayClip{i}') for i in range(1, 14)]
    #night_train_folders = [os.path.join('nightTrain', f'nightClip{i}') for i in range(1, 6)]
    #train_folders = day_train_folders + night_train_folders + ['daySequence2', 'nightSequence2']
    #test_folders        = ['daySequence1', 'nightSequence1']

    day_train_folders   = [os.path.join('dayTrain', f'dayClip{i}') for i in range(1, 14)]
    train_folders       = day_train_folders + ['daySequence2']
    test_folders        = ['daySequence1']
    # print('day_train_folders: ', day_train_folders)
    # print('train_folders: ', train_folders)
    # print('test_folders: ', test_folders)

    if annotations:
        train_folders = [os.path.join(lisa_path, 'Annotations', 'Annotations', i) for i in train_folders]
        test_folders = [os.path.join(lisa_path, 'Annotations', 'Annotations', i) for i in test_folders]
    else:
        train_folders = [os.path.join(lisa_path, i) for i in train_folders]
        test_folders = [os.path.join(lisa_path, i) for i in test_folders]

    return train_folders, test_folders


def get_full_csv():
    columns = ['Filename', 'Annotation tag', 'Upper left corner X', 'Upper left corner Y', 'Lower right corner X', 
        'Lower right corner Y', 'Origin file', 'Origin frame number', 'Origin track', 'Origin track frame number']
    train_df = pd.DataFrame(columns=columns)
    test_df = pd.DataFrame(columns=columns)

    train_folders, test_folders = get_folders(annotations=True)

    for f in train_folders:
        new_df = pd.read_csv(os.path.join(f, 'frameAnnotationsBOX.csv'), sep=';')
        # print(f'Processing {os.path.join(f, "frameAnnotationsBOX.csv")} training file with {new_df.shape[0]} rows.')
        train_df = train_df.merge(new_df, how='outer', left_on=columns, right_on=columns)

    train_df.to_csv('training_data.csv', index=False)
    print(f'Finished processing {train_df.shape[0]} rows')

    for f in test_folders:
        new_df = pd.read_csv(os.path.join(f, 'frameAnnotationsBOX.csv'), sep=';')
        # print(f'Processing {os.path.join(f, "frameAnnotationsBOX.csv")} test file with {new_df.shape[0]} rows.')
        test_df = test_df.merge(new_df, how='outer', left_on=columns, right_on=columns)

    test_df.to_csv('test_data.csv', index=False)
    print(f'Finished processing {test_df.shape[0]} rows')

get_full_csv()
View Code

lisa2coco128.py

import pandas as pd
import argparse
import json
import os
import shutil

parser = argparse.ArgumentParser()
parser.add_argument('folder_path', type=str, help='Dataset main folder RELATIVE path')
args = parser.parse_args()
DATASET_PATH = args.folder_path
classes = ['go', 'goForward', 'goLeft', 'stop', 'stopLeft', 'warning', 'warningLeft']


def get_bbox(size, box):
    # Convert LISA box to YOLO xywh box
    dw = 1. / size[0]
    dh = 1. / size[1]
    xc = ((box[0] + box[2])/2)*dw
    yc = ((box[1] + box[3])/2)*dh
    w = (box[2]-box[0])*dw
    h = (box[3]-box[1])*dh
    return xc, yc, w, h


def csv2coco128(train=True):
    # Getting dataset root path
    dir_path = os.path.dirname(os.path.realpath(__file__))
    lisa_path = os.path.join(dir_path, DATASET_PATH)
    new_dataset = 'Lisa2coco128'
    new_path = os.path.join(lisa_path, '..', new_dataset)
    if train:
        labels_path = os.path.join(new_path, 'labels', 'train')
        images_path = os.path.join(new_path, 'images', 'train')
        df = pd.read_csv(os.path.join(lisa_path, 'training_data.csv'), index_col=False)
    else:
        labels_path = os.path.join(new_path, 'labels', 'valid')
        images_path = os.path.join(new_path, 'images', 'valid')
        df = pd.read_csv(os.path.join(lisa_path, 'test_data.csv'), index_col=False)

    # Converting the dataframe into a list of lists
    data = df.values.tolist()

    # Each element in data is a 11-element list with the following fields:
    # 0. Filename
    # 1. Annotation tag
    # 2. Upper left corner X
    # 3. Upper left corner Y
    # 4. Lower right corner X 
    # 5. Lower right corner Y
    # 6. Origin file
    # 7. Origin frame number
    # 8. Origin track
    # 9. Origin track frame number
    img_size = (1280, 960)
    for i, d in enumerate(data, 1):
        filename = d[0].rsplit('/')[1]
        # copy images
        shutil.copy(os.path.join(new_path, 'allimg', filename), images_path)


        for i, v in enumerate(classes, 0):
            if(d[1] == v):
                classid = i
                break
        # bbox
        bbox = get_bbox(img_size, d[2:6])
        info = f"{classid} {' '.join(f'{x:.6f}' for x in bbox)}\n"
        labelname = os.path.join(labels_path, filename.replace('jpg', 'txt'))
        labelfile = open(labelname, 'a+')
        labelfile.write(info)
        labelfile.close()

if __name__ == "__main__":
    csv2coco128(train=True)
    csv2coco128(train=False)
View Code

 

参考

1. kaggle_LISA Traffic Light Dataset;

2. Traffic Light Detection: A Learning Algorithm and Evaluations on Challenging Dataset;

3. LISA_COCO_converter;

posted on 2022-06-16 19:37  鹅要长大  阅读(887)  评论(0编辑  收藏  举报

导航