coco数据集制作-多文件夹

背景:标准的coco格式数据集需要把所有图片放到一个文件夹里,而很多情况下,会有很多个文件夹,我们并不想把所有图片放到一起。

本文把多个文件夹下的yolov8(5)的txt标签转换至coco标签,转换标签代码如下:

import os
import json
import cv2

# 图片和标签路径
img_root = './soccernet/tracking/images/test/'
label_root = './soccernet/tracking/labels/test/'
# 模板
coco = {
    "info": {
        "year": 2023,
        "version": "1.0",
        "description": "Example COCO Dataset",
        "contributor": "jefft",
        "date_created": "2023-08-22"
    },
    "licenses": [
        {
            "id": 1,
            "name": "License Name",
            "url": "https://license-url.com"
        }
    ],
    "categories": [
        {
            "id": 0,
            "name": "person",
        },
        {
            "id": 1,
            "name": "soccer",
        }
    ],
    "images": [ 
    ],
    "annotations": [
    ]
}

image_tp = {
            "id": 1,
            "width": 640,
            "height": 480,
            "file_name": "cat1.jpg",
            "license": 1
        }

anno_tp = {
            "id": 1,
            "image_id": 1,
            "category_id": 1,
            "bbox": [],
            "area": 0,
            "segmentation": [],
            "iscrowd": 0
        }

idx = 0
img_id_= 0 
for root, dirs, files in os.walk(label_root):
    
    for file in files:
        if file.endswith('txt'): # 遍历所有yolov8格式的txt标注文件
            txt_path = os.path.join(root,file)
            print("Current directory:", txt_path)
            img_path = txt_path.replace('labels','images')
            img_path = img_path.replace('.txt','.jpg') # 找到图片路径
            if 'old' not in img_path: # 不用管,自行修改
                anno = open(txt_path).read().splitlines()
                img = cv2.imread(img_path)
                h,w,_ = img.shape
                
                image_tp["id"] = idx
                image_tp["width"] = w
                image_tp["height"] = h
                image_tp["file_name"] = img_path # 写入完整路径
                
                coco["images"].append(image_tp.copy()) # 添加图片信息
                for a in anno:
                    l = a.split(' ')
                    cat,cx,cy,lw,lh = int(l[0]),float(l[1])*w,float(l[2])*h,float(l[3])*w,float(l[4])*h
                    anno_tp["id"] = img_id_
                    anno_tp["image_id"] = img_path
                    img_id_+=1
                    anno_tp["bbox"] = [cx-lw/2,cy-lh/2,lw,lh] # 转换标注格式
                    anno_tp["category_id"] = cat
                    anno_tp["area"] = lw*lh
                    coco["annotations"].append(anno_tp.copy())  # 添加标注信息
                idx+=1
                assert os.path.exists(img_path)
                
    # if idx>500:
    #     break

with open('./test_soccer_coco.json', 'w') as l:
    l.write(json.dumps(coco))

验证是否转换正确代码如下:

from pycocotools.coco import COCO
import os
import cv2
import numpy as np

# 替换为你的数据集标注文件的路径和图像文件夹路径
annotation_file = 'test_soccer_coco.json'
image_folder = ''

# 初始化COCO对象
coco = COCO(annotation_file)

idx = 0
# 遍历每个图像并绘制标注
for image_id in coco.getImgIds():
    image_info = coco.loadImgs(image_id)[0]
    image_path = os.path.join(image_folder, image_info['file_name'])
    image = cv2.imread(image_path)

    annotations = coco.loadAnns(coco.getAnnIds(imgIds=[image_info['file_name']]))  # 原来是imgIds=image_id,进行了修改

    for ann in annotations:
        bbox = ann['bbox']
        category_info = coco.loadCats(ann['category_id'])[0]
        category_name = category_info['name']

        # 在图像上绘制边界框
        x, y, w, h = map(int, bbox)
        if category_info['id'] == 0:
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(image, category_name, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        else:
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(image, category_name, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    # 保存绘制标注后的图像
   
    cv2.imwrite('tmp/{}.jpg'.format(idx), image)
    idx+=1
print("Annotation visualization and saving complete.")

另外,使用别人的代码训练的时候可能需要修改,就比如DAMO-YOLO图中的位置:

在这里插入图片描述

另外还需要修改以下文件,测试的时候map才不会等于-1.
在这里插入图片描述

       def _prepare(self):
        '''
        Prepare ._gts and ._dts for evaluation based on params
        :return: None
        '''
        def _toMask(anns, coco):
            # modify ann['segmentation'] by reference
            for ann in anns:
                rle = coco.annToRLE(ann)
                ann['segmentation'] = rle
        p = self.params
        
        '''
        tfj modi:
        '''
        mflag = False # true说明是自己制作的完整路径的数据集
        for tid,tit in self.cocoGt.imgs.items():
            if '/' in tit['file_name']:
                mflag = True
            break
        file_path = []
        if mflag:
            for im in range(len(self.cocoGt.imgs)):
                file_path.append(self.cocoGt.imgs[im]['file_name'])
            if p.useCats:
                gts=self.cocoGt.loadAnns(self.cocoGt.getAnnIds(imgIds=file_path, catIds=p.catIds))
                dts=self.cocoDt.loadAnns(self.cocoDt.getAnnIds(imgIds=p.imgIds, catIds=p.catIds))
            else:
                gts=self.cocoGt.loadAnns(self.cocoGt.getAnnIds(imgIds=file_path))
                dts=self.cocoDt.loadAnns(self.cocoDt.getAnnIds(imgIds=p.imgIds))
        else:
            if p.useCats:
                gts=self.cocoGt.loadAnns(self.cocoGt.getAnnIds(imgIds=p.imgIds, catIds=p.catIds))
                dts=self.cocoDt.loadAnns(self.cocoDt.getAnnIds(imgIds=p.imgIds, catIds=p.catIds))
            else:
                gts=self.cocoGt.loadAnns(self.cocoGt.getAnnIds(imgIds=p.imgIds))
                dts=self.cocoDt.loadAnns(self.cocoDt.getAnnIds(imgIds=p.imgIds))

        # convert ground truth to mask if iouType == 'segm'
        if p.iouType == 'segm':
            _toMask(gts, self.cocoGt)
            _toMask(dts, self.cocoDt)
        # set ignore flag
        for gt in gts:
            gt['ignore'] = gt['ignore'] if 'ignore' in gt else 0
            gt['ignore'] = 'iscrowd' in gt and gt['iscrowd']
            if p.iouType == 'keypoints':
                gt['ignore'] = (gt['num_keypoints'] == 0) or gt['ignore']
        self._gts = defaultdict(list)       # gt for evaluation
        self._dts = defaultdict(list)       # dt for evaluation
        for gt in gts:
            if mflag:  # tfj modi
                tmp_id = file_path.index(gt['image_id'])
                self._gts[tmp_id, gt['category_id']].append(gt)
            else:
                self._gts[gt['image_id'], gt['category_id']].append(gt)
        for dt in dts:
            self._dts[dt['image_id'], dt['category_id']].append(dt)
        self.evalImgs = defaultdict(list)   # per-image per-category evaluation results
        self.eval     = {}                  # accumulated evaluation results 

有用的话点个赞哦😄

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
制作coco数据集的txt转json,你可以参考以下步骤: 1. 首先,你需要创建一个用于储存数据文件夹,这个文件夹应该包括以下几个文件夹:images、annotations和labels。其中,images文件夹存放所有的图像文件,annotations文件夹存放所有的标注文件,labels文件夹存放类别标签文件。 2. 然后,你需要将图像文件复制到images文件夹中,并在annotations文件夹中创建一个空的json文件,用于存放最后生成的coco数据集格式的标注信息。 3. 接下来,你可以编写一个脚本来将txt转换为json格式。你可以使用Python中的json库来处理json数据。在脚本中,你需要读取txt文件,并将其转换为对应的json数据格式,然后写入到annotations文件夹中的json文件中。你可以参考引用中提到的资料中的代码示例来实现。 4. 最后,你可以使用cocoapi来验证生成的json文件是否符合coco数据集的格式要求。你可以使用cocoapi中的pycocotools库来进行验证。具体的验证步骤可以参考引用中提到的资料。 总结一下步骤: 1. 创建用于储存数据文件夹,包括images、annotations和labels文件夹。 2. 将图像文件复制到images文件夹中,并创建一个空的json文件用于存放标注信息。 3. 编写一个脚本来将txt转换为json格式,并将其写入到annotations文件夹中的json文件中。 4. 使用cocoapi中的pycocotools库来验证生成的json文件是否符合coco数据集的格式要求。 希望以上信息对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【制作coco数据集】](https://blog.csdn.net/BITCCK/article/details/126061350)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [目标检测数据制作.zip](https://download.csdn.net/download/baidu_38876334/87935532)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值