mmdetection2测试单张图片并保存
from argparse import ArgumentParserimport osfrom mmdet.apis import inference_detector, init_detector#, show_result_pyplotimport cv2def show_result_pyplot(model, img, result, score_thr=0.3, fig_size=(1
·
from argparse import ArgumentParser
import os
from mmdet.apis import inference_detector, init_detector #, show_result_pyplot
import cv2
def show_result_pyplot(model, img, result, score_thr=0.3, fig_size=(15, 10)):
"""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.
fig_size (tuple): Figure size of the pyplot figure.
"""
if hasattr(model, 'module'):
model = model.module
img = model.show_result(img, result, score_thr=score_thr, show=False)
return img
def main():
# config文件
config_file = '/root/mmdetection/work_dirs/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco.py'
# 训练好的模型
checkpoint_file = '/root/mmdetection/work_dirs/faster_rcnn_r50_fpn_1x_coco/latest.pth'
# model = init_detector(config_file, checkpoint_file)
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# 图片路径
name= '/root/mmdetection/data/coco/val2017/000088.jpg'
# 检测后存放图片路径
out_dir = '/root/output/'
if not os.path.exists(out_dir):
os.mkdir(out_dir)
result = inference_detector(model, name)
img = show_result_pyplot(model, name, result, score_thr=0.8)
#命名输出图片名称
cv2.imwrite("{}/{}.jpg".format(out_dir, 122), img)
if __name__ == '__main__':
main()
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)