目录

需求

coco格式

python代码实现

整理不易,欢迎一键三连!!!

送你们一条美丽的--分割线--

需求

        单波段灰度图label转coco格式的JSON文件

 

coco格式

        COCO 数据集的格式是基于 JSON(JavaScript Object Notation)的,使用一个主 JSON 文件来描述整个数据集,以及其他辅助的 JSON 文件来存储图像和标注等信息。在 COCO 数据集格式中,主要的 JSON 文件通常被称为 "instance" 文件,它包含了以下重要字段:

 "info" 字段:用于描述数据集的基本信息,例如名称、版本、年份等。"license" 字段:包含了数据集使用的许可证信息,可以指定数据集的使用和共享方式。"images" 字段:存储了每张图像的相关信息,包括图像的唯一标识符、文件名、高度、宽度等。"annotations" 字段:记录了每个图像的标注信息,每个标注包含了一些必要的信息,如标注的唯一标识符、所属图像的标识符、类别、边界框信息等。"categories" 字段:定义了数据集中所有类别的信息,包括类别的唯一标识符、名称等。 

                       

        coco格式的JSON文件包含了一个COCO数据集的基本信息。它包括了数据集的信息(年份、版本、描述等)、许可证信息、图像信息(宽度、高度、文件名等)、标注信息(标注ID、图像ID、类别ID、边界框、面积等)以及类别信息(类别ID、类别名称、超类别等)。

        COCO数据格式的JSON文件示例:

{

"info": {

"year": 2021,

"version": "1.0",

"description": "COCO Dataset Example",

"contributor": "John Doe",

"url": "http://example.com",

"date_created": "2021-06-01"

},

"licenses": [

{

"id": 1,

"name": "License 1",

"url": "http://example.com/license1"

},

{

"id": 2,

"name": "License 2",

"url": "http://example.com/license2"

}

],

"images": [

{

"id": 1,

"width": 500,

"height": 400,

"file_name": "image1.jpg",

"license": 1,

"date_captured": "2021-01-01"

},

{

"id": 2,

"width": 800,

"height": 600,

"file_name": "image2.jpg",

"license": 2,

"date_captured": "2021-02-01"

}

],

"annotations": [

{

"id": 1,

"image_id": 1,

"category_id": 1,

"bbox": [100, 100, 200, 200],

"area": 40000,

"iscrowd": 0

},

{

"id": 2,

"image_id": 1,

"category_id": 2,

"bbox": [300, 200, 150, 250],

"area": 37500,

"iscrowd": 1

},

{

"id": 3,

"image_id": 2,

"category_id": 1,

"bbox": [50, 50, 300, 400],

"area": 120000,

"iscrowd": 0

}

],

"categories": [

{

"id": 1,

"name": "cat",

"supercategory": "animal"

},

{

"id": 2,

"name": "dog",

"supercategory": "animal"

}

]

}

原文链接:https://blog.csdn.net/qq_38308388/article/details/136377164 

python代码实现

        python源码实现:

import json

import numpy as np

def label2coco(gray_label, image_id):

# 创建COCO数据格式的字典

coco_data = {

"info": {},

"licenses": [],

"images": [],

"annotations": [],

"categories": []

}

# 添加图像信息

image_info = {

"id": image_id,

"width": gray_label.shape[1],

"height": gray_label.shape[0],

"file_name": "image.jpg",

}

coco_data["images"].append(image_info)

# 添加类别信息

category_info = {

"id": 1,

"name": "object",

"supercategory": "none"

}

coco_data["categories"].append(category_info)

# 转换标签数据为COCO格式

label_data = np.squeeze(gray_label)

unique_labels = np.unique(label_data)

for label in unique_labels:

if label == 0:

continue

mask = label_data == label

annotation_info = {

"id": label,

"image_id": image_id,

"category_id": 1,

"segmentation": [],

"bbox": [],

"area": int(np.sum(mask)),

"iscrowd": 0

}

coco_data["annotations"].append(annotation_info)

return coco_data

# 示例用法

gray_label = np.array([[0, 0, 1, 1],

[0, 2, 1, 1],

[0, 2, 2, 1]])

coco_data = label2coco(gray_label, image_id=1)

json.dump(coco_data, open("coco_data.json", "w"))

        上述代码实现了将单波段灰度图标签转换为COCO格式的JSON文件的功能。需要将标签存储为一个二维的numpy数组(gray_label),其中每个元素代表一个像素的类别信息。

        在示例中,灰度图标签为:[[0, 0, 1, 1], [0, 2, 1, 1], [0, 2, 2, 1]],其中0表示背景,1和2表示两个不同的物体。image_id为1,表示图像的唯一标识。

        函数label2coco接受灰度标签和图像ID作为输入,并返回COCO数据格式的字典。最后,将该字典存储为JSON文件(coco_data.json)。

        注意:该代码只实现了将灰度图标签转换为COCO格式的基本功能,可以根据实际需要进行修改和扩展。

整理不易,欢迎一键三连!!!

送你们一条美丽的--分割线--

⛵⛵⭐⭐

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: