From 2972f8de8e896791dc690206216e7389dc576102 Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Tue, 20 Feb 2018 02:43:04 +0300 Subject: [PATCH] Fixed Tracking using SparsePyrLKOpticalFlow on CPU --- .circleci/config.yml | 6 ++++-- src/yolo_console_dll.cpp | 1 + src/yolo_v2_class.hpp | 29 +++++++++++------------------ 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 4a79b0a5591..0bae244ef73 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,6 +7,8 @@ jobs: working_directory: ~/work steps: - checkout - - run: make LIBSO=1 GPU=0 CUDNN=0 OPENCV=0 -j 8 - - run: make clean + - run: make LIBSO=1 GPU=0 CUDNN=0 OPENCV=0 -j 8 + - run: make clean + - run: make LIBSO=1 GPU=0 CUDNN=0 OPENCV=1 -j 8 + - run: make clean - run: make LIBSO=1 GPU=1 CUDNN=1 OPENCV=1 -j 8 diff --git a/src/yolo_console_dll.cpp b/src/yolo_console_dll.cpp index 3bd1246b00e..dee4eab8b6a 100644 --- a/src/yolo_console_dll.cpp +++ b/src/yolo_console_dll.cpp @@ -11,6 +11,7 @@ #ifdef _WIN32 #define OPENCV +#define GPU #endif // To use tracking - uncomment the following line. Tracking is supported only by OpenCV 3.x diff --git a/src/yolo_v2_class.hpp b/src/yolo_v2_class.hpp index 4e27b00d0c4..b1b5bf78784 100644 --- a/src/yolo_v2_class.hpp +++ b/src/yolo_v2_class.hpp @@ -323,7 +323,8 @@ class Tracker_optflow { #elif defined(TRACK_OPTFLOW) && defined(OPENCV) -#include +//#include +#include class Tracker_optflow { public: @@ -340,8 +341,7 @@ class Tracker_optflow { } // just to avoid extra allocations - cv::Mat src_mat; - cv::Mat dst_mat, dst_grey; + cv::Mat dst_grey; cv::Mat prev_pts_flow, cur_pts_flow; cv::Mat status, err; @@ -373,15 +373,10 @@ class Tracker_optflow { void update_tracking_flow(cv::Mat new_src_mat, std::vector _cur_bbox_vec) { if (new_src_mat.channels() == 3) { - if (src_mat.cols == 0) { - src_mat = cv::Mat(new_src_mat.size(), new_src_mat.type()); - src_grey = cv::Mat(new_src_mat.size(), CV_8UC1); - } update_cur_bbox_vec(_cur_bbox_vec); - src_mat = new_src_mat; - cv::cvtColor(src_mat, src_grey, CV_BGR2GRAY, 1); + cv::cvtColor(new_src_mat, src_grey, CV_BGR2GRAY, 1); } } @@ -393,19 +388,17 @@ class Tracker_optflow { return cur_bbox_vec; } - if (dst_mat.cols == 0) { - dst_mat = cv::Mat(new_dst_mat.size(), new_dst_mat.type()); - dst_grey = cv::Mat(new_dst_mat.size(), CV_8UC1); - } - - dst_mat = new_dst_mat; - cv::cvtColor(dst_mat, dst_grey, CV_BGR2GRAY, 1); + cv::cvtColor(new_dst_mat, dst_grey, CV_BGR2GRAY, 1); if (src_grey.rows != dst_grey.rows || src_grey.cols != dst_grey.cols) { src_grey = dst_grey.clone(); return cur_bbox_vec; } + if (prev_pts_flow.cols < 1) { + return cur_bbox_vec; + } + ////sync_PyrLKOpticalFlow_gpu.sparse(src_grey_gpu, dst_grey_gpu, prev_pts_flow_gpu, cur_pts_flow_gpu, status_gpu, &err_gpu); // OpenCV 2.4.x sync_PyrLKOpticalFlow->calc(src_grey, dst_grey, prev_pts_flow, cur_pts_flow, status, err); // OpenCV 3.x @@ -413,7 +406,7 @@ class Tracker_optflow { std::vector result_bbox_vec; - if (err.cols == cur_bbox_vec.size() && status.cols == cur_bbox_vec.size()) + if (err.rows == cur_bbox_vec.size() && status.rows == cur_bbox_vec.size()) { for (size_t i = 0; i < cur_bbox_vec.size(); ++i) { @@ -438,7 +431,7 @@ class Tracker_optflow { } } - prev_pts_flow = cur_pts_flow; + prev_pts_flow = cur_pts_flow.clone(); return result_bbox_vec; }