Skip to content

Commit

Permalink
Merge pull request #443 from Nuzhny007/master
Browse files Browse the repository at this point in the history
VitTracker and NanoTracker
  • Loading branch information
Nuzhny007 authored Jan 29, 2024
2 parents 814ed80 + 0108b84 commit d4e8096
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/Tracker/Ctracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ void CTracker::UpdateTrackingState(const regions_t& regions,
#if DRAW_DBG_ASSIGNMENT
std::cout << "CTracker::UpdateTrackingState: m_tracks = " << N << ", regions = " << M << std::endl;

int fontType = cv::FONT_HERSHEY_TRIPLEX;
double fontSize = 0.6;
cv::Scalar colorRegionEllow(0, 255, 255);
cv::Scalar colorMatchedAboveThreshRed(0, 0, 255);
cv::Scalar colorMatchedGreen(0, 255, 0);
cv::Scalar colorMatchedNearMargenta(255, 0, 255);
cv::Scalar colorNotMatchedNearWhite(255, 255, 255);
cv::Scalar colorUnknownBlue(255, 0, 0);

auto DrawRRect = [](cv::Mat& img, const cv::RotatedRect& rr, const cv::Scalar& cl, int thikness)
{
cv::Point2f rectPoints[4];
Expand Down Expand Up @@ -221,7 +230,7 @@ void CTracker::UpdateTrackingState(const regions_t& regions,
#endif
for (const auto& reg : regions)
{
DrawRRect(dbgAssignment, reg.m_rrect, cv::Scalar(0, 255, 255), 2);
DrawRRect(dbgAssignment, reg.m_rrect, colorRegionEllow, 2);
}
}
#endif
Expand Down Expand Up @@ -256,25 +265,25 @@ void CTracker::UpdateTrackingState(const regions_t& regions,
#if DRAW_DBG_ASSIGNMENT
std::cout << "CTracker::UpdateTrackingState: Clean assignment from pairs with large distance" << std::endl;
#endif
for (size_t i = 0; i < assignment.size(); i++)
for (size_t i = 0; i < assignment.size(); ++i)
{
#if DRAW_DBG_ASSIGNMENT
std::stringstream ss;
if (assignment[i] != -1)
{
ss << std::fixed << std::setprecision(2) << costMatrix[i + assignment[i] * N];
ss << m_tracks[i]->GetID().ID2Str() << "-" << assignment[i] << ": " << std::fixed << std::setprecision(2) << costMatrix[i + assignment[i] * N];

if (costMatrix[i + assignment[i] * N] > m_settings.m_distThres)
{
ss << ">" << m_settings.m_distThres;
cv::line(dbgAssignment, m_tracks[i]->GetLastRect().center, regions[assignment[i]].m_rrect.center, cv::Scalar(0, 0, 255), 1);
DrawRRect(dbgAssignment, m_tracks[i]->LastRegion().m_rrect, cv::Scalar(0, 0, 255), 1);
cv::line(dbgAssignment, m_tracks[i]->GetLastRect().center, regions[assignment[i]].m_rrect.center, colorMatchedAboveThreshRed, 2);
DrawRRect(dbgAssignment, m_tracks[i]->LastRegion().m_rrect, colorMatchedAboveThreshRed, 1);
}
else
{
ss << "<" << m_settings.m_distThres;
cv::line(dbgAssignment, m_tracks[i]->GetLastRect().center, regions[assignment[i]].m_rrect.center, cv::Scalar(0, 255, 0), 1);
DrawRRect(dbgAssignment, m_tracks[i]->LastRegion().m_rrect, cv::Scalar(0, 255, 0), 1);
cv::line(dbgAssignment, m_tracks[i]->GetLastRect().center, regions[assignment[i]].m_rrect.center, colorMatchedGreen, 1);
DrawRRect(dbgAssignment, m_tracks[i]->LastRegion().m_rrect, colorMatchedGreen, 1);
}

for (size_t ri = 0; ri < regions.size(); ++ri)
Expand All @@ -285,15 +294,15 @@ void CTracker::UpdateTrackingState(const regions_t& regions,
liness << std::fixed << std::setprecision(2) << costMatrix[i + ri * N];
auto p1 = m_tracks[i]->GetLastRect().center;
auto p2 = regions[ri].m_rrect.center;
cv::line(dbgAssignment, p1, p2, cv::Scalar(255, 0, 255), 1);
cv::putText(dbgAssignment, liness.str(), cv::Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2), cv::FONT_HERSHEY_DUPLEX, 0.7, cv::Scalar(255, 255, 255), 1, 8);
cv::line(dbgAssignment, p1, p2, colorMatchedNearMargenta, 1);
cv::putText(dbgAssignment, liness.str(), cv::Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2), fontType, fontSize, colorMatchedNearMargenta, 1, 8);
}
}
}
else
{
// If track have no assigned detect, then increment skipped frames counter.
DrawRRect(dbgAssignment, m_tracks[i]->LastRegion().m_rrect, cv::Scalar(255, 0, 255), 1);
DrawRRect(dbgAssignment, m_tracks[i]->LastRegion().m_rrect, colorNotMatchedNearWhite, 1);
for (size_t ri = 0; ri < regions.size(); ++ri)
{
if (costMatrix[i + ri * N] < 1)
Expand All @@ -302,15 +311,15 @@ void CTracker::UpdateTrackingState(const regions_t& regions,
liness << std::fixed << std::setprecision(2) << costMatrix[i + ri * N];
auto p1 = m_tracks[i]->GetLastRect().center;
auto p2 = regions[ri].m_rrect.center;
cv::line(dbgAssignment, p1, p2, cv::Scalar(255, 255, 255), 1);
cv::putText(dbgAssignment, liness.str(), cv::Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2), cv::FONT_HERSHEY_DUPLEX, 0.7, cv::Scalar(255, 255, 255), 1, 8);
cv::line(dbgAssignment, p1, p2, colorNotMatchedNearWhite, 1);
cv::putText(dbgAssignment, liness.str(), cv::Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2), fontType, fontSize, colorNotMatchedNearWhite, 1, 8);
}
}
}
if (ss.str().length() > 0)
{
auto brect = m_tracks[i]->LastRegion().m_brect;
cv::putText(dbgAssignment, ss.str(), cv::Point(brect.x, brect.y), cv::FONT_HERSHEY_DUPLEX, 0.7, cv::Scalar(255, 0, 255), 1, 8);
cv::putText(dbgAssignment, ss.str(), cv::Point(brect.x, brect.y), fontType, fontSize, colorUnknownBlue, 1, 8);
}
#endif

Expand Down Expand Up @@ -651,7 +660,7 @@ track_t CTracker::GetEllipseDist(const CTrack& trackRef, const CRegion& reg)
{
cv::Size_<track_t> minRadius;

if (m_settings.m_minAreaRadiusPix < 0)
if (m_settings.m_minAreaRadiusPix <= 0)
{
minRadius.width = m_settings.m_minAreaRadiusK * trackRef.LastRegion().m_rrect.size.width;
minRadius.height = m_settings.m_minAreaRadiusK * trackRef.LastRegion().m_rrect.size.height;
Expand Down
89 changes: 89 additions & 0 deletions src/Tracker/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ void CTrack::RectUpdate(const CRegion& region,
case tracking::TrackMOSSE:
case tracking::TrackCSRT:
case tracking::TrackDaSiamRPN:
case tracking::TrackNano:
case tracking::TrackVit:
#ifdef USE_OCV_KCF
{
roiRect.width = std::max(3 * brect.width, currFrame.cols / 4);
Expand Down Expand Up @@ -803,6 +805,8 @@ void CTrack::RectUpdate(const CRegion& region,
case tracking::TrackMOSSE:
case tracking::TrackCSRT:
case tracking::TrackDaSiamRPN:
case tracking::TrackNano:
case tracking::TrackVit:
#ifdef USE_OCV_KCF
{
cv::Rect roiRect;
Expand Down Expand Up @@ -1113,6 +1117,26 @@ void CTrack::CreateExternalTracker(int channels)
{
#if (((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR > 5)) || ((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR == 5) && (CV_VERSION_REVISION > 2)) || (CV_VERSION_MAJOR > 4))
cv::TrackerDaSiamRPN::Params params;
params.model = "dasiamrpn_model.onnx";
params.kernel_cls1 = "dasiamrpn_kernel_cls1.onnx";
params.kernel_r1 = "dasiamrpn_kernel_r1.onnx";
// backend
// 0: automatically (by default)
// 1: Halide language
// 2: Intel's Deep Learning Inference Engine
// 3: OpenCV implementation
// 4: VKCOM
// 5: CUDA
params.backend = 0;
// target
// 0: CPU target (by default)
// 1: OpenCL
// 2: OpenCL fp16 (half-float precision)
// 3: VPU
// 4: Vulkan
// 6: CUDA
// 7: CUDA fp16 (half-float preprocess)
params.target = 0;
m_tracker = cv::TrackerDaSiamRPN::create(params);
#endif
}
Expand All @@ -1121,6 +1145,71 @@ void CTrack::CreateExternalTracker(int channels)
m_VOTTracker = nullptr;
break;

case tracking::TrackNano:
#ifdef USE_OCV_KCF
if (!m_tracker || m_tracker.empty())
{
#if (((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR > 5)) || ((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR == 5) && (CV_VERSION_REVISION > 2)) || (CV_VERSION_MAJOR > 4))
cv::TrackerNano::Params params;
params.backbone = "nanotrack_backbone_sim.onnx";
params.neckhead = "nanotrack_head_sim.onnx";
// backend
// 0: automatically (by default)
// 1: Halide language
// 2: Intel's Deep Learning Inference Engine
// 3: OpenCV implementation
// 4: VKCOM
// 5: CUDA
params.backend = 0;
// target
// 0: CPU target (by default)
// 1: OpenCL
// 2: OpenCL fp16 (half-float precision)
// 3: VPU
// 4: Vulkan
// 6: CUDA
// 7: CUDA fp16 (half-float preprocess)
params.target = 0;
m_tracker = cv::TrackerNano::create(params);
#endif
}
#endif
if (m_VOTTracker)
m_VOTTracker = nullptr;
break;

case tracking::TrackVit:
#ifdef USE_OCV_KCF
if (!m_tracker || m_tracker.empty())
{
#if (((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR > 8)) || (CV_VERSION_MAJOR > 4))
cv::TrackerVit::Params params;
params.net = "vitTracker.onnx";
// backend
// 0: automatically (by default)
// 1: Halide language
// 2: Intel's Deep Learning Inference Engine
// 3: OpenCV implementation
// 4: VKCOM
// 5: CUDA
params.backend = 0;
// target
// 0: CPU target (by default)
// 1: OpenCL
// 2: OpenCL fp16 (half-float precision)
// 3: VPU
// 4: Vulkan
// 6: CUDA
// 7: CUDA fp16 (half-float preprocess)
params.target = 0;
m_tracker = cv::TrackerVit::create(params);
#endif
}
#endif
if (m_VOTTracker)
m_VOTTracker = nullptr;
break;

case tracking::TrackDAT:
#ifdef USE_OCV_KCF
if (m_tracker && !m_tracker.empty())
Expand Down
2 changes: 2 additions & 0 deletions src/common/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ enum LostTrackType
TrackSTAPLE,
TrackLDES,
TrackDaSiamRPN,
TrackNano,
TrackVit,
SingleTracksCount
};
}

0 comments on commit d4e8096

Please sign in to comment.