文章背景:

MMdection框架安装好之后,需要测试一下环境以及相关的检测功能,通过下载相应的模型和加载配置文件以及框架自带的demo,可以很方便的完成目标检测,同时,该代码也便于后期批量推论测试改写。

测试代码如下:

# -*- coding=utf-8 -*-
'''
功能:针对安装好的MMCV环境及MMdection框架进行检测图片测试
'''

from mmdet.apis import init_detector, inference_detector

config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
# 从 model zoo 下载 checkpoint 并放在 `checkpoints/` 文件下
# 网址为: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cuda:0'
# 初始化检测器
model = init_detector(config_file, checkpoint_file, device=device)
# 推理演示图像
img = 'demo/demo.jpg'
result = inference_detector(model, img)
model.show_result(img, result)
# 将推理的结果保存
model.show_result(img, result, out_file='result.jpg')

测试结果如下:

说明环境安装OK,并且可以使用demo了。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