mmdetection测试单张/多张图片并保存(继大佬分享改错)

目录

----------------------------说明--------------------------------

--------------------------已解决-----------------------------


----------------------------说明--------------------------------

为了避免原链接失效,以下将在原文章复制几句做一个简单介绍。感谢大佬分享!

从mmdetectionv1.0rc1版本及以后的版本中,源代码有了一定的更新,而官方文档上用于测试单张图片的方法还并未更新。在mmdetectionv1.0rc1及以后的版本中,做了以下变更。因此,为测试图片,需要做对应的更改。

1、show_result()函数被封装进了show_result_pyplot()函数中。

2、在show_result_pyplot()函数中,并未留出保存测试好图片的接口。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。原文链接:https://blog.csdn.net/weixin_41922853/article/details/118807709

报错1:show_result_pyplot(model, img, result, out_file, score_thr=0.6) TypeError: show_result_pyplot() got multiple values for argument 'score_thr':

报错2:if score_thr > 0: TypeError: '>' not supported between instances of 'str' and 'int'

--------------------------已解决-----------------------------

1、新建test.py,代码如下。

不同的是:show_result_pyplot(model, img, result, out_file)  #这里删去原博主的score_thr=0.6

from mmdet.apis import init_detector, inference_detector
from mmdet.apis import show_result_pyplot
import os

imagepath = r'D:\images'  # 需要加载的测试图片的文件路径
savepath = r'D:\test_img'  # 保存测试图片的路径
config_file = r'D:\documents\config.py'  # 网络模型
checkpoint_file = r'D:\documents\epoch_55.pth'  # 训练好的模型参数
device = 'cuda:0'
# init a detector
model = init_detector(config_file, checkpoint_file, device=device)
# inference the demo image

for filename in os.listdir(imagepath):
  img = os.path.join(imagepath, filename)
  result = inference_detector(model, img)
  out_file = os.path.join(savepath, filename)
  show_result_pyplot(model, img, result, out_file)  #-------这里删去原博主的score_thr=0.6---

2、跟着代码中注释修改mmdet\apis\inference.py文件中的以下函数:

# mmdet\apis\inference.py

 
def show_result_pyplot(model,
                       img,
                       result,
                       # score_thr=0.3,#-----这里直接删了--------
                       out_file,       #-------加这一行---------
                       title='result',
                       wait_time=0,
                       palette=None):
                       # out_file=None):


    if hasattr(model, 'module'):
        model = model.module
    model.show_result(
        img,
        result,
        score_thr=0.6,     #-----这里直接改为0.6--------
        show=True,
        out_file=out_file, #-------加这一行---------
        wait_time=wait_time,
        win_name=title,
        bbox_color=palette,
        text_color=(200, 200, 200),
        mask_color=palette)

如果不想保存只想看看show一下,不需保存,那就设置show_result_pyplot中的out_file=None

3、运行即可test.py即可。成功了,结果如下。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
mmdetection框架中,可以通过以下方法将测试结果保存下来: 1. 首先,创建一个test.py文件,并添加以下代码: ``` from mmdet.apis import init_detector, inference_detector, show_result_pyplot import os imagepath = r'D:\software\opensources\mmdet214\data\new_cell_cocoformat\valset' # 需要加载的测试图片的文件路径 savepath = r'D:\software\opensources\mmdet214\configs\retinanet\cell\test_show' # 保存测试图片的路径 config_file = r'D:\software\opensources\mmdet214\configs\cell\retinanet_r101_fpn_1x_cell.py' # 网络模型 checkpoint_file = r'D:\software\opensources\mmdet214\work_dirs\retinanet_r101_fpn_1x_cell_1300_472\epoch_100.pth' # 训练好的模型参数 device = 'cuda:0' # 初始化检测器模型 model = init_detector(config_file, checkpoint_file, device=device) # 对每张测试图片进行推理和结果保存 for filename in os.listdir(imagepath): img = os.path.join(imagepath, filename) result = inference_detector(model, img) out_file = os.path.join(savepath, filename) show_result_pyplot(model, img, result, out_file, score_thr=0.6) ``` 2. 修改mmdet\apis\inference.py文件中的show_result_pyplot函数,添加一个out_file参数,并将其传递给model.show_result函数。具体修改内容如下: ``` def show_result_pyplot(model, img, result, out_file, score_thr=0.3, title='result', wait_time=0): """ Visualize the detection results on the image. Args: model (nn.Module): The loaded detector. img (str or np.ndarray): Image filename or loaded image. result (tuple[list or list): The detection result, can be either (bbox, segm) or just bbox. score_thr (float): The threshold to visualize the bboxes and masks. title (str): Title of the pyplot figure. wait_time (float): Value of waitKey param. Default: 0. out_file (str): Path to save the visualization result. """ if hasattr(model, 'module'): model = model.module model.show_result(img, result, out_file, score_thr=score_thr, show=True, wait_time=wait_time, win_name=title, bbox_color=(72, 101, 241), text_color=(72, 101, 241)) ``` 通过以上两个步骤,你可以在mmdetection框架中测试图片保存结果。每个测试图片的结果将保存在指定的路径下。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [(最新)mmdetection测试单张/多张图片保存](https://blog.csdn.net/weixin_41922853/article/details/118807709)[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_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值