Skip to main content

Detectron2 开始

本文将引导快速使用 Detectron2 ,介绍用摄像头测试实时目标检测。

环境准备#

基础环境#

Detectron2#

安装,

# 创建 Python 虚拟环境
conda create -n detectron2 python=3.8 -y
conda activate detectron2
# 安装 PyTorch with CUDA
conda install pytorch==1.7.1 torchvision==0.8.2 cudatoolkit=10.2 -c pytorch -y
# 安装 Detectron2
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2
# 安装 OpenCV ,捕获相机图像及显示
pip install opencv-python

检查,

$ python - <<EOF
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())
import cv2 as cv
print(cv.__version__)
EOF
1.7.1 True
4.5.1

现有模型进行推断#

从其 model zoo 选择一个感兴趣的模型进行推断。这里以 COCO R50-FPN 3x 训练的各类模型进行演示。

下载 model 进如下路径,

detectron2/models/
├── COCO-Detection
│   └── faster_rcnn_R_50_FPN_3x
│   └── 137849458
│   ├── metrics.json
│   └── model_final_280758.pkl
├── COCO-InstanceSegmentation
│   └── mask_rcnn_R_50_FPN_3x
│   └── 137849600
│   ├── metrics.json
│   └── model_final_f10217.pkl
├── COCO-Keypoints
│   └── keypoint_rcnn_R_50_FPN_3x
│   └── 137849621
│   ├── metrics.json
│   └── model_final_a6e10b.pkl
└── COCO-PanopticSegmentation
└── panoptic_fpn_R_50_3x
└── 139514569
├── metrics.json
└── model_final_c10459.pkl

目标检测 - Faster R-CNN#

执行,

cd detectron2/
mkdir -p _output
python demo/demo.py \
--config-file configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml \
--input ../data/bicycle.jpg \
--output _output/bicycle_COCO-Detection.jpg \
--confidence-threshold 0.5 \
--opts MODEL.WEIGHTS models/COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pkl

结果,

实例分割 - Mask R-CNN#

执行,

python demo/demo.py \
--config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
--input ../data/bicycle.jpg \
--output _output/bicycle_COCO-InstanceSegmentation.jpg \
--confidence-threshold 0.5 \
--opts MODEL.WEIGHTS models/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

结果,

姿态估计 - Keypoint R-CNN#

执行,

python demo/demo.py \
--config-file configs/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml \
--input ../data/bicycle.jpg \
--output _output/bicycle_COCO-Keypoints.jpg \
--confidence-threshold 0.5 \
--opts MODEL.WEIGHTS models/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x/137849621/model_final_a6e10b.pkl

结果,

全景分割 - Panoptic FPN#

执行,

python demo/demo.py \
--config-file configs/COCO-PanopticSegmentation/panoptic_fpn_R_50_3x.yaml \
--input ../data/bicycle.jpg \
--output _output/bicycle_COCO-PanopticSegmentation.jpg \
--confidence-threshold 0.5 \
--opts MODEL.WEIGHTS models/COCO-PanopticSegmentation/panoptic_fpn_R_50_3x/139514569/model_final_c10459.pkl

结果,

WebCam 摄像头使用#

获取本机的 WebCam 列表,

$ ls /dev/video*
/dev/video0 /dev/video1 /dev/video2 /dev/video3
# 查看 WebCam 列表
# 如下:有 0, 2 两个 videos
# - 第一个是 video ,第二个是 metadata
# - 从 Linux Kernel 4.16 开始,增加的 metadata node
$ sudo apt install v4l-utils
$ v4l2-ctl --list-devices
HD Webcam: HD Webcam (usb-0000:00:14.0-13):
/dev/video0
/dev/video1
HD Pro Webcam C920 (usb-0000:00:14.0-4):
/dev/video2
/dev/video3
# 查看某 WebCam 支持的格式、分辨率、fps 信息
$ v4l2-ctl -d 2 --list-formats-ext

demo/demo.py 可修改期望打开的摄像头及其分辨率等,

elif args.webcam:
cam = cv2.VideoCapture(2)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cam.set(cv2.CAP_PROP_FPS, 30)
print(f"wencam: {cam.get(cv2.CAP_PROP_FRAME_WIDTH)}x{cam.get(cv2.CAP_PROP_FRAME_HEIGHT)} {cam.get(cv2.CAP_PROP_FPS)}")

运行,

python demo/demo.py \
--config-file configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml \
--webcam \
--confidence-threshold 0.5 \
--opts MODEL.WEIGHTS models/COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pkl

效果,