Skip to content

Commit

Permalink
Added new CSRT tracker from opencv_contrib (Smorodov#106)
Browse files Browse the repository at this point in the history
* Added hough3d sources

* Calculate 3D hough trajectory lines

* Added fps

* Some warnings removing

* Fixed warning

* Added small crops in DNN detector for High resolution videos and small objects

* Small refactoring

* Some fix for single tracking

* Added openmp flags to the compiler

* Draw trajectory option

* Update Readme

* Fixed link

* Hough3D disabled now in CMake

* Added Yolo (and Tiny Yolo) detector

* Update Readme

* Remove unused Hybrid face detector

* Extract Capture+Detection into the separate thread

* Fixed potential deadlock

* Points visualization

* Control out the frame moving

* Remove hough3d from master

* Fixed NMS for object with different types

* Fix in Readme and TODO

* OpenCV 4.0 ready

* Added YOLO v3 tiny

* Added compute target for DNN examples (default is OpenCL_FP16)

* Fixed compatible bug with OpenCV version < 4.0

* Fixed project name in CMake

* Simple implementation of the Abandoned detection

* Increased timeout

* Fixed help message

* Added Abandoned to the Readme

* Fixed Readme

* Switch travis to the latest OpenCV 3.4.1 version

* Set timeouts to 60 sec

* Fixed travis script for OpenCV 3.4.1

* Fixed travis for gcc6

* Fixed linker error

* Fixed compiler in travis

* Fixed travis and CMakeLists for C++14 support

* Capture for webcams

* Fix bug with ROI for local tracking

* Automatic inertia correction in linear Kalman filter

* Update TODO

* Yolo eample refactoring

* Refactoring: split project to share library and example

* Added cars counting example

* Fix for OpenCV 4.0

* Single threaded cars counting example

* Added lines class

* Added API for road lines

* Added cars counting code

* Changed road line caption

* Fixed bug with roi rect clamping

* To Linear Kalman filter added speed for rectangle size

* Improved quality of line intersection algorithm

* Fixed bug with Kalman width and height correction

* Return MOSSE tracker

* Temporal correction object size when selected only points filter

* Some small improvements

* Compilation on Windows MSVC 2017 with OpenCV 4.0

* Added new CSRT tracker from opencv_contrib

* Fixed some warnings

* Fixed type conversion warnings

* In CMake added options for enable|disable building cars counting and all other examples
  • Loading branch information
Nuzhny007 authored and Smorodov committed Oct 25, 2018
1 parent b2a1ea6 commit 98ba77e
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 67 deletions.
17 changes: 12 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.5)

project(MultitargetTracking)
project(MTTracking)

unset(CMAKE_CXX_FLAGS CACHE)

Expand All @@ -18,14 +18,21 @@ elseif (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /W4 -DGTL_STATIC" CACHE STRING COMPILE_FLAGS FORCE)
endif()


set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

FIND_PACKAGE(OpenCV)

add_subdirectory(src)
add_subdirectory(example)
add_subdirectory(cars_counting)

option(BUILD_EXAMPLES "Should compiled examples (motion detection, pedestrians, faces, DNNs etc)?" ON)
if (BUILD_EXAMPLES)
add_subdirectory(example)
endif(BUILD_EXAMPLES)

option(BUILD_CARS_COUNTING "Should compiled Cars counting example?" OFF)
if (BUILD_CARS_COUNTING)
add_subdirectory(cars_counting)
endif(BUILD_CARS_COUNTING)

2 changes: 1 addition & 1 deletion cars_counting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.8)
cmake_minimum_required (VERSION 3.5)

project(CarsCounting)

Expand Down
43 changes: 32 additions & 11 deletions cars_counting/CarsCounting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ void CarsCounting::Process()

m_fps = std::max(1.f, (float)capture.get(cv::CAP_PROP_FPS));

m_fps = std::max(1.f, (float)capture.get(cv::CAP_PROP_FPS));

cv::Mat colorFrame;
cv::UMat grayFrame;
for (;;)
Expand Down Expand Up @@ -189,11 +191,19 @@ void CarsCounting::DrawTrack(cv::Mat frame,

if (isStatic)
{
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, cv::LINE_AA);
#else
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
#endif
}
else
{
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
#else
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
#endif
}

