The value of knowledge lies not in possession, but in share.

0%

基于ROS运行Caffe-SSD demo【通用】

最近仔细研究了下SSD相关的几个demo文件,在这里做一个简要的记录。

大神weiliu89提供的Caffe/examples/ssd下提供了很多demo程序,常用的有以下这么几个。

关于配置Caffe-SSD请参看官方文档或我之前的笔记不过我之前的教程是基于ROS的

使用ssd_detect.cpp

生成可执行程序:

注:C++程序不自动显示label

  1. 对ssd_detect.cpp进行编译,生成可执行二进制文件,使用ssd_detect.bin接口程序(路径:caffe/build/examples/ssd/),检测image或video;

    1
    2
    3
    4
    5
    6
    7
    build/examples/ssd/ssd_detect.bin models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt \
    models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel \
    examples/videos/test.txt --file_type video \
    --out_file output.txt \
    --confidence_threshold 0.4 #检测视频,阈值为0.4并保存结果
    # 一定要注意上述路径,最易出错
    # 注:若出现错误,请将换行符删除
  2. 使用examples/ssd/plot_detections.py,对ssd_detect.cpp的输出文件,绘制检测结果。

    1
    2
    3
    4
    5
    6
    python examples/ssd/plot_detections.py examples/images/result.txt \
    /home/catkin_ws/src/roc_caffe/caffe \
    --labelmap-file data/VOC0712/labelmap_voc.prototxt \
    --save-dir examples/
    # 一定要注意上述路径,最易出错
    # 注:若出现错误,请将换行符删除

作为类来调用

将ssd_detect.cpp改写为一个头文件(譬如:Detector.h),然后进行调用。

使用ssd_detect.py

1
2
3
4
5
6
7
8
# 先进入caffe主路径,譬如我的路径
cd ~/catkin_ws/src/ros_caffe/caffe/
# 检测单张图片
python examples/ssd/score_ssd_pascal.py
# 检测视频
python examples/ssd/ssd_pascal_video.py
# 通过摄像头,检测实时视频
python examples/ssd/ssd_pascal_webcam.py

Debugs

1
2
3
4
5
6
7
# Bug
ImportError:No module named caffe
# Debug
# 在py代码中添加
import sys,os
caffe_root = '/home/cong/catkin_ws/src/ros_caffe/caffe'
sys.path.insert(0, caffe_root + 'python')

🍭支持一根棒棒糖吧!