创建conda环境
conda create -n detectron2 python=3.8
conda activate detectron2
安装opencv
pip install opencv-python
pip install opencv-contrib-python
安装pytorch和cuda
pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
下载并编译detectron2
注意:
- 如果编译成功后,重新装了pytorch,请cd到detectron2目录下执行命令
rm -rf build/ **/*
删除编译的文件并重新编译
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2
运行demo
注意:请选择对应的模型和权重文件
- 模型在configs/COCO-InstanceSegmentation里寻找.yaml文件
- 权重文件在MODEL_ZOO.md里寻找
python demo/demo.py --config-file /home/wj/PythonGithub/detectron2-main/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input /home/wj/PythonGithub/detectron2-main/input/bus.jpg --opts MODEL.WEIGHTS /home/wj/PythonGithub/detectron2-main/demo/mask_rcnn_R_50_FPN_3x.pkl
注意:
- 在网络摄像头上运行,请把–input files 替换为–webcam
- 播放视频,请把–input files替换为–video-input video.mp4
- 要在cpu上运行,请在 -opts之后添加MODEL.DEVICE cpu-
- 要将输出保存到目录(用于图像)或文件(用于网络摄像头或视频),请使用–output
效果演示
图片检测
摄像头检测