if (drawTrajectory)
Expand All @@ -204,11 +214,18 @@ void CarsCounting::DrawTrack(cv::Mat frame,
{
const TrajectoryPoint& pt1 = track.m_trace.at(j);
const TrajectoryPoint& pt2 = track.m_trace.at(j + 1);

cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, cv::LINE_AA);
#else
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
#endif
if (!pt2.m_hasRaw)
{
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, cv::LINE_AA);
#else
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
#endif
}
}
}
Expand All @@ -219,7 +236,11 @@ void CarsCounting::DrawTrack(cv::Mat frame,

for (auto pt : track.m_lastRegion.m_points)
{
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, cv::LINE_AA);
#else
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
#endif
}
}
}
Expand Down Expand Up @@ -254,24 +275,24 @@ bool CarsCounting::InitTracker(cv::UMat frame)
settings.m_distType = tracking::DistCenters;
settings.m_kalmanType = tracking::KalmanLinear;
settings.m_filterGoal = tracking::FilterRect;
settings.m_lostTrackType = tracking::TrackKCF; // Use KCF tracker for collisions resolving
settings.m_lostTrackType = tracking::TrackerCSRT; // Use KCF tracker for collisions resolving
settings.m_matchType = tracking::MatchHungrian;
settings.m_dt = 0.5f; // Delta time for Kalman filter
settings.m_accelNoiseMag = 0.5f; // Accel noise magnitude for Kalman filter
settings.m_distThres = frame.rows / 15; // Distance threshold between region and object on two frames
settings.m_distThres = frame.rows / 15.f; // Distance threshold between region and object on two frames

settings.m_useAbandonedDetection = false;
if (settings.m_useAbandonedDetection)
{
settings.m_minStaticTime = minStaticTime;
settings.m_maxStaticTime = 60;
settings.m_maximumAllowedSkippedFrames = settings.m_minStaticTime * m_fps; // Maximum allowed skipped frames
settings.m_maximumAllowedSkippedFrames = cvRound(settings.m_minStaticTime * m_fps); // Maximum allowed skipped frames
settings.m_maxTraceLength = 2 * settings.m_maximumAllowedSkippedFrames; // Maximum trace length
}
else
{
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
settings.m_maxTraceLength = 4 * m_fps; // Maximum trace length
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
settings.m_maxTraceLength = cvRound(4 * m_fps); // Maximum trace length
}

m_tracker = std::make_unique<CTracker>(settings);
Expand Down
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.8)
cmake_minimum_required (VERSION 3.5)

project(MultitargetTracker)

Expand Down
12 changes: 10 additions & 2 deletions example/MouseExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ void MouseTracking(cv::CommandLineParser parser)

for (size_t i = 0; i < pts.size(); i++)
{
cv::circle(frame, pts[i], 3, cv::Scalar(0, 255, 0), 1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::circle(frame, pts[i], 3, cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
#else
cv::circle(frame, pts[i], 3, cv::Scalar(0, 255, 0), 1, CV_AA);
#endif
}

tracker.Update(regions, cv::UMat(), 100);
Expand All @@ -101,7 +105,11 @@ void MouseTracking(cv::CommandLineParser parser)
{
for (size_t j = 0; j < track->m_trace.size() - 1; j++)
{
cv::line(frame, track->m_trace[j], track->m_trace[j + 1], colors[i % colors.size()], 2, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::line(frame, track->m_trace[j], track->m_trace[j + 1], colors[i % colors.size()], 2, cv::LINE_AA);
#else
cv::line(frame, track->m_trace[j], track->m_trace[j + 1], colors[i % colors.size()], 2, CV_AA);
#endif
}
}
}
Expand Down
31 changes: 25 additions & 6 deletions example/VideoExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,19 @@ void VideoExample::DrawTrack(cv::Mat frame,

if (isStatic)
{
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, cv::LINE_AA);
#else
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(255, 0, 255), 2, CV_AA);
#endif
}
else
{
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, cv::LINE_AA);
#else
cv::rectangle(frame, ResizeRect(track.GetLastRect()), cv::Scalar(0, 255, 0), 1, CV_AA);
#endif
}

if (drawTrajectory)
Expand All @@ -337,11 +345,18 @@ void VideoExample::DrawTrack(cv::Mat frame,
{
const TrajectoryPoint& pt1 = track.m_trace.at(j);
const TrajectoryPoint& pt2 = track.m_trace.at(j + 1);

cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, cv::LINE_AA);
#else
cv::line(frame, ResizePoint(pt1.m_prediction), ResizePoint(pt2.m_prediction), cl, 1, CV_AA);
#endif
if (!pt2.m_hasRaw)
{
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, cv::LINE_AA);
#else
cv::circle(frame, ResizePoint(pt2.m_prediction), 4, cl, 1, CV_AA);
#endif
}
}
}
Expand All @@ -352,7 +367,11 @@ void VideoExample::DrawTrack(cv::Mat frame,

for (auto pt : track.m_lastRegion.m_points)
{
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
#if (CV_VERSION_MAJOR >= 4)
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, cv::LINE_AA);
#else
cv::circle(frame, cv::Point(cvRound(pt.x), cvRound(pt.y)), 1, cl, -1, CV_AA);
#endif
}
}
}
42 changes: 25 additions & 17 deletions example/VideoExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,20 @@ class MotionDetectorExample : public VideoExample
settings.m_matchType = tracking::MatchHungrian;
settings.m_dt = 0.4f; // Delta time for Kalman filter
settings.m_accelNoiseMag = 0.5f; // Accel noise magnitude for Kalman filter
settings.m_distThres = frame.rows / 20; // Distance threshold between region and object on two frames
settings.m_distThres = frame.rows / 20.f; // Distance threshold between region and object on two frames

settings.m_useAbandonedDetection = true;
if (settings.m_useAbandonedDetection)
{
settings.m_minStaticTime = minStaticTime;
settings.m_maxStaticTime = 60;
settings.m_maximumAllowedSkippedFrames = settings.m_minStaticTime * m_fps; // Maximum allowed skipped frames
settings.m_maximumAllowedSkippedFrames = cvRound(settings.m_minStaticTime * m_fps); // Maximum allowed skipped frames
settings.m_maxTraceLength = 2 * settings.m_maximumAllowedSkippedFrames; // Maximum trace length
}
else
{
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
settings.m_maxTraceLength = 4 * m_fps; // Maximum trace length
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
settings.m_maxTraceLength = cvRound(4 * m_fps); // Maximum trace length
}

m_tracker = std::make_unique<CTracker>(settings);
Expand Down Expand Up @@ -235,8 +235,8 @@ class FaceDetectorExample : public VideoExample
settings.m_dt = 0.3f; // Delta time for Kalman filter
settings.m_accelNoiseMag = 0.1f; // Accel noise magnitude for Kalman filter
settings.m_distThres = 0.8f; // Distance threshold between region and object on two frames
settings.m_maximumAllowedSkippedFrames = m_fps / 2; // Maximum allowed skipped frames
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
settings.m_maximumAllowedSkippedFrames = cvRound(m_fps / 2); // Maximum allowed skipped frames
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length

m_tracker = std::make_unique<CTracker>(settings);

Expand Down Expand Up @@ -313,9 +313,9 @@ class PedestrianDetectorExample : public VideoExample
settings.m_matchType = tracking::MatchHungrian;
settings.m_dt = 0.3f; // Delta time for Kalman filter
settings.m_accelNoiseMag = 0.1f; // Accel noise magnitude for Kalman filter
settings.m_distThres = frame.rows / 10; // Distance threshold between region and object on two frames
settings.m_maximumAllowedSkippedFrames = m_fps; // Maximum allowed skipped frames
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
settings.m_distThres = frame.rows / 10.f; // Distance threshold between region and object on two frames
settings.m_maximumAllowedSkippedFrames = cvRound(m_fps); // Maximum allowed skipped frames
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length

m_tracker = std::make_unique<CTracker>(settings);

Expand Down Expand Up @@ -391,9 +391,9 @@ class SSDMobileNetExample : public VideoExample
settings.m_matchType = tracking::MatchHungrian;
settings.m_dt = 0.3f; // Delta time for Kalman filter
settings.m_accelNoiseMag = 0.1f; // Accel noise magnitude for Kalman filter
settings.m_distThres = frame.rows / 10; // Distance threshold between region and object on two frames
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
settings.m_distThres = frame.rows / 10.f; // Distance threshold between region and object on two frames
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length

m_tracker = std::make_unique<CTracker>(settings);

Expand Down Expand Up @@ -424,7 +424,11 @@ class SSDMobileNetExample : public VideoExample
int baseLine = 0;
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
auto rect(track->GetLastRect());
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
#if (CV_VERSION_MAJOR >= 4)
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), cv::FILLED);
#else
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
#endif
cv::putText(frame, label, cv::Point(rect.x, rect.y), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
}
}
Expand Down Expand Up @@ -502,9 +506,9 @@ class YoloExample : public VideoExample
settings.m_matchType = tracking::MatchHungrian;
settings.m_dt = 0.3f; // Delta time for Kalman filter
settings.m_accelNoiseMag = 0.2f; // Accel noise magnitude for Kalman filter
settings.m_distThres = frame.rows / 10; // Distance threshold between region and object on two frames
settings.m_maximumAllowedSkippedFrames = 2 * m_fps; // Maximum allowed skipped frames
settings.m_maxTraceLength = 5 * m_fps; // Maximum trace length
settings.m_distThres = frame.rows / 10.f; // Distance threshold between region and object on two frames
settings.m_maximumAllowedSkippedFrames = cvRound(2 * m_fps); // Maximum allowed skipped frames
settings.m_maxTraceLength = cvRound(5 * m_fps); // Maximum trace length

m_tracker = std::make_unique<CTracker>(settings);

Expand Down Expand Up @@ -535,7 +539,11 @@ class YoloExample : public VideoExample
int baseLine = 0;
cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
auto rect(track->GetLastRect());
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
#if (CV_VERSION_MAJOR >= 4)
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), cv::FILLED);
#else
cv::rectangle(frame, cv::Rect(cv::Point(rect.x, rect.y - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED);
#endif
cv::putText(frame, label, cv::Point(rect.x, rect.y), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
}
}
Expand Down
Loading

0 comments on commit 98ba77e

Please sign in to comment.