这个是用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,界面截图如下所示: