栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

MS COCO数据集

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MS COCO数据集

参考链接

一、使用Python的json库查看 二、读取每张图片的bbox信息
import os
from pycocotools.coco import COCO
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt

json_path = "D:/pywork/labelme2coco/data_dataset_coco/annotations.json"
img_path = "D:/pywork/labelme2coco/data_dataset_coco"

# load coco data
coco = COCO(annotation_file=json_path)

执行完以上代码后,控制台打印出以下内容:

coco.loadImgs(img_id)
这个返回的结果是一个list, [0]表示取list中第一个元素,[‘file_name’]取出当前图片的名字。
import os
from pycocotools.coco import COCO
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt

json_path = "D:/pywork/labelme2coco/data_dataset_coco/annotations.json"
img_path = "D:/pywork/labelme2coco/data_dataset_coco"

# load coco data
coco = COCO(annotation_file=json_path)

# get all image index info
ids = list(sorted(coco.imgs.keys()))
print("number of images: {}".format(len(ids)))

# get all coco class labels
coco_classes = dict([(v["id"], v["name"]) for k, v in coco.cats.items()])

# 遍历前三张图像
for img_id in ids[:3]:
    # 获取对应图像id的所有annotations idx信息
    ann_ids = coco.getAnnIds(imgIds=img_id)

    # 根据annotations idx信息获取所有标注信息
    targets = coco.loadAnns(ann_ids)

    # get image file name
    path = coco.loadImgs(img_id)[0]['file_name']

    # read image
    img = Image.open(os.path.join(img_path, path)).convert('RGB')
    draw = ImageDraw.Draw(img)
    # draw box to image
    for target in targets:
        x, y, w, h = target["bbox"]
        x1, y1, x2, y2 = x, y, int(x + w), int(y + h)
        draw.rectangle((x1, y1, x2, y2), outline='red')
        draw.text((x1, y1), coco_classes[target["category_id"]])

    # show image
    plt.imshow(img)
    plt.show()

ImageDraw.Draw.rectangle()绘制一个矩形
outline-用于轮廓的颜色。
fill-用于填充的颜色
ps:默认情况绘制的矩形框显示的是白色的,由于图片背景和白色相近,刚开始都没发现画了框……

三、读取每张图像的segmentation信息
import os
import random

import numpy as np
from pycocotools.coco import COCO
from pycocotools import mask as coco_mask
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt

random.seed(0)

json_path = "D:/pywork/labelme2coco/data_dataset_coco/annotations.json"
img_path = "D:/pywork/labelme2coco/data_dataset_coco"

# random pallette
pallette = [0, 0, 0] + [random.randint(0, 255) for _ in range(255*3)]

# load coco data
coco = COCO(annotation_file=json_path)

# get all image index info
ids = list(sorted(coco.imgs.keys()))
print("number of images: {}".format(len(ids)))

# get all coco class labels
coco_classes = dict([(v["id"], v["name"]) for k, v in coco.cats.items()])

# 遍历前三张图像
for img_id in ids[:3]:
    # 获取对应图像id的所有annotations idx信息
    ann_ids = coco.getAnnIds(imgIds=img_id)
    # 根据annotations idx信息获取所有标注信息
    targets = coco.loadAnns(ann_ids)

    # get image file name
    path = coco.loadImgs(img_id)[0]['file_name']
    # read image
    img = Image.open(os.path.join(img_path, path)).convert('RGB')
    img_w, img_h = img.size

    masks = []
    cats = []
    for target in targets:
        cats.append(target["category_id"])  # get object class id
        polygons = target["segmentation"]   # get object polygons
        rles = coco_mask.frPyObjects(polygons, img_h, img_w)
        mask = coco_mask.decode(rles)
        if len(mask.shape) < 3:
            mask = mask[..., None]
        mask = mask.any(axis=2)
        masks.append(mask)

    cats = np.array(cats, dtype=np.int32)
    if masks:
        masks = np.stack(masks, axis=0)
    else:
        masks = np.zeros((0, height, width), dtype=np.uint8)

    # merge all instance masks into a single segmentation map
    # with its corresponding categories
    target = (masks * cats[:, None, None]).max(axis=0)
    # discard overlapping instances
    target[masks.sum(0) > 1] = 255
    target = Image.fromarray(target.astype(np.uint8))

    target.putpalette(pallette)
    plt.imshow(target)
    plt.show()

mask.frPyObjects方法

四、COCO数据集的介绍

(1)分类

  • 目标检测任务 Object Detection Task,可以细分为两大类:通过矩形框将待测目标框出来;把整个目标用一种颜色涂出来,包括它的轮廓都区分的比较明显(这种标注方式也称为实例分割)
  • Stuff 分割任务 Stuff Segmentation Task,与前面任务不同之处:前面任务处理的是有特定大小、形状的物体,后面这个任务关注的是草、山、沙子没有特定空间范围、形状的物体
  • 全景分割 Panoptic Segmentation Task,同时实现前面两种任务,既要检测出形状明显的人,又要检测出形状不明显的背景信息。
  • 看图说话 Captioning Challenge,
  • 人体关键点检测 Keypoint Detection Task
  • 人体姿态密度检测任务 DensePose Task

(2)特点


如何加载并处理COCO文件?COCO API是官方提供的一些处理标注文件的函数。在python中pycocotools。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号