1.安装conda虚拟环境时,按照官方说明一次就正常安装好了;
2.需要提前下载模型权重,运行时会自动下载resnet等模型的预训练权重,可以提前下载好后放进对应的隐藏文件夹内;
3.运行推理代码:
python demo/image_demo.py 'demo/demo.jpg' 'configs/detectors/detectors_cascade_rcnn_r50_1x_coco.py' 'checkpoints/detectors_cascade_rcnn_r50_1x_coco-32a10ba0.pth'
会报警告:
/home/lgy/PycharmProjects/mmdetection/mmdet/datasets/utils.py:68: UserWarning: "ImageToTensor" pipeline is replaced by "DefaultFormatBundle" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.
'data pipeline in your config file.', UserWarning)
官方对该警告的建议是不用搭理,个人按警告替换后,报了一些奇奇怪怪的错误。
或者直接新建个py文件运行:
from mmdet.apis import init_detector, inference_detector
import mmcv
# Specify the path to model config and checkpoint file
config_file = 'configs/detectors/detectors_cascade_rcnn_r50_1x_coco.py'
checkpoint_file = 'checkpoints/detectors_cascade_rcnn_r50_1x_coco-32a10ba0.pth'
# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# test a single image and show the results
img = 'demo/demo.jpg'
# img = mmcv.imread(img) # which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
# 可视化推理检测的结果
model.show_result(img, result)
# or save the visualization results to image files
# 将推理的结果保存
model.show_result(img, result, out_file='result.jpg')
# test a video and show the results
# 测试视频片段的推理结果
# video = mmcv.VideoReader('video.mp4')
# for frame in video:
# result = inference_detector(model, frame)
# model.show_result(frame, result, wait_time=1)