mmdetection测试保存到新的文件夹,无需标签

这个是用demo这个代码测试的,需要先训练一个pth文件夹,训练之后再调用pth文件夹进行测试。测试的代码文件名是:image_demo_new.py,代码如系所示:

# Copyright (c) OpenMMLab. All rights reserved.
import asyncio
from argparse import ArgumentParser

from mmdet.apis import (async_inference_detector, inference_detector,
                        init_detector, show_result_pyplot)
import cv2
import os

def parse_args():
    parser = ArgumentParser()
    parser.add_argument('img', default=None,help='Image file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument('--out-file', default=None, help='Path to output file')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--palette',
        default='coco',
        choices=['coco', 'voc', 'citys', 'random'],
        help='Color palette used for visualization')
    parser.add_argument(
        '--score-thr', type=float, default=0.3, help='bbox score threshold')
    parser.add_argument(
        '--async-test',
        action='store_true',
        help='whether to set async options for async inference.')
    args = parser.parse_args()
    return args


def main(args):
    outdir='./demo_out1'
    imgdir='./image/fire'
    for imgf in os.listdir(imgdir):
        if not imgf.endswith('jpg'):
            continue
        # build the model from a config file and a checkpoint file
        model = init_detector(args.config, args.checkpoint, device=args.device)
        # test a single image
        result = inference_detector(model, imgdir + '/'+ imgf)
        # import pdb;pdb.set_trace()
        if hasattr(model, 'module'):
            model = model.module
        img = model.show_result(imgdir + '/'+ imgf,result,score_thr=args.score_thr,show=False)
        cv2.imwrite('{}/{}'.format(outdir,imgf),img)

if __name__ == '__main__':
    args = parse_args()
    main(args)

记得自己新建“demo_out1”这个文件夹
测试完成之后会保存到一个新的文件夹下面,文件名是:demo_out1,测试代码文件夹名称是: imgdir='./image/fire',运行的代码如下所示:python demo/image_demo_new.py image/fire/image_733.jpg configs/ddod_log_ddod_r50_fpn_1x_coco/ddod_r50_fpn_1x_coco.py configs/ddod_log_ddod_r50_fpn_1x_coco/epoch_100.pth --device cpu

测试我是直接用cpu的,没用GPU,界面截图如下所示:
在这里插入图片描述

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值