文章目录[隐藏]
mmdetection中使用训练好的模型单张图片推理并保存到文件夹
one_image_demo.py
# Copyright (c) OpenMMLab. All rights reserved.
import asyncio
import numpy as np
from argparse import ArgumentParser
from mmdet.apis import (async_inference_detector, inference_detector,
init_detector, show_result_pyplot)
import warnings
warnings.filterwarnings("ignore")
def parse_args():
parser = ArgumentParser()
parser.add_argument('--img', default='demo.jpg',help='Image file')
parser.add_argument('--config',default='../configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py', help='Config file')
parser.add_argument('--checkpoint',default='../checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth', help='Checkpoint file')
parser.add_argument(
'--device', default='cpu', help='Device used for inference') #cuda:0
parser.add_argument(
'--score-thr', type=float, default=0.9, 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):
model = init_detector(args.config, args.checkpoint, device=args.device)
result = inference_detector(model, args.img)
#bboxes_scores = np.vstack(result)
#bboxes=bboxes_scores[:,:4]
#score=bboxes_scores[:,4]
#labels = [
# np.full(bbox.shape[0], i, dtype=np.int32)
# for i, bbox in enumerate(result)
# ]
#labels = np.concatenate(labels)
#print(bboxes_scores)
#print(labels)
#print(result)
model.show_result(args.img,result, score_thr=args.score_thr,out_file='demo_test.jpg')
# show the results
#show_result_pyplot(model, args.img, result, score_thr=args.score_thr)
if __name__ == '__main__':
args = parse_args()
if args.async_test:
asyncio.run(async_main(args))
else:
main(args)
版权声明:本文为CSDN博主「平凡中寻找不平凡」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43804210/article/details/122110359
暂无评论