This article is a quick guide to Detectron2 and shows you how to test real-time object detection with a camera.
- Detectron2: github.com/facebookres…
Environment to prepare
Based on the environment
- Ubuntu 18.04
- System installation, visible production USB boot disk, and system installation
- Nvidia Driver
- Driver installation, visible Ubuntu initial configuration – Nvidia driver
- Anaconda Python
- Download the Linux version here and install it
Detectron2
The installation,
Create a Python virtual environmentConda create -n Detectron2 Python =3.8 -y conda activate detectron2Install PyTorch with CUDAConda install Pytorch ==1.7.1 TorchVision ==0.8.2 CudatoolKit =10.2 -C Pytorch -y# installation Detectron2
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2
# Install OpenCV, capture camera image and display
pip install opencv-python
Copy the code
Check,
$ 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
Copy the code
- Installation documentation – official
Existing models are extrapolated
Select an interesting model from its model zoo for inference. Here to COCO R50-FPN 3X training of various models for demonstration.
Download model to the following path:
├─ ├─ untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt Model_final_7482.489. ├── 7482.489 │ ├── trimester. Json │ ├─ 4482.489 ├─ ├─ untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt, untr.txt Model_final_a6e10b. PKL └ ─ ─ COCO - PanopticSegmentation └ ─ ─ panoptic_fpn_R_50_3x └ ─ ─ 139514569 ├ ─ ─ metrics. The json └ ─ ─ model_final_c10459.pklCopy the code
Target detection – Faster R-CNN
To perform,
cddetectron2/ 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 \ -- confident-threshold 0.5 \ --opts model.weights models/COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pklCopy the code
As a result,
Example segmentation -mask r-CNN
To perform,
python demo/demo.py \ --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \ --input .. 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.pklCopy the code
As a result,
Attitude estimation — Keypoint R — CNN
To perform,
python demo/demo.py \ --config-file configs/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml \ --input .. JPG \ --output _output/ bicycle_coco-keypoint. JPG \ -- confident-threshold 0.5 \ --opts model.weights models/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x/137849621/model_final_a6e10b.pklCopy the code
As a result,
Panorama Segmentation – Panoptic FPN
To perform,
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.pklCopy the code
As a result,
WebCam camera use
Get the local WebCam list,
$ ls /dev/video*
/dev/video0 /dev/video1 /dev/video2 /dev/video3
# Check the WebCam list
There are 0, 2 videos
# - The first is video, the second is metadata
# - Metadata node added from Linux Kernel 4.16$sudo apt install v4l-utils $v4l2-ctl --list-devices HD Webcam: HD Webcam (usB-0000 :00:14.0-13): $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# check the format, resolution and FPS information supported by a WebCam
$ v4l2-ctl -d 2 --list-formats-ext
Copy the code
Demo /demo.py can modify the desired open camera and its resolution, etc.
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)}")
Copy the code
Run,
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
Copy the code
Effect,
GoCoding personal practice experience sharing, please pay attention to the public account!