coco格式数据集可视化 之 分割标签可视化

靠数据做事当然首先要知道数据是什么样的

本文的可视化是基于pycocotools来实现的(有方便的轮子为什么不用),网上的大多数教程就是把pycocotools的demo拿来说了一下,而demo中标签的可视化都需要指定一个类,只显示这个类的标签,但我想可视化一张图片中所有目标的标签该怎么办。。。。

一时半会在网上没找到想要的 👉 自己写一个算了 👉 所以有了本文

默认已经安装了pycocotools

直接上代码:

下面的代码可以直接可视化 train的标签,其中’test‘模式是为了打比赛的时候测试集没有label, 检测完的结果文件和train.json格式有所差异(只是个List,而不是pycocotools要求的字典,还需要添加{'images': ...},{'annotations': ...},等等),所以在test模式下还需要进行格式转换。

import os
from pycocotools.coco import COCO
from skimage import io
from matplotlib import pyplot as plt
import json
import cv2

if __name__=='__main__':

    mode = 'test'

    test_json_file = '你的路径/data/test/annotations/test.json'
    train_json_file = '你的路径/data/train/annotations/train.json'
	
	# train模式下  json_file 与train_json_file 相同
	# test模式下   json_file 是你的检测结果文件
    json_file = '你的路径/xx.json' 
	
	# test模式下格式转换完后的结果文件,train模式下用不到
    newJsonSaveName = '你的路径/result/test_result_visual.json'

    imgsPath = '你的路径/data/train/images/'

    jsonFile = json.load(open(json_file))
    
    # 测试结果可视化
    if mode == 'test':
        imgsPath = '你的路径/data/test/images/'
        test_json_file = json.load(open(test_json_file))

        for i,_ in enumerate(jsonFile):
            jsonFile[i]['id'] = i
            jsonFile[i]['iscrowd'] = 0

        test_json_file['annotations'] = jsonFile
        jsonFile = test_json_file.copy()
        json_str = json.dumps(jsonFile, indent=4)
        with open(newJsonSaveName, 'w') as json_file:
            json_file.write(json_str)
        json_file = newJsonSaveName

    classId2className = jsonFile['categories']
    imId2imageNames = {}
    for imgs in jsonFile['images']:
        imId2imageNames[imgs['id']] = imgs['file_name']
    imgId2labels = {}
    for label in jsonFile['annotations']:
        imgId = label['image_id']
        if imgId not in imgId2labels:
            imgId2labels[imgId] = []
        imgId2labels[imgId].append(label)

    coco = COCO(json_file)
    for imgId in imgId2labels:
        imgName = imId2imageNames[imgId]
        img = io.imread(imgsPath + imgName)

        # for label in imgId2labels[imgId]:
        plt.axis('off')
        plt.imshow(img)  # 绘制图像,显示交给plt.show()处理
        coco.showAnns(imgId2labels[imgId])

        plt.show()  # 显示图像

最终效果如下所示:
在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
根引用的内容,对于COCO数据集可视化可以分为三部分:标注框bbox的单独可视化分割标注segmentation的单独可视化以及bbox和segmentation的联合可视化。 对于标注框bbox的单独可视化,可以使用OpenCV或COCO API来实现。使用OpenCV的话,可以读取图像和对应的标注文件,然后利用OpenCV的绘制函数将标注框绘制在图像上。另外,也可以使用COCO API来读取JSON格式的标注文件,然后按照API提供的函数进行可视化。 对于分割标注segmentation的单独可视化,可以使用相似的方法。可以读取图像和对应的标注文件,然后根据标注的分割信息,使用OpenCV或其他图像处理库将分割结果可视化出来。 对于bbox和segmentation的联合可视化,可以将标注框和分割结果叠加在图像上,以更清晰地展示目标物体的位置和形状。 此外,还可以加载对应照片的语义分析数据,并根据需要进行打印或展示。可以使用COCO API加载对应的语义分析数据,并通过调用API提供的函数来展示数据。 综上所述,COCO数据集可视化可以通过加载图像和对应的标注文件,利用OpenCV、COCO API或其他图像处理库来实现标注框和分割结果的可视化,并可以根据需要加载并展示照片的语义分析数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [COCO数据集annotations解析以及可视化](https://blog.csdn.net/ZitaoWang/article/details/119824877)[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_2"}}] [.reference_item style="max-width: 50%"] - *2* [COCO数据集可视化程序(包括bbox和segmentation)](https://blog.csdn.net/wtandyn/article/details/109751015)[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_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值