激活记录2中创建的pytorch虚拟环境,然后输入
pip install opencv-python
显示成功安装后,输入python import cv2
无报错
然后根据detectron2官方介绍安装detectron2
https://detectron2.readthedocs.io/en/latest/tutorials/install.html
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2
安装成功后,打开detectron2配套的ModelZoo,选取其中的一个预训练模型进行测试。
我选择了Cityscapes的一个Baseline,
点击右边的model下载到本地
然后参考https://detectron2.readthedocs.io/en/latest/tutorials/getting_started.html的教程,
单张输入图片为CityScapes中的一张(此处注意ModelZoo中的不同模型是用于检测不同类型的图片的,在最开始我随便选了一个coco模型来测cityscapes中的图片,结果并不是很好,不能精准检测到目标)
python /HOMES/user/detectron2/demo/demo.py
#此处为detectron2 所提供的demo.py ,用于demo内置的configs.
--config-file /HOMES/user/detectron2/configs/Cityscapes/mask_rcnn_R_50_FPN.yaml
#此处根据所选择的ModelZoo中的模型来在configs中选择并写入绝对路径
--input /HOMES/user/CityScapes/berlin/berlin_000000_000008_leftImg8bit.png
#输入需要检测的图片
--output /HOMES/user/detectron2/output
#指定检测后图片的输出路径
--opts MODEL.DEVICE cpu MODEL.WEIGHTS /HOMES/user/Downloads/model_final_af9cf5.pkl
#由于前端没有安装GPU,选择在cpu上运行,在MODEL.WEIGHTS前加MODEL.DEVICE cpu,后边跟上下载的Model的绝对路径
运行完后输出图片如下:
成功!
-
调用摄像头, 替换
--input files
为--webcam
. -
检测视频, 替换
--input files
为--video-input video.mp4
. -
在cpu上运行, 添加
MODEL.DEVICE cpu
在--opts后
. -
多张图片检测,输入图片的文件夹路径后加/*.jpg