在上一个博客:detectron2实现Faster RCNN目标检测
讲解了如何实现,这一个博客剖析其中一些关键代码。
1,准备及目的
- detectron2项目地址
- 目的是跟踪代码,清楚代码的相关部分的作用。
安装:
pip install -U torch torchvision cython
pip install -U 'git+https://github.com/facebookresearch/fvcore.git' 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
git clone https://github.com/facebookresearch/detectron2 detectron2_repo
pip install -e detectron2_repo
2,Faster RCNN目标检测
步骤和detectron2实现Faster RCNN目标检测一样
在终端输入:
python3 demo.py --config-file ../configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml \
--input ../img/1.jpg \
--output ../img/1_1.jpg \
--opts MODEL.WEIGHTS detectron2://COCO-Detection/faster_rcnn_R_50_FPN_3x/137849458/model_final_280758.pkl
3.错误解决
运行过程中可能会出现
Traceback (most recent call last):
File “demo.py”, line 7, in
import cv2
File “/opt/conda/lib/python3.7/site-packages/cv2/init.py”, line 5, in
from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
解决方案:
在终端运行:
apt update
apt install libgl1-mesa-glx
3,代码跟踪
在代码中设置输出,在终端中观察输出的顺序。
出现的运行代码文件包括:
- detectron2_repo/demo/demo.py
- detectron2_repo/demo/predictor.py
- detectron2/utils/visualizer.py