mmdetection 预测目标框和类别颜色的修改

本文介绍如何修改mmdetection框架中imshow_det_bboxes函数,以实现按类别显示不同颜色的目标框,并在base.py文件调整字体大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

image.py 修改

在这里插入图片描述
主要修改imshow_det_bboxes函数,按照类别进行不同颜色的显示

def imshow_det_bboxes(img,
                      bboxes,
                      labels,
                      segms=None,
                      class_names=None,
                      score_thr=0,
                      bbox_color='green',
                    
                      text_color='green',
                      mask_color=None,
                      thickness=6,
                      font_size=36,
                      win_name='',
                      show=True,
                      wait_time=0,
                      out_file=None):
    """Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        segms (ndarray or None): Masks, shaped (n,h,w) or None
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.  Default: 0
        bbox_color (str or tuple(int) or :obj:`Color`):Color of bbox lines.
           The tuple of color should be in BGR order. Default: 'green'
        text_color (str or tuple(int) or :obj:`Color`):Color of texts.
           The tuple of color should be in BGR order. Default: 'green'
        mask_color (str or tuple(int) or :obj:`Color`, optional):
           Color of masks. The tuple of color should be in BGR order.
           Default: None
        thickness (int): Thickness of lines. Default: 2
        font_size (int): Font size of texts. Default: 13
        show (bool): Whether to show the image. Default: True
        win_name (str): The window name. Default: ''
        wait_time (float): Value of waitKey param. Default: 0.
        out_file (str, optional): The filename to write the image.
            Default: None

    Returns:
        ndarray: The image with bboxes drawn on it.
    """
    assert bboxes.ndim == 2, \
        f' bboxes ndim should be 2, but its ndim is {bboxes.ndim}.'
    assert labels.ndim == 1, \
        f' labels ndim should be 1, but its ndim is {labels.ndim}.'
    assert bboxes.shape[0] == labels.shape[0], \
        'bboxes.shape[0] and labels.shape[0] should have the same length.'
    assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5, \
        f' bboxes.shape[1] should be 4 or 5, but its {bboxes.shape[1]}.'
    img = mmcv.imread(img).astype(np.uint8)

    if score_thr > 0:
        assert bboxes.shape[1] == <
### 关于 MMDetection 预测的使用方法及相关问题 #### 1. 数据容器与预测存储 MMDetection 的核心功能之一是对图像进行目标检测并返回预测的结果。这些预测通常以列表的形式存储,其中每个元素对应一个类别下的多个边界及其置信度分数。为了适应不同批次的数据结构差异(如每张图片的目标数量不一致),MMDetection 引入了 `DataContainer` 模块[^1]。然而,PyTorch 并不能直接解析该模块对象,因此需要开发者手动处理数据转换逻辑。 #### 2. 基础配置与推理流程 在实际应用中,可以通过加载预训练模型来进行目标检测任务。以下是基本的代码实现方式: ```python from mmdet.apis import init_detector, inference_detector, show_result_pyplot config_file = 'configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py' checkpoint_file = 'mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth' model = init_detector(config_file, checkpoint_file, device='cuda:0') img_path = 'test.jpg' result = inference_detector(model, img_path) show_result_pyplot(model, img_path, result) ``` 此段代码展示了如何初始化模型、执行推断以及可视化结果[^2]。 #### 3. 多模态扩展与高级用法 对于更复杂的场景,例如点云三维检测或多模态融合任务,MMDetection 提供了专门的设计方案。例如,在构建三阶段检测器时,可以继承自 `Base3DDetector` `TwoStageDetector` 类型,从而充分利用已有组件的功能[^3]。 #### 4. 结果保存与展示优化 当希望将检测结果保存到本地磁盘而非仅限屏幕显示时,可通过调整函数调用来完成操作。例如,修改如下代码即可指定输出文件路径,并避免因无图形界面而导致错误发生: ```python import mmcv score_thr = 0.3 output_image = model.show_result( img, result, score_thr=score_thr, show=False, out_file="result/result2.jpg" ) mmcv.imwrite(output_image, "result/output_image.jpg") # 进一步确认保存成功 ``` 这里特别注意设置了参数 `out_file` 来定义最终生成图片的位置[^4]。 --- ###
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值