深度学习目标检测入门

常见数据集类型:

COCO数据集:

Pytorch加载COCO数据集:

COCO数据集的读取

COCO_dataset = torchvision.datasets.CocoDetection(
    root="./dataset/val2017",
    annFile="./instances_val2017/instances_val2017.json"
)
  • root (str or pathlib.Path) -- Root directory where images are downloaded to.

  • annFile (string) -- Path to json annotation file.

root 参数为图像所在的目录 annFile为标注文件所在的路径

val2017:包含 COCO数据集2017 验证集 内容为图片 1G

instances_val2017.json 该json文件中包括:

图像信息images:

  • id: 图像的唯一标识符
  • file_name: 图像文件名
  • width 和 height: 图像尺寸
  • date_captured: 图像捕获日期
  • license: 图像许可证信息

标注信息annotations

  • id: 注释的唯一标识符
  • image_id: 对应图像的id
  • category_id: 对象类别的id
  • segmentation: 对象的分割掩码(可能是多边形或RLE格式):

counts:

如果"counts"为 [3,2,1,4],则表示:3个背景像素2个前景像素1个背景像素4个前景像素

  • area: 分割区域的面积
  • bbox: 边界框坐标 [x, y, width, height]
  • iscrowd: 标记是否为群体对象

将COCO数据集中第六个元素为例 :

利用Image和info接收

image,info = COCO_dataset[5]

原因:解包内容

(<PIL.Image.Image image mode=RGB size=640x425 at 0x264B630EB10>, [{'segmentation': [[353.37, 67.65, 358.15, 52.37, 362.92, 47.59, 374.38, 44.73, 389.66, 52.37, 389.66, 67.65, 389.66, 76.25, 393.48, 83.89, 396.35, 88.66, 397.3, 91.53, 406.85, 99.17, 413.54, 104.9, 451.74, 148.83, 458.43, 153.6, 462.25, 166.02, 467.02, 173.66, 463.2, 181.3, 449.83, 183.21, 448.88, 191.81, 455.56, 226.19, 448.88, 254.84, 453.65, 286.36, 475.62, 323.6, 491.85, 361.81, 494.72, 382.82, 494.72, 382.82, 499.49, 391.41, 416.4, 391.41, 424.04, 383.77, 439.33, 374.22, 445.06, 360.85, 436.46, 334.11, 421.18, 303.55, 416.4, 289.22, 409.72, 268.21, 396.35, 280.63, 405.9, 298.77, 417.36, 324.56, 425.0, 349.39, 425.0, 357.99, 419.27, 360.85, 394.44, 367.54, 362.92, 370.4, 346.69, 367.54, 360.06, 362.76, 369.61, 360.85, 382.98, 340.8, 355.28, 271.08, 360.06, 266.3, 386.8, 219.5, 368.65, 162.2, 348.6, 175.57, 309.44, 187.03, 301.8, 192.76, 288.43, 193.72, 282.7, 193.72, 280.79, 187.03, 280.79, 174.62, 287.47, 171.75, 291.29, 171.75, 295.11, 171.75, 306.57, 166.98, 312.3, 165.07, 345.73, 142.14, 350.51, 117.31, 350.51, 102.03, 350.51, 90.57, 353.37, 65.74]], 'area': 27789.110550000005, 'iscrowd': 0, 'image_id': 785, 'bbox': [280.79, 44.73, 218.7, 346.68], 'category_id': 1, 'id': 442619}, {'segmentation': [[613.15, 389.9, 368.65, 400.41, 275.06, 400.41, 264.55, 388.95, 267.42, 384.17, 275.06, 388.95, 290.34, 395.63, 409.72, 393.72, 504.27, 386.08, 615.06, 385.13], [446.97, 369.85, 217.75, 382.26, 205.34, 370.8, 213.93, 367.94, 232.08, 375.58, 456.52, 362.21]], 'area': 3871.073400000008, 'iscrowd': 0, 'image_id': 785, 'bbox': [205.34, 362.21, 409.72, 38.2], 'category_id': 35, 'id': 609541}])

image_handler = ImageDraw.ImageDraw(image)

