目标检测 - 绘制bounding box

这篇具有很好参考价值的文章主要介绍了目标检测 - 绘制bounding box。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

工具类

from PIL.Image import Image, fromarray
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
from PIL import ImageColor
import numpy as np

STANDARD_COLORS = [
    'AliceBlue', 'Chartreuse', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque',
    'BlanchedAlmond', 'BlueViolet', 'BurlyWood', 'CadetBlue', 'AntiqueWhite',
    'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan',
    'DarkCyan', 'DarkGoldenRod', 'DarkGrey', 'DarkKhaki', 'DarkOrange',
    'DarkOrchid', 'DarkSalmon', 'DarkSeaGreen', 'DarkTurquoise', 'DarkViolet',
    'DeepPink', 'DeepSkyBlue', 'DodgerBlue', 'FireBrick', 'FloralWhite',
    'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod',
    'Salmon', 'Tan', 'HoneyDew', 'HotPink', 'IndianRed', 'Ivory', 'Khaki',
    'Lavender', 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue',
    'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey',
    'LightGreen', 'LightPink', 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue',
    'LightSlateGray', 'LightSlateGrey', 'LightSteelBlue', 'LightYellow', 'Lime',
    'LimeGreen', 'Linen', 'Magenta', 'MediumAquaMarine', 'MediumOrchid',
    'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue', 'MediumSpringGreen',
    'MediumTurquoise', 'MediumVioletRed', 'MintCream', 'MistyRose', 'Moccasin',
    'NavajoWhite', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed',
    'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed',
    'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple',
    'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Green', 'SandyBrown',
    'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
    'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'GreenYellow',
    'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White',
    'WhiteSmoke', 'Yellow', 'YellowGreen'
]

"""
将目标边界框和类别信息绘制到图片上
"""
def draw_text(draw,
              box: list,
              cls: int,
              score: float,
              category_index: dict,
              color: str,
              font: str = 'arial.ttf',
              font_size: int = 24):

    try:
        font = ImageFont.truetype(font, font_size)
    except IOError:
        font = ImageFont.load_default()

    left, top, right, bottom = box
    display_str = f"{category_index[str(cls)]}: {int(100 * score)}%"
    display_str_heights = [font.getsize(ds)[1] for ds in display_str]
    display_str_height = (1 + 2 * 0.05) * max(display_str_heights)

    if top > display_str_height:
        text_top = top - display_str_height
        text_bottom = top
    else:
        text_top = bottom
        text_bottom = bottom + display_str_height

    for ds in display_str:
        text_width, text_height = font.getsize(ds)
        margin = np.ceil(0.05 * text_width)
        draw.rectangle([(left, text_top),
                        (left + text_width + 2 * margin, text_bottom)], fill=color)
        draw.text((left + margin, text_top),
                  ds,
                  fill='black',
                  font=font)
        left += text_width


def draw_masks(image, masks, colors, thresh: float = 0.7, alpha: float = 0.5):
    np_image = np.array(image)
    masks = np.where(masks > thresh, True, False)

    # colors = np.array(colors)
    img_to_draw = np.copy(np_image)
    # TODO: There might be a way to vectorize this
    for mask, color in zip(masks, colors):
        img_to_draw[mask] = color

    out = np_image * (1 - alpha) + img_to_draw * alpha
    return fromarray(out.astype(np.uint8))

"""
调用该函数进行绘制,传入图像,边界框信息,类别信息,置信度,类别索引,字体等信息。
将目标边界框信息,类别信息,mask信息绘制在图片上
Args:
    image: 需要绘制的图片
    boxes: 目标边界框信息
    classes: 目标类别信息
    scores: 目标概率信息
    masks: 目标mask信息
    category_index: 类别与名称字典
    box_thresh: 过滤的概率阈值
    mask_thresh:
    line_thickness: 边界框宽度
    font: 字体类型
    font_size: 字体大小
    draw_boxes_on_image:
    draw_masks_on_image:

Returns:
"""
def draw_objs(image: Image,
              boxes: np.ndarray = None,
              classes: np.ndarray = None,
              scores: np.ndarray = None,
              masks: np.ndarray = None,
              category_index: dict = None,
              box_thresh: float = 0.1,
              mask_thresh: float = 0.5,
              line_thickness: int = 8,
              font: str = 'arial.ttf',
              font_size: int = 24,
              draw_boxes_on_image: bool = True,
              draw_masks_on_image: bool = False):


    # 过滤掉低概率的目标
    idxs = np.greater(scores, box_thresh)
    boxes = boxes[idxs]
    classes = classes[idxs]
    scores = scores[idxs]
    if masks is not None:
        masks = masks[idxs]
    if len(boxes) == 0:
        return image

    colors = [ImageColor.getrgb(STANDARD_COLORS[cls % len(STANDARD_COLORS)]) for cls in classes]

    if draw_boxes_on_image:
        draw = ImageDraw.Draw(image)
        for box, cls, score, color in zip(boxes, classes, scores, colors):
            left, top, right, bottom = box
            # 绘制目标边界框
            draw.line([(left, top), (left, bottom), (right, bottom),
                       (right, top), (left, top)], width=line_thickness, fill=color)
            # 绘制类别和概率信息
            draw_text(draw, box.tolist(), int(cls), float(score), category_index, color, font, font_size)

    if draw_masks_on_image and (masks is not None):
        image = draw_masks(image, masks, colors, mask_thresh)

    return image

目标检测 - 绘制bounding box,AI,目标检测,人工智能,计算机视觉文章来源地址https://www.toymoban.com/news/detail-819491.html

到了这里,关于目标检测 - 绘制bounding box的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 人工智能TensorFlow PyTorch物体分类和目标检测合集【持续更新】

    1. 基于TensorFlow2.3.0的花卉识别 基于TensorFlow2.3.0的花卉识别Android APP设计_基于安卓的花卉识别_lilihewo的博客-CSDN博客 2. 基于TensorFlow2.3.0的垃圾分类 基于TensorFlow2.3.0的垃圾分类Android APP设计_def model_load(img_shape=(224, 224, 3)_lilihewo的博客-CSDN博客   3. 基于TensorFlow2.3.0的果蔬识别系统的

    2024年02月09日
    浏览(37)
  • 人工智能学习与实训笔记(三):神经网络之目标检测问题

    人工智能专栏文章汇总:人工智能学习专栏文章汇总-CSDN博客 目录 三、目标检测问题 3.1 目标检测基础概念 3.1.1 边界框(bounding box) 3.1.2 锚框(Anchor box) 3.1.3 交并比 3.2 单阶段目标检测模型YOLOv3 3.2.1 YOLOv3模型设计思想 3.2.2 YOLOv3模型训练过程 3.2.3 如何建立输出特征图与预

    2024年02月20日
    浏览(36)
  • YOLO目标检测——真实和人工智能生成的合成图像数据集下载分享

    YOLO真实和人工智能生成的合成图像数据集,真实场景的高质量图片数据,图片格式为jpg,数据场景丰富。可用于检测图像是真实的还是由人工智能生成。 数据集点击下载 :YOLO真实和人工智能生成的合成图像数据集+120000图片+数据说明.rar

    2024年02月10日
    浏览(34)
  • 人工智能学习07--pytorch15(前接pytorch10)--目标检测:FPN结构详解

    backbone:骨干网络,例如cnn的一系列。(特征提取) (a)特征图像金字塔 检测不同尺寸目标。 首先将图片缩放到不同尺度,针对每个尺度图片都一次通过算法进行预测。 但是这样一来,生成多少个尺度就要预测多少次,训练效率很低。 (b)单一特征图 faster rcnn所采用的一种方式

    2023年04月12日
    浏览(36)
  • 基于人工智能与边缘计算Aidlux的鸟类检测驱赶系统(可修改为coco 80类目标检测)

    ●项目名称 基于人工智能与边缘计算Aidlux的鸟类检测驱赶系统(可修改为coco 80类目标检测) ●项目简介 本项目在Aidlux上部署鸟类检测驱赶系统,通过视觉技术检测到有鸟类时,会进行提示。并可在源码上修改coco 80类目标检测索引直接检测其他79类目标,可以直接修改、快速

    2024年02月12日
    浏览(43)
  • 毕业设计:基于机器学习的课堂学生表情识别系统 人工智能 python 目标检测

    目录 前言 项目背景 数据集 设计思路 更多帮助     📅大四是整个大学期间最忙碌的时光,一边要忙着备考或实习为毕业后面临的就业升学做准备,一边要为毕业设计耗费大量精力。近几年各个学校要求的毕设项目越来越难,有不少课题是研究生级别难度的,对本科同学来说是充

    2024年04月16日
    浏览(48)
  • 人工智能学习07--pytorch20--目标检测:COCO数据集介绍+pycocotools简单使用

    如:天空 coco包含pascal voc 的所有类别,并且对每个类别的标注目标个数也比pascal voc的多。 一般使用coco数据集预训练好的权重来迁移学习。 如果仅仅针对目标检测object80类而言,有些图片并没有标注信息,或者有错误标注信息。所以在实际的训练过程中,需要对这些数据进行

    2024年02月12日
    浏览(37)
  • 毕业设计选题- 基于深度学习的海洋生物目标检测系统 YOLO 人工智能

    目录 前言 课题背景和意义 实现技术思路 一、基于深度学习的海洋生物目标检测研究主题 二、水下图像处理算法的研究 2.1Retinex算法 2.2直方图均衡化算法 2.3暗通道去雾算法 三、基于深度学习的目标检测算法 海洋生物目标检测实现效果 最后        📅大四是整个大学期间最

    2024年02月01日
    浏览(55)
  • 人工智能学习07--pytorch23--目标检测:Deformable-DETR训练自己的数据集

    1、pytorch conda create -n deformable_detr python=3.9 pip 2、激活环境 conda activate deformable_detr 3、torch 4、其他的库 pip install -r requirements.txt 5、编译CUDA cd ./models/ops sh ./make.sh #unit test (should see all checking is True) python test.py (我没运行这一步) 主要是MultiScaleDeformableAttention包,如果中途换了

    2024年02月14日
    浏览(36)
  • 毕业设计:基于机器学习的草莓成熟度识别分类系统 人工智能 python 目标检测

    目录 前言 项目背景 数据集 设计思路 更多帮助     📅大四是整个大学期间最忙碌的时光,一边要忙着备考或实习为毕业后面临的就业升学做准备,一边要为毕业设计耗费大量精力。近几年各个学校要求的毕设项目越来越难,有不少课题是研究生级别难度的,对本科同学来说是充

    2024年04月27日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包