构建
构建 “dnn_objectect” 模块的示例。有关详细信息,请参阅 OpenCV 构建教程。启用 BUILD_EXAMPLES=ON CMake 选项并构建以下目标 (Linux):
- example_dnn_objdetect_image_classification
- example_dnn_objdetect_obj_detect
从 opencv_extra/dnn_objdetect 下载权重文件和模型定义文件
对象检测
example_dnn_objdetect_obj_detect <模型定义文件> <模型权重文件> <测试图像>
以下所有示例均在配备 Intel(R) Core(TM)2 i3-4005U CPU @ 1.70GHz(无 GPU)的笔记本电脑上运行。
该模型速度非常快,平均只需 0.172091 秒即可预测多个边界框。
<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/aeroplane.jpg
总共检测到 1 个对象,耗时 0.168792 秒
------
类别:aeroplane
概率:0.845181
坐标:41 116 415 254
------
Train_Dets
<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/bus.jpg
总共检测到 1 个对象,耗时 0.201276 秒
------
类别:bus
概率:0.701829
坐标:0 32 415 244
------
Train_Dets
<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/cat.jpg
总共检测到 1 个对象,耗时 0.190335 秒
------
类别:cat
概率:0.703465
坐标:34 0 381 282
------
Train_Dets
<bin_path>/example_dnn_objdetect_obj_detect SqueezeDet_deploy.prototxt SqueezeDet.caffemodel tutorials/images/persons_mutli.jpg
总共检测到 2 个对象,耗时 0.169152 秒
------
类别:person
概率:0.737349
坐标:160 67 313 363
------
类别:person
概率:0.720328
坐标:187 198 222 323
------
Train_Dets
继续使用其他图像运行模型!
更改阈值
默认情况下,此模型会将检测的置信度阈值设置为 0.53。在过滤预测的多个边界框时,您可以手动控制阈值,方法是传递可选参数 threshold 的值,如下所示:
<bin_path>/example_dnn_objdetect_obj_detect <模型定义文件> <模型权重文件> <测试图像> <threshold>
将阈值更改为例如 0.0,会产生以下结果:
Train_Dets
这似乎没有什么帮助!
图像分类
example_dnn_objdetect_image_classification <模型定义文件> <模型权重文件> <测试图像>
由于模型的大小为 4.9MB,因此仅需 0.136401 秒即可对图像进行分类。
在示例上运行模型会产生以下结果:
<bin_path>/example_dnn_objdetect_image_classification SqueezeNet_deploy.prototxt SqueezeNet.caffemodel tutorials/images/aeroplane.jpg
最佳类别索引:404
耗时:0.137722
概率:77.1757
查看 synset_words.txt,预测的类别属于 airliner
<bin_path>/example_dnn_objdetect_image_classification SqueezeNet_deploy.prototxt SqueezeNet.caffemodel tutorials/images/cat.jpg
最佳类别索引:285
耗时:0.136401
概率:40.7111
这属于类别:Egyptian cat
<bin_path>/example_dnn_objdetect_image_classification SqueezeNet_deploy.prototxt SqueezeNet.caffemodel tutorials/images/space_shuttle.jpg
最佳类别索引:812
耗时:0.137792
概率:15.8467
这属于类别:space shuttle