一、介绍
If you want to create object detectors then try the scan_fhog_pyramid tool first. It is quite easy to use and train and will, in many cases, give excellent results. If that doesn't give good results then try the more powerful convolutional neural network based detector. 3Also note that dlib contains more powerful CNN based object detection tooling, which will usually run slower but produce much more general and accurate detectors. 4
dlib的官网也说明了,使用fhog特征进行分类就能得到不错的结果,如果对结果不满意,可以使用cnn模型进行训练,但是耗时也会更长。
二、相关名词
2.1 图像金字塔
N越大,每次缩放的尺寸越小,同样下采样到同一个扫描窗口大小,必然花费的时间更多,金字塔中产生图片也就越多,但是获得信息也就越多。
2.2 检测窗口
2.3 fhog 特征
如果想知道计算过程,可以参考这两篇文章
fhog 则是 Felzenszwalb 这位改良后的一个hog特征。
三、检测过程
3.1 模型导入
//1. 图片特征扫描器
typedef dlib::scan_fhog_pyramid<dlib::pyramid_down<N>> image_scanner_type;
//2. 图片位置检测器
dlib::object_detector<image_scanner_type> thumb,thumb_mobile;
//3. 图片位置检测器列表,为什么要一个列表,下面会说到
std::vector<dlib::object_detector<image_scanner_type> > my_detectors;
//4. 导入模型
dlib::deserialize(thumb_svm) >> thumb;
my_detectors.push_back(thumb);
template <typename Pyramid_type, typename Feature_extractor_type =default_fhog_feature_extractor>
class scan_fhog_pyramid : noncopyable{...}
类模板本身要接收两个参数:
- 第一个参数是
pyramid_down
,下采样因子,这个前面已经说过了
deserialize
我们将我们训练好的模型解析出来。3.2 检测
好了,开始检测吧。
dlib::cv_image<dlib::bgr_pixel> cimg(cvMat);//cv::Mat 转成dlib所需的图片格式
std::vector<dlib::rect_detection> &dets;//检测结果的列表,每个结果包含检测物体在图像中的位置
double detect_shreshold = -2;//检测的门限,越小越容易检测出
evaluate_detectors(my_detectors, cimg, dets, detect_shreshold);//
cv_image
_image[imagemage]。rect_detection
是用来盛放检测结果的容器,定义如下: struct rect_detection
{
double detection_confidence; //检测结果的置信度
unsigned long weight_index; //如果有多个模型的话,当前结果是哪个模型序号
rectangle rect;//检测结果的位置信息
bool operator<(const rect_detection& item) const { return detection_confidence < item.detection_confidence; }
};
my_detectors
有多个检测器的话,那检测结果就有可能是多个,即返回的dets有多个元素。这个时候,我们就可以通过判断多个结果的置信度来决定最终的结果是什么。 在这个过程中,我们可以人为的根据某些原因(比如检测结果的尺寸)来更相信其中的一个结果。四、训练过程
训练过程也并不难。
4.1 采集数据
4.2 标注数据
selectROI
函数,作用就是可以接管我们的鼠标事件,然后能够返回圈出来的图片的位置信息,最后就能根据圈出来的位置信息使用OpenCV的图片处理裁剪图片并保存到本地了。这里可以参考boundingBox4.3 训练数据
最后有一个使用训练好的检测器来测试测试集的函数是:
test_object_detection_function(detector, images_test, face_boxes_test)
- 召回率 = 把有点赞的识别成点赞数目 / (把有点赞的识别成点赞数目 + 把有点赞的识别成没有点赞的)
- 精确率 = 把有点赞的识别成点赞数目 / (把有点赞的识别成点赞数目 + 把没有点赞的识别成点赞的数目)
- 正确率 = (把有点赞的识别成点赞数目 + 把没有点赞的识别成没有点赞数目) / (检测样本总数)
最后,推荐我在查询资料过程中,包括写这篇文章过程中找到的一些比较好的文章,可以进一步阅读:
参考链接:
- 下面只是对dlib 使用
scan_fhog_pyramid
进行分类识别的一个示例和代码说明。 ↩ - https://zhuanlan.zhihu.com/p/43740927 ↩
- http://dlib.net/imaging.html ↩
- http://dlib.net/imaging.html#object_detector ↩
- https://cloud.tencent.com/developer/article/1081460 ↩
- https://zhuanlan.zhihu.com/p/43740927 ↩
- https://blog.csdn.net/qq_29153321/article/details/103938829 ↩
- scan_fhog_pyramid ↩
- 默认情况下使用 "fhog.h"中的extract_fhog_feature() 提取特征 ↩
- object_detector ↩
- http://dlib.net/imaging.html#evaluate_detectors ↩
- http://dlib.net/fhog_object_detector_ex.cpp.html ↩
- 如何解释召回率与精确率? ↩
4 条评论
主题做的很不错
大佬就是牛逼
哈哈,开始搞机器学习了么?
上次实验室临时接的一个项目 就学了一下