cvat标注文件转为mmdetection可以训练的格式

cvat标注文件注意事项:
1、group:目标成组出现,比如自行车和骑车的人
2、occluded/iscrowd:被遮挡,没有完全显示的目标。我认为cvat标注的时候是occluded,但mmdetection使用的关键字是iscrowd.
cvat标注矩形框用于目标检测,导出MOT1.1格式数据,将这种格式数据转为mmdetection能够识别的一种格式。
一个txt按图片拆分多个txt,改变矩形框数据格式

import os
import mmcv
file_path="/home/mby/mmtracking/data/MOTWOOD/train/MOT-07/img1/"
list_name=os.listdir(file_path)

lines=mmcv.list_from_file('/home/mby/mmtracking/data/MOTWOOD/train/MOT-07/gt/gt.txt')
contents=[line.strip().split(',') for line in lines]

last_name='---'
fo=None
for content in contents:
    this_name=(format(int(content[0]),'06d')+'.PNG')
    if( this_name in list_name):
        if(this_name!=last_name):
            if(fo!=None):
                fo.close()
            fo = open('demo/label/'+format(int(content[0]),'06d') + '.txt', "w")
            last_name = this_name
        content[4] = str(float(content[2]) + float(content[4]))
        content[5] = str(float(content[3]) + float(content[5]))
        line_content = ','.join(content)
        fo.write(line_content + '\n')

mmdetecion在使用自定义数据之前需要注册,下面是数据注册程序,需要放在数据训练程序中。


import os.path as osp
import mmcv
import numpy as np

from mmdet.datasets.builder import DATASETS
from mmdet.datasets.custom import CustomDataset

@DATASETS.register_module()
class WoodDataset(CustomDataset):
    CLASSES = ('Wood',)
    def load_annotations(self, ann_file):
        cat2label = {k: i for i, k in enumerate(self.CLASSES)}
        # load image list from file
        image_list = mmcv.list_from_file(self.ann_file)
        data_infos = []
        # convert annotations to middle format
        for image_id in image_list:
            filename = f'{self.img_prefix}/{image_id}.PNG'
            image = mmcv.imread(filename)
            height, width = image.shape[:2]
            data_info = dict(filename=f'{image_id}.PNG', width=width, height=height)
            # load annotations
            label_prefix = self.img_prefix.replace('image_2', 'label_2')
            lines = mmcv.list_from_file(osp.join(label_prefix, f'{image_id}.txt'))

            content = [line.strip().split(',') for line in lines]
            bbox_names = [x[0] for x in content]
            bboxes = [[float(info) for info in x[2:6]] for x in content]
            gt_bboxes = []
            gt_labels = []
            gt_bboxes_ignore = []
            gt_labels_ignore = []
            for bbox in bboxes:
                gt_labels.append(0)
                gt_bboxes.append(bbox)
            data_anno = dict(
                bboxes=np.array(gt_bboxes, dtype=np.float32).reshape(-1, 4),
                labels=np.array(gt_labels, dtype=np.long),
                bboxes_ignore=np.array(gt_bboxes_ignore,
                                       dtype=np.float32).reshape(-1, 4),
                labels_ignore=np.array(gt_labels_ignore, dtype=np.long))

            data_info.update(ann=data_anno)
            data_infos.append(data_info)

        return data_infos

这是我尝试成功的一种方式,可能会有很多更好的转换方法。

你可能感兴趣的:(mmdtection,深度学习,人工智能,python)