GOTURN——基于深度学习的物体追踪 (OpenCV contrib)

前言

见惯了KCF、DCF、MEDIANFLOW这些常规追踪器,来看看基于神经网络的GOTURN吧。

首先,这个Tracker并不算慢,利用GPU能跑到100fps

论文原文:Learning to Track at 100 FPS with Deep Regression Networks

撇开原理,从工程的角度来看,算法输入前后两帧被裁剪过的图像,送入卷积神经网络(CNN),继而回归出物体的boudingbox,输出结果。

GOTURN——基于深度学习的物体追踪 (OpenCV contrib)_第1张图片

算法结构:

GOTURN——基于深度学习的物体追踪 (OpenCV contrib)_第2张图片

如下图所示,首先获取到上一帧的结果(第二行图),以中心为原点扩大一圈得到Rect,利用这个Rect裁剪当前帧,分别送入两个五层的卷积网络,再经过三个全连层回归出最终的结果。好了不扯原理了,来看看如何用OpenCV把这个Tracker跑起来。

 

====================利用OpenCV运行GOTURN==================

1.下载模型


首先,当然是要下载别人训练好的模型文件咯

下载地址:地址

如果上面的地址下载速度太慢,可以用这个  地址  或者Github地址(需要自己拼一下)

2.配置OpenCV_contrib环境


保证OpenCV版本在3.4.1以上,编译最新的contrib模型

可以参考我之前的博客:地址

3.简单粗暴的代码


记得把视频的路径换成自己的,一开始需要自己框选一下目标所在的位置。

/*
Copyright 2018 Satya Mallick (LearnOpenCV.com)
*/
 
#include 
#include 
 
using namespace cv;
using namespace std;
 
#define SSTR( x ) static_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
 
int main(int argc, char **argv)
{
    // Create tracker
    Ptr tracker = TrackerGOTURN::create();
 
    // Read video
    VideoCapture video("vtest.avi");
 
    // Exit if video is not opened
    if (!video.isOpened())
    {
        cout << "Could not read video file" << endl;
        return EXIT_FAILURE;
    }
 
    // Read first frame
    Mat frame;
    if (!video.read(frame))
    {
        cout << "Cannot read video file" << endl;
        return EXIT_FAILURE;
    }
 
    // Define initial boundibg box
    Rect2d bbox = selectROI(frame, false);// (287, 23, 86, 320);
 
    // Uncomment the line below to select a different bounding box
    //bbox = selectROI(frame, false);
 
    // Initialize tracker with first frame and bounding box
    tracker->init(frame, bbox);
 
    while (video.read(frame))
    {
        // Start timer
        double timer = (double)getTickCount();
 
        // Update the tracking result
        bool ok = tracker->update(frame, bbox);
 
        // Calculate Frames per second (FPS)
        float fps = getTickFrequency() / ((double)getTickCount() - timer);
 
        if (ok)
        {
            // Tracking success : Draw the tracked object
            rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1);
        }
        else
        {
            // Tracking failure detected.
            putText(frame, "Tracking failure detected", Point(100, 80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 0, 255), 2);
        }
 
        // Display tracker type on frame
        putText(frame, "GOTURN Tracker", Point(100, 20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);
 
        // Display FPS on frame
        putText(frame, "FPS : " + SSTR(int(fps)), Point(100, 50), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50, 170, 50), 2);
 
        // Display frame.
        imshow("Tracking", frame);
 
        // Exit if ESC pressed.
        if (waitKey(1) == 27) break;
    }
 
    return EXIT_SUCCESS;
}


比较好的效果:(大概脸是在训练样本里的,所以效果会好一些

图片转载自   地址

比较差的效果:(而行人估计是没有的,所以想效果好还是需要自己训练一下模型

体验感觉不是很好,用自己的数据训练下肯定会好很多的吧。。。

 


参考:

1.learnopencv博客  地址

2.Github:  地址

你可能感兴趣的:(GOTURN——基于深度学习的物体追踪 (OpenCV contrib))