代码 image_handler = ImageDraw.ImageDraw(image) 是在使用 Python Imaging Library (PIL) 中的 ImageDraw 模块创建一个绘图对象。这个操作的目的和用途如下:

  1. 创建绘图对象:

    ImageDraw.Draw() 方法创建一个可以在给定图像上进行绘制操作的对象。

  2. 图像编辑:

    这个对象允许你在现有图像上进行各种绘图操作,而不需要创建新的图像。

本代码中用于在目标检测或图像分割任务中标注边界框或区域

标注原始图像:

循环遍历:

遍历 info 中的每个元素,赋值给annotation(标注)

COCO数据集中'bbox':

 'bbox': [205.34, 362.21, 409.72, 38.2]

含义x_min,y_min,width,height

而VOC 数据集中'bbox':

含义为 左上x,左上y,右下x,右下y坐标

接收x_min,y_min,width,height参数:

在Info中遍历得到的信息 赋值给了 annotation

此时annotation内部为一个字典包括:area,bbox等参数

x_min,y_min,width,height  = annotation['bbox']

标注处理:

image_handler.rectangle((x_min,y_min,x_min+width,y_min+height),outline='red')

image_handler.rectangle([(x_min,y_min),(x_min+width,y_min+height)],outline="blue")
  1. xy(必需):

    这是定义矩形位置和大小的主要参数。它可以是以下两种格式之一:

    a) 四元组 (x0, y0, x1, y1)

    • (x0, y0) 是左上角坐标
    • (x1, y1) 是右下角坐标
      例如:(100, 100, 200, 200)

    b) 包含两个坐标点的列表或元组 [(x0, y0), (x1, y1)]

    • (x0, y0) 是左上角坐标
    • (x1, y1) 是右下角坐标
      例如:[(100, 100), (200, 200)]
  2. fill(可选):

    • 矩形的填充颜色
    • 可以是颜色名称字符串(如 "red")或 RGB/RGBA 元组(如 (255, 0, 0) 或 (255, 0, 0, 255))
    • 默认为 None(不填充)
  3. outline(可选):

    • 矩形轮廓的颜色
    • 格式同 fill
    • 默认为 None(无轮廓)
  4. width(可选):

    • 轮廓线的宽度(像素)
    • 默认为 1

完整代码:

import torchvision
from PIL import ImageDraw

COCO_dataset = torchvision.datasets.CocoDetection(
    root="./dataset/val2017",
    annFile="./instances_val2017/instances_val2017.json"
)
print(COCO_dataset[5])
image,info = COCO_dataset[5]

image_handler = ImageDraw.ImageDraw(image)####对原有图像进行标注画框处理
for annotation in info:
    x_min,y_min,width,height  = annotation['bbox']
    # image_handler.rectangle((x_min,y_min,x_min+width,y_min+height),outline='red')
    image_handler.rectangle([(x_min,y_min),(x_min+width,y_min+height)],outline="blue")
    # image_handler.rectangle(((x_min,y_min),(x_min+width,y_min+height)))
image.show()

实验结果 :

相关推荐
新加坡内哥谈技术6 分钟前
口哨声、歌声、boing声和biotwang声:用AI识别鲸鱼叫声
人工智能·自然语言处理
wx74085132617 分钟前
小琳AI课堂:机器学习
人工智能·机器学习
FL162386312925 分钟前
[数据集][目标检测]车油口挡板开关闭合检测数据集VOC+YOLO格式138张2类别
人工智能·yolo·目标检测
YesPMP平台官方27 分钟前
AI+教育|拥抱AI智能科技,让课堂更生动高效
人工智能·科技·ai·数据分析·软件开发·教育
FL16238631291 小时前
AI健身体能测试之基于paddlehub实现引体向上计数个数统计
人工智能
黑客-雨1 小时前
构建你的AI职业生涯:从基础知识到专业实践的路线图
人工智能·产品经理·ai大模型·ai产品经理·大模型学习·大模型入门·大模型教程
子午1 小时前
动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
人工智能·python·cnn
大耳朵爱学习1 小时前
掌握Transformer之注意力为什么有效
人工智能·深度学习·自然语言处理·大模型·llm·transformer·大语言模型
TAICHIFEI1 小时前
目标检测-数据集
人工智能·目标检测·目标跟踪
qq_15321452641 小时前
【2023工业异常检测文献】SimpleNet
图像处理·人工智能·深度学习·神经网络·机器学习·计算机视觉·视觉检测