把数据集转换成coco格式
时间: 2023-09-20 15:03:30 浏览: 148
data转coco格式
COCO(Common Objects in Context)是一种通用的目标检测、分割和图像标注数据集的格式,它包含了很多现有的图像数据集。如果需要将数据集转换成COCO格式,可以按照以下步骤进行:
1. 安装cocoapi
在终端中输入以下命令:
```
pip install cython
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
```
2. 准备数据
将数据集整理成如下格式:
```
dataset/
- train/
- image1.jpg
- image2.jpg
- ...
- annotations.json
- val/
- image1.jpg
- image2.jpg
- ...
- annotations.json
```
其中,annotations.json的格式如下:
```
{
"info": {...},
"licenses": [...],
"images": [
{
"id": ,
"width": ,
"height": ,
"file_name": ,
"license": ,
"flickr_url": ,
"coco_url": ,
"date_captured":
},
...
],
"annotations": [
{
"id": ,
"image_id": ,
"category_id": ,
"segmentation": ,
"area": ,
"bbox": ,
"iscrowd":
},
...
],
"categories": [
{
"id": ,
"name": ,
"supercategory":
},
...
]
}
```
其中,categories为类别信息,其中id为类别标识符,name为类别名称,supercategory为类别父类别(可选)。images为图片信息,其中id为图片标识符,width和height为图片尺寸,file_name为图片文件名,license为许可证信息(可选),flickr_url和coco_url为图片来源链接(可选),date_captured为图片拍摄日期(可选)。annotations为标注信息,其中id为标注标识符,image_id为图片标识符,category_id为类别标识符,segmentation为标注的分割信息(可选),area为标注的面积(可选),bbox为标注的边界框信息(可选),iscrowd为标注是否为群体(可选)。
3. 将数据集转换成COCO格式
在终端中输入以下命令:
```
python create_coco_dataset.py dataset/ train
python create_coco_dataset.py dataset/ val
```
其中,create_coco_dataset.py为如下脚本:
```python
import os
import json
import argparse
from PIL import Image
def create_coco_dataset(root_dir, split):
images = []
annotations = []
categories = []
# Load categories
categories_list = ["category1", "category2", "category3"]
for i, category_name in enumerate(categories_list):
categories.append({
"id": i+1,
"name": category_name,
"supercategory": ""
})
# Load images and annotations
annotations_dir = os.path.join(root_dir, split, "annotations")
for filename in os.listdir(annotations_dir):
if filename.endswith(".json"):
with open(os.path.join(annotations_dir, filename), "r") as f:
data = json.load(f)
for image_info in data["images"]:
image_path = os.path.join(root_dir, split, image_info["file_name"])
image = Image.open(image_path)
width, height = image.size
images.append({
"id": image_info["id"],
"width": width,
"height": height,
"file_name": image_info["file_name"],
"license": 0,
"flickr_url": "",
"coco_url": "",
"date_captured": ""
})
for annotation_info in data["annotations"]:
annotations.append({
"id": annotation_info["id"],
"image_id": annotation_info["image_id"],
"category_id": annotation_info["category_id"],
"segmentation": annotation_info.get("segmentation", []),
"area": annotation_info.get("area", 0),
"bbox": annotation_info.get("bbox", []),
"iscrowd": annotation_info.get("iscrowd", 0),
})
# Create COCO dataset
dataset = {
"info": {
"description": "",
"url": "",
"version": "",
"year": "",
"contributor": "",
"date_created": ""
},
"licenses": [
{
"id": 0,
"name": "",
"url": ""
}
],
"images": images,
"annotations": annotations,
"categories": categories
}
# Save COCO dataset
coco_dir = os.path.join(root_dir, "coco")
if not os.path.exists(coco_dir):
os.makedirs(coco_dir)
with open(os.path.join(coco_dir, f"{split}.json"), "w") as f:
json.dump(dataset, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("root_dir", type=str, help="root directory of dataset")
parser.add_argument("split", type=str, choices=["train", "val"], help="dataset split")
args = parser.parse_args()
create_coco_dataset(args.root_dir, args.split)
```
该脚本将数据集转换成COCO格式,并生成train.json和val.json文件,保存在root_dir/coco目录中。
4. 使用COCO格式的数据集
可以使用COCO API或者第三方库(如detectron2)读取和使用COCO格式的数据集。
阅读全文