diff --git a/CMakeLists.txt b/CMakeLists.txt index a9bdcc8c..fed4b1ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,9 +44,9 @@ endif(SILENT_WORK) include(CheckIncludeFileCXX) check_include_file_cxx(filesystem HAVE_FILESYSTEM) if(HAVE_FILESYSTEM) - add_definitions(-DHAVE_FILESYSTEM) message("Founded filesystem header") else(HAVE_FILESYSTEM) + add_definitions(-DHAVE_EXPERIMENTAL_FILESYSTEM) message("Do not found filesystem header") endif(HAVE_FILESYSTEM) diff --git a/async_detector/CMakeLists.txt b/async_detector/CMakeLists.txt index 06b97ebe..2a552a62 100644 --- a/async_detector/CMakeLists.txt +++ b/async_detector/CMakeLists.txt @@ -12,7 +12,7 @@ set(HEADERS AsyncDetector.h # добавляем include директории # ---------------------------------------------------------------------------- INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/../src - ${PROJECT_SOURCE_DIR}/../src/common + ${PROJECT_SOURCE_DIR}/../src/mtracking ${PROJECT_SOURCE_DIR}/../src/Detector ${PROJECT_SOURCE_DIR}/../src/Detector/vibe_src ${PROJECT_SOURCE_DIR}/../src/Detector/Subsense diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 5674e9be..163680cb 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -20,7 +20,7 @@ endif(BUILD_CARS_COUNTING) # добавляем include директории # ---------------------------------------------------------------------------- INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/../src - ${PROJECT_SOURCE_DIR}/../src/common + ${PROJECT_SOURCE_DIR}/../src/mtracking ${PROJECT_SOURCE_DIR}/../src/Detector ${PROJECT_SOURCE_DIR}/../src/Detector/vibe_src ${PROJECT_SOURCE_DIR}/../src/Detector/Subsense diff --git a/example/MotionDetectorExample.h b/example/MotionDetectorExample.h index ac4e5409..7136b758 100644 --- a/example/MotionDetectorExample.h +++ b/example/MotionDetectorExample.h @@ -39,7 +39,7 @@ class MotionDetectorExample final : public VideoExample m_logger->info("MotionDetectorExample::InitDetector"); //m_minObjWidth = frame.cols / 20; - m_minObjWidth = 2; + m_minObjWidth = 4; config_t config; config.emplace("useRotatedRect", "0"); @@ -56,7 +56,7 @@ class MotionDetectorExample final : public VideoExample config.emplace("updateFactor", "16"); break; case tracking::Detectors::Motion_MOG: - config.emplace("history", std::to_string(cvRound(50 * m_fps))); + config.emplace("history", std::to_string(cvRound(5000 * m_fps))); config.emplace("nmixtures", "3"); config.emplace("backgroundRatio", "0.7"); config.emplace("noiseSigma", "0"); @@ -97,7 +97,7 @@ class MotionDetectorExample final : public VideoExample if (!m_trackerSettingsLoaded) { - m_trackerSettings.SetDistance(tracking::DistCenters); + m_trackerSettings.SetDistance(tracking::DistJaccard); m_trackerSettings.m_kalmanType = tracking::KalmanLinear; m_trackerSettings.m_filterGoal = tracking::FilterCenter; m_trackerSettings.m_lostTrackType = tracking::TrackNone; // Use visual objects tracker for collisions resolving. Used if m_filterGoal == tracking::FilterRect @@ -197,8 +197,9 @@ class MotionDetectorExample final : public VideoExample auto velocity = sqrt(sqr(track.m_velocity[0]) + sqr(track.m_velocity[1])); if (track.IsRobust(4, // Minimal trajectory size 0.3f, // Minimal ratio raw_trajectory_points / trajectory_lenght - cv::Size2f(0.2f, 5.0f))) // Min and max ratio: width / height - //velocity > 30 // Velocity more than 30 pixels per second + cv::Size2f(0.2f, 5.0f), // Min and max ratio: width / height + 2)) + //velocity > 30 // Velocity more than 30 pixels per second { //track_t mean = 0; //track_t stddev = 0; diff --git a/example/VideoExample.cpp b/example/VideoExample.cpp index 323cde52..4c41e003 100644 --- a/example/VideoExample.cpp +++ b/example/VideoExample.cpp @@ -153,8 +153,14 @@ void VideoExample::SyncProcess() int64 startLoopTime = cv::getTickCount(); + //double fps = capture.get(cv::CAP_PROP_FPS); + //double readPeriodSeconds = 2.; + //int readPeriodFrames = cvRound(readPeriodSeconds * fps); + for (;;) { + //int currFramesPos = cvRound(capture.get(cv::CAP_PROP_POS_FRAMES)); + size_t i = 0; for (; i < m_batchSize; ++i) { @@ -177,6 +183,8 @@ void VideoExample::SyncProcess() if (i < m_batchSize) break; + //capture.set(cv::CAP_PROP_POS_FRAMES, currFramesPos + readPeriodFrames); + if (!m_isDetectorInitialized || !m_isTrackerInitialized) { cv::UMat ufirst = frameInfo.m_frames[0].GetUMatBGR(); @@ -202,7 +210,6 @@ void VideoExample::SyncProcess() int64 t1 = cv::getTickCount(); - regions_t regions; Detection(frameInfo); Tracking(frameInfo); int64 t2 = cv::getTickCount(); diff --git a/example/VideoExample.h b/example/VideoExample.h index b6b65918..b5e0fc69 100644 --- a/example/VideoExample.h +++ b/example/VideoExample.h @@ -281,7 +281,11 @@ class VideoExample bool m_isDetectorInitialized = false; std::string m_inFile; std::string m_outFile; - int m_fourcc = cv::VideoWriter::fourcc('h', '2', '6', '4'); //cv::VideoWriter::fourcc('M', 'J', 'P', 'G'); +#if 0 + int m_fourcc = cv::VideoWriter::fourcc('h', '2', '6', '4'); +#else + int m_fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G'); +#endif int m_startFrame = 0; int m_endFrame = 0; int m_finishDelay = 0; diff --git a/example/main.cpp b/example/main.cpp index 34e413da..2ef4f32a 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -9,47 +9,39 @@ #include #include -// ---------------------------------------------------------------------- - -static void Help() -{ - printf("\nExamples of the Multitarget tracking algorithm\n" - "Usage: \n" - " ./MultitargetTracker [--example]= [--start_frame]= [--end_frame]= [--end_delay]= [--out]= [--show_logs]= [--async]= [--res]= [--settings]= [--batch_size=] \n\n" - "Press:\n" - "\'m\' key for change mode: play|pause. When video is paused you can press any key for get next frame. \n\n" - "Press Esc to exit from video \n\n" - ); -} - -const char* keys = -{ - "{ @1 |../data/atrium.avi | movie file | }" - "{ e example |1 | number of example 0 - MouseTracking, 1 - MotionDetector, 3 - YOLO TensorRT Detector, 4 - Cars counting | }" - "{ sf start_frame |0 | Start a video from this position | }" - "{ ef end_frame |0 | Play a video to this position (if 0 then played to the end of file) | }" - "{ ed end_delay |0 | Delay in milliseconds after video ending | }" - "{ o out | | Name of result video file | }" - "{ show_logs |info | Show Trackers logs: trace, debug, info, warning, error, critical, off | }" - "{ g gpu |0 | Use OpenCL acceleration | }" - "{ a async |1 | Use 2 theads for processing pipeline | }" - "{ r log_res | | Path to the csv file with tracking result | }" - "{ cvat_res | | Path to the xml file in cvat format with tracking result | }" - "{ s settings | | Path to the ini file with tracking settings | }" - "{ bs batch_size |1 | Batch size - frames count for processing | }" - "{ wf write_n_frame |1 | Write logs on each N frame: 1 for writing each frame | }" - "{ hm heat_map |0 | For CarsCounting: Draw heat map | }" - "{ geo_bind |geo_bind.ini | For CarsCounting: ini file with geographical binding | }" - "{ contrast_adjustment |0 | Use contrast adjustment for frames before detection | }" -}; - -// ---------------------------------------------------------------------- - +///---------------------------------------------------------------------- int main(int argc, char** argv) { + const char* keys = + { + "{ @1 |../data/atrium.avi | movie file | }" + "{ e example |1 | number of example 0 - MouseTracking, 1 - MotionDetector, 3 - YOLO TensorRT Detector, 4 - Cars counting | }" + "{ sf start_frame |0 | Start a video from this position | }" + "{ ef end_frame |0 | Play a video to this position (if 0 then played to the end of file) | }" + "{ ed end_delay |0 | Delay in milliseconds after video ending | }" + "{ o out | | Name of result video file | }" + "{ show_logs |info | Show Trackers logs: trace, debug, info, warning, error, critical, off | }" + "{ g gpu |0 | Use OpenCL acceleration | }" + "{ a async |1 | Use 2 theads for processing pipeline | }" + "{ r log_res | | Path to the csv file with tracking result | }" + "{ cvat_res | | Path to the xml file in cvat format with tracking result | }" + "{ s settings | | Path to the ini file with tracking settings | }" + "{ bs batch_size |1 | Batch size - frames count for processing | }" + "{ wf write_n_frame |1 | Write logs on each N frame: 1 for writing each frame | }" + "{ hm heat_map |0 | For CarsCounting: Draw heat map | }" + "{ geo_bind |geo_bind.ini | For CarsCounting: ini file with geographical binding | }" + "{ contrast_adjustment |0 | Use contrast adjustment for frames before detection | }" + }; + cv::CommandLineParser parser(argc, argv, keys); - Help(); + std::cout << "\nExamples of the Multitarget tracking algorithm\n" + "Usage: \n" + " ./MultitargetTracker [--example]= [--start_frame]= [--end_frame]= [--end_delay]= [--out]= [--show_logs]= [--async]= [--res]= [--settings]= [--batch_size=] \n\n" + "Press:\n" + "\'m\' key for change mode: play|pause. When video is paused you can press any key for get next frame. \n\n" + "Press Esc to exit from video \n" << std::endl; + parser.printMessage(); bool useOCL = parser.get("gpu") != 0; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5a017769..5aa83f30 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,7 +23,7 @@ else() set(LIB_PTHREAD pthread) endif() - include_directories(common) + include_directories(mtracking) pybind11_add_module(pymtracking ${mtracker_python_src} ${mtracker_python_inc}) target_link_libraries(pymtracking PRIVATE mtracking mdetection ${OpenCV_LIBS} ${PYTHON_LIBRARY} pybind11::module) diff --git a/src/Detector/BackgroundSubtract.h b/src/Detector/BackgroundSubtract.h index c815be08..31dfe5d9 100644 --- a/src/Detector/BackgroundSubtract.h +++ b/src/Detector/BackgroundSubtract.h @@ -1,6 +1,6 @@ #pragma once -#include "defines.h" +#include "mtracking/defines.h" #include "vibe_src/vibe.hpp" #ifdef USE_OCV_BGFG diff --git a/src/Detector/BaseDetector.h b/src/Detector/BaseDetector.h index b3c4c5d3..7a24f336 100644 --- a/src/Detector/BaseDetector.h +++ b/src/Detector/BaseDetector.h @@ -1,7 +1,7 @@ #pragma once #include -#include "defines.h" +#include "mtracking/defines.h" /// /// \brief The KeyVal struct diff --git a/src/Detector/CMakeLists.txt b/src/Detector/CMakeLists.txt index 8cf3f2b0..ffc36707 100644 --- a/src/Detector/CMakeLists.txt +++ b/src/Detector/CMakeLists.txt @@ -50,7 +50,7 @@ endif(USE_OCV_BGFG) include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/../src) -include_directories(${PROJECT_SOURCE_DIR}/../common) +include_directories(${PROJECT_SOURCE_DIR}/..) if (CMAKE_COMPILER_IS_GNUCXX) add_library(${PROJECT_NAME} SHARED @@ -76,8 +76,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBS}) set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${detector_headers}") install(TARGETS ${PROJECT_NAME} EXPORT MTTrackingExports - LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}) + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "libs") diff --git a/src/Detector/OCVDNNDetector.cpp b/src/Detector/OCVDNNDetector.cpp index 4239059c..c6982e5d 100644 --- a/src/Detector/OCVDNNDetector.cpp +++ b/src/Detector/OCVDNNDetector.cpp @@ -1,6 +1,6 @@ #include #include "OCVDNNDetector.h" -#include "nms.h" +#include "mtracking/nms.h" /// /// \brief OCVDNNDetector::OCVDNNDetector diff --git a/src/Detector/ONNXTensorRTDetector.cpp b/src/Detector/ONNXTensorRTDetector.cpp index 582a2e7f..d6b7fddd 100644 --- a/src/Detector/ONNXTensorRTDetector.cpp +++ b/src/Detector/ONNXTensorRTDetector.cpp @@ -1,6 +1,6 @@ #include #include "ONNXTensorRTDetector.h" -#include "nms.h" +#include "mtracking/nms.h" /// /// \brief ONNXTensorRTDetector::ONNXTensorRTDetector diff --git a/src/Detector/tensorrt_onnx/CMakeLists.txt b/src/Detector/tensorrt_onnx/CMakeLists.txt index 1de484a9..257dd3e2 100644 --- a/src/Detector/tensorrt_onnx/CMakeLists.txt +++ b/src/Detector/tensorrt_onnx/CMakeLists.txt @@ -49,7 +49,7 @@ include_directories(${OpenCV_INCLUDE_DIRS}) include_directories(${CUDA_INCLUDE_DIRS}) include_directories(${CUDNN_INCLUDE_DIR}) include_directories(${TensorRT_INCLUDE_DIRS}) -include_directories(${PROJECT_SOURCE_DIR}/../../common) +include_directories(${PROJECT_SOURCE_DIR}/../../mtracking) file(GLOB TENSORRT_SOURCE_FILES *.cpp common/*.cpp) file(GLOB TENSORRT_HEADER_FILES *.h* common/*.h*) @@ -91,9 +91,9 @@ target_link_libraries(${libname_rt} ${TENSORRT_LIBS}) install(TARGETS ${libname_rt} EXPORT MTTrackingExports - ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}) + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "libs") diff --git a/src/Detector/tensorrt_onnx/YoloONNX.cpp b/src/Detector/tensorrt_onnx/YoloONNX.cpp index 4e241d5a..aa4d23a6 100644 --- a/src/Detector/tensorrt_onnx/YoloONNX.cpp +++ b/src/Detector/tensorrt_onnx/YoloONNX.cpp @@ -3,7 +3,7 @@ #define DEFINE_TRT_ENTRYPOINTS 1 #include "YoloONNX.hpp" -#include "../../common/defines.h" +#include "../../mtracking/defines.h" //! //! \brief Creates the network, configures the builder and creates the network engine diff --git a/src/Detector/tensorrt_onnx/YoloONNXv11_instance.hpp b/src/Detector/tensorrt_onnx/YoloONNXv11_instance.hpp index b1a0e11c..f65d75b3 100644 --- a/src/Detector/tensorrt_onnx/YoloONNXv11_instance.hpp +++ b/src/Detector/tensorrt_onnx/YoloONNXv11_instance.hpp @@ -1,7 +1,7 @@ #pragma once #include "YoloONNX.hpp" -#include "../../common/defines.h" +#include "../../mtracking/defines.h" /// /// \brief The YOLOv11_instance_onnx class diff --git a/src/Detector/tensorrt_onnx/YoloONNXv7_instance.hpp b/src/Detector/tensorrt_onnx/YoloONNXv7_instance.hpp index 09a38fab..ec73b7d6 100644 --- a/src/Detector/tensorrt_onnx/YoloONNXv7_instance.hpp +++ b/src/Detector/tensorrt_onnx/YoloONNXv7_instance.hpp @@ -1,7 +1,7 @@ #pragma once #include "YoloONNX.hpp" -#include "../../common/defines.h" +#include "../../mtracking/defines.h" /// /// \brief The YOLOv7_instance_onnx class diff --git a/src/Detector/tensorrt_onnx/YoloONNXv8_instance.hpp b/src/Detector/tensorrt_onnx/YoloONNXv8_instance.hpp index 56fe03f1..0bc2e598 100644 --- a/src/Detector/tensorrt_onnx/YoloONNXv8_instance.hpp +++ b/src/Detector/tensorrt_onnx/YoloONNXv8_instance.hpp @@ -1,7 +1,7 @@ #pragma once #include "YoloONNX.hpp" -#include "../../common/defines.h" +#include "../../mtracking/defines.h" /// /// \brief The YOLOv8_instance_onnx class diff --git a/src/Tracker/BaseTracker.cpp b/src/Tracker/BaseTracker.cpp index db6a9573..7fbae980 100644 --- a/src/Tracker/BaseTracker.cpp +++ b/src/Tracker/BaseTracker.cpp @@ -149,8 +149,8 @@ void CTracker::UpdateTrackingState(const regions_t& regions, std::cout << "CTracker::UpdateTrackingState: m_tracks = " << colsTracks << ", regions = " << rowsRegions << std::endl; int fontType = cv::FONT_HERSHEY_TRIPLEX; - double fontSize = 0.6; - cv::Scalar colorRegionEllow(0, 255, 255); + double fontSize = (currFrame.cols < 1000) ? 0.4 : 0.6; + cv::Scalar colorRegionEllow(100, 100, 100); cv::Scalar colorMatchedAboveThreshRed(0, 0, 255); cv::Scalar colorMatchedGreen(0, 255, 0); cv::Scalar colorMatchedNearMargenta(255, 0, 255); @@ -217,12 +217,7 @@ void CTracker::UpdateTrackingState(const regions_t& regions, CreateDistaceMatrix(regions, regionEmbeddings, costMatrix, maxPossibleCost, maxCost); #if DRAW_DBG_ASSIGNMENT std::cout << "CTracker::UpdateTrackingState: maxPossibleCost = " << maxPossibleCost << ", maxCost = " << maxCost << std::endl; - std::cout << "costMatrix: "; - for (auto costv : costMatrix) - { - std::cout << costv << " "; - } - std::cout << std::endl; + std::cout << "costMatrix: " << cv::Mat_(rowsRegions, colsTracks, costMatrix.data()) << std::endl; #endif // Solving assignment problem (shortest paths) @@ -332,7 +327,9 @@ void CTracker::UpdateTrackingState(const regions_t& regions, m_tracks[i]->IsStaticTimeout(frameTime, m_settings.m_maxStaticTime - m_settings.m_minStaticTime)) { m_removedObjects.push_back(m_tracks[i]->GetID()); - //std::cout << "Remove: " << m_tracks[i]->GetID().ID2Str() << ": lost = " << m_tracks[i]->GetLostPeriod(frameTime) << ", maximumAllowedLostTime = " << m_settings.m_maximumAllowedLostTime << ", out of frame " << m_tracks[i]->IsOutOfTheFrame() << std::endl; +#if DRAW_DBG_ASSIGNMENT + std::cout << "Remove: " << m_tracks[i]->GetID().ID2Str() << ": lost = " << m_tracks[i]->GetLostPeriod(frameTime) << ", maximumAllowedLostTime = " << m_settings.m_maximumAllowedLostTime << ", out of frame " << m_tracks[i]->IsOutOfTheFrame() << std::endl; +#endif m_tracks.erase(m_tracks.begin() + i); assignmentT2R.erase(assignmentT2R.begin() + i); } @@ -349,8 +346,9 @@ void CTracker::UpdateTrackingState(const regions_t& regions, #endif for (size_t i = 0; i < regions.size(); ++i) { - //std::cout << "CTracker::update: regions[" << i << "].m_rrect: " << regions[i].m_rrect.center << ", " << regions[i].m_rrect.angle << ", " << regions[i].m_rrect.size << std::endl; - +#if DRAW_DBG_ASSIGNMENT + std::cout << "CTracker::update: regions[" << i << "].m_rrect: " << regions[i].m_rrect.center << ", " << regions[i].m_rrect.angle << ", " << regions[i].m_rrect.size << std::endl; +#endif if (std::find(assignmentT2R.begin(), assignmentT2R.end(), i) == assignmentT2R.end()) { if (regionEmbeddings.empty()) @@ -391,7 +389,9 @@ void CTracker::UpdateTrackingState(const regions_t& regions, if (assignmentT2R[i] != -1) // If we have assigned detect, then update using its coordinates, { m_tracks[i]->ResetLostTime(frameTime); - // std::cout << "Update track " << i << " for " << assignment[i] << " region, regionEmbeddings.size = " << regionEmbeddings.size() << std::endl; +#if DRAW_DBG_ASSIGNMENT + std::cout << "Update track " << i << " for " << assignmentT2R[i] << " region, regionEmbeddings.size = " << regionEmbeddings.size() << std::endl; +#endif if (regionEmbeddings.empty()) m_tracks[i]->Update(regions[assignmentT2R[i]], true, m_settings.m_maxTraceLength, diff --git a/src/Tracker/CMakeLists.txt b/src/Tracker/CMakeLists.txt index 0141e5e4..fbb355a8 100644 --- a/src/Tracker/CMakeLists.txt +++ b/src/Tracker/CMakeLists.txt @@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.9) project(mtracking) set(main_sources - ../common/nms.h - ../common/defines.h - ../common/object_types.h - ../common/object_types.cpp) + ../mtracking/nms.h + ../mtracking/defines.h + ../mtracking/object_types.h + ../mtracking/object_types.cpp) set(tracker_sources BaseTracker.cpp @@ -76,7 +76,7 @@ endif(USE_OCV_EMBEDDINGS) include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR}/../src) -include_directories(${PROJECT_SOURCE_DIR}/../common) +include_directories(${PROJECT_SOURCE_DIR}/../mtracking) include_directories(${PROJECT_SOURCE_DIR}/../../thirdparty) if (CMAKE_COMPILER_IS_GNUCXX) @@ -98,11 +98,11 @@ endif() target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBS}) -set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${tracker_headers};../common/defines.h;../common/object_types.h") +set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${tracker_headers};../mtracking/defines.h;../mtracking/object_types.h") install(TARGETS ${PROJECT_NAME} EXPORT MTTrackingExports - LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}) + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "libs") diff --git a/src/Tracker/TrackerSettings.h b/src/Tracker/TrackerSettings.h index ba862f5f..8e68b4bf 100644 --- a/src/Tracker/TrackerSettings.h +++ b/src/Tracker/TrackerSettings.h @@ -20,7 +20,7 @@ struct TrackerSettings tracking::KalmanType m_kalmanType = tracking::KalmanLinear; tracking::FilterGoal m_filterGoal = tracking::FilterCenter; tracking::LostTrackType m_lostTrackType = tracking::TrackKCF; // Used if m_filterGoal == tracking::FilterRect - tracking::MatchType m_matchType = tracking::MatchHungrian; + tracking::MatchType m_matchType = tracking::MatchLAPJV; std::array m_distType; diff --git a/src/Tracker/byte_track/BYTETracker.cpp b/src/Tracker/byte_track/BYTETracker.cpp index 57227535..8a63cf56 100644 --- a/src/Tracker/byte_track/BYTETracker.cpp +++ b/src/Tracker/byte_track/BYTETracker.cpp @@ -24,18 +24,37 @@ byte_track::BYTETracker::BYTETracker(const int& frame_rate, /// void byte_track::BYTETracker::GetTracks(std::vector& tracks) const { + tracks.clear(); + if (output_stracks_.size() > tracks.capacity()) + tracks.reserve(output_stracks_.size()); + for (const auto& track : output_stracks_) + { + std::chrono::duration period = m_lastFrameTime - m_lastFrameTime; + cv::RotatedRect rr(track->getRect().tl(), cv::Point2f(static_cast(track->getRect().x + track->getRect().width), static_cast(track->getRect().y)), track->getRect().br()); + TrackingObject to(rr, track->getTrackId(), track->getTrace(), false, cvRound(period.count()), false, + track->getType(), track->getScore(), track->getVelocity()); + + tracks.emplace_back(to); + } } /// void byte_track::BYTETracker::GetRemovedTracks(std::vector& trackIDs) const { - + if (removed_stracks_.size() > trackIDs.capacity()) + trackIDs.reserve(removed_stracks_.size()); + for (const auto& remTrack : removed_stracks_) + { + trackIDs.emplace_back(remTrack->getTrackId()); + } } /// -void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat currFrame, time_point_t frameTime) +void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat /*currFrame*/, time_point_t frameTime) { + m_lastFrameTime = frameTime; + ////////////////// Step 1: Get detections ////////////////// frame_id_++; @@ -45,7 +64,7 @@ void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat currFram for (const auto ®ion : regions) { - const auto strack = std::make_shared(region.m_brect, region.m_confidence, frameTime); + const auto strack = std::make_shared(region.m_brect, region.m_confidence, region.m_type, frameTime); if (region.m_confidence >= track_thresh_) det_stracks.push_back(strack); else @@ -81,7 +100,7 @@ void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat currFram { std::vector> matches_idx; - std::vector unmatch_detection_idx, unmatch_track_idx; + std::vector unmatch_detection_idx, unmatch_track_idx; const auto dists = calcIouDistance(strack_pool, det_stracks); linearAssignment(dists, strack_pool.size(), det_stracks.size(), match_thresh_, @@ -120,7 +139,7 @@ void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat currFram { std::vector> matches_idx; - std::vector unmatch_track_idx, unmatch_detection_idx; + std::vector unmatch_track_idx, unmatch_detection_idx; const auto dists = calcIouDistance(remain_tracked_stracks, det_low_stracks); linearAssignment(dists, remain_tracked_stracks.size(), det_low_stracks.size(), 0.5, @@ -157,8 +176,8 @@ void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat currFram std::vector current_removed_stracks; { - std::vector unmatch_detection_idx; - std::vector unmatch_unconfirmed_idx; + std::vector unmatch_detection_idx; + std::vector unmatch_unconfirmed_idx; std::vector> matches_idx; // Deal with unconfirmed tracks, usually tracks with only one beginning frame @@ -224,7 +243,7 @@ void byte_track::BYTETracker::Update(const regions_t& regions, cv::UMat currFram std::vector byte_track::BYTETracker::jointStracks(const std::vector &a_tlist, const std::vector &b_tlist) const { - std::map exists; + std::map exists; std::vector res; for (size_t i = 0; i < a_tlist.size(); i++) { @@ -233,7 +252,7 @@ std::vector byte_track::BYTETracker::jointSt } for (size_t i = 0; i < b_tlist.size(); i++) { - const int &tid = b_tlist[i]->getTrackId(); + const size_t &tid = b_tlist[i]->getTrackId(); if (!exists[tid] || exists.count(tid) == 0) { exists[tid] = 1; @@ -247,7 +266,7 @@ std::vector byte_track::BYTETracker::jointSt std::vector byte_track::BYTETracker::subStracks(const std::vector &a_tlist, const std::vector &b_tlist) const { - std::map stracks; + std::map stracks; for (size_t i = 0; i < a_tlist.size(); i++) { stracks.emplace(a_tlist[i]->getTrackId(), a_tlist[i]); @@ -255,13 +274,13 @@ std::vector byte_track::BYTETracker::subStra for (size_t i = 0; i < b_tlist.size(); i++) { - const int &tid = b_tlist[i]->getTrackId(); + const size_t&tid = b_tlist[i]->getTrackId(); if (stracks.count(tid) != 0) stracks.erase(tid); } std::vector res; - std::map::iterator it; + std::map::iterator it; for (it = stracks.begin(); it != stracks.end(); ++it) { res.push_back(it->second); @@ -314,27 +333,28 @@ void byte_track::BYTETracker::removeDuplicateStracks(const std::vector> &cost_matrix, - const int &cost_matrix_size, - const int &cost_matrix_size_size, + const size_t &cost_matrix_size, + const size_t &cost_matrix_size_size, const float &thresh, std::vector> &matches, - std::vector &a_unmatched, - std::vector &b_unmatched) const + std::vector &a_unmatched, + std::vector &b_unmatched) const { if (cost_matrix.size() == 0) { - for (int i = 0; i < cost_matrix_size; i++) + for (size_t i = 0; i < cost_matrix_size; i++) { a_unmatched.push_back(i); } - for (int i = 0; i < cost_matrix_size_size; i++) + for (size_t i = 0; i < cost_matrix_size_size; i++) { b_unmatched.push_back(i); } return; } - std::vector rowsol; std::vector colsol; + std::vector rowsol; + std::vector colsol; execLapjv(cost_matrix, rowsol, colsol, true, thresh); for (size_t i = 0; i < rowsol.size(); i++) { @@ -443,12 +463,12 @@ double byte_track::BYTETracker::execLapjv(const std::vector> std::vector > cost_c_extended; - int n_rows = cost.size(); - int n_cols = cost[0].size(); + size_t n_rows = cost.size(); + size_t n_cols = cost[0].size(); rowsol.resize(n_rows); colsol.resize(n_cols); - int n = 0; + size_t n = 0; if (n_rows == n_cols) { n = n_rows; @@ -472,7 +492,7 @@ double byte_track::BYTETracker::execLapjv(const std::vector> { for (size_t j = 0; j < cost_c_extended[i].size(); j++) { - cost_c_extended[i][j] = cost_limit / 2.0; + cost_c_extended[i][j] = cost_limit / 2.0f; } } } @@ -503,9 +523,9 @@ double byte_track::BYTETracker::execLapjv(const std::vector> cost_c_extended[i][j] = 0; } } - for (int i = 0; i < n_rows; i++) + for (size_t i = 0; i < n_rows; i++) { - for (int j = 0; j < n_cols; j++) + for (size_t j = 0; j < n_cols; j++) { cost_c_extended[i][j] = cost_c[i][j]; } @@ -526,18 +546,18 @@ double byte_track::BYTETracker::execLapjv(const std::vector> if (n != n_rows) { - for (int i = 0; i < n; i++) + for (size_t i = 0; i < n; i++) { if (x_c[i] >= n_cols) x_c[i] = -1; if (y_c[i] >= n_rows) y_c[i] = -1; } - for (int i = 0; i < n_rows; i++) + for (size_t i = 0; i < n_rows; i++) { rowsol[i] = x_c[i]; } - for (int i = 0; i < n_cols; i++) + for (size_t i = 0; i < n_cols; i++) { colsol[i] = y_c[i]; } diff --git a/src/Tracker/byte_track/BYTETracker.h b/src/Tracker/byte_track/BYTETracker.h index c2b16b93..95146a7b 100644 --- a/src/Tracker/byte_track/BYTETracker.h +++ b/src/Tracker/byte_track/BYTETracker.h @@ -37,12 +37,12 @@ class BYTETracker final : public BaseTracker std::vector &b_res) const; void linearAssignment(const std::vector> &cost_matrix, - const int &cost_matrix_size, - const int &cost_matrix_size_size, + const size_t &cost_matrix_size, + const size_t &cost_matrix_size_size, const float &thresh, std::vector> &matches, - std::vector &b_unmatched, - std::vector &a_unmatched) const; + std::vector &b_unmatched, + std::vector &a_unmatched) const; std::vector> calcIouDistance(const std::vector &a_tracks, const std::vector &b_tracks) const; @@ -63,6 +63,7 @@ class BYTETracker final : public BaseTracker const float match_thresh_ = 0.8f; const size_t max_time_lost_ = 30; + time_point_t m_lastFrameTime; size_t frame_id_ = 0; size_t track_id_count_ = 0; diff --git a/src/Tracker/byte_track/STrack.cpp b/src/Tracker/byte_track/STrack.cpp index decaaa25..728b4e6e 100644 --- a/src/Tracker/byte_track/STrack.cpp +++ b/src/Tracker/byte_track/STrack.cpp @@ -2,10 +2,11 @@ #include -byte_track::STrack::STrack(const cv::Rect2f& rect, const float& score, time_point_t currTime) : +byte_track::STrack::STrack(const cv::Rect2f& rect, const float& score, objtype_t type, time_point_t currTime) : kalman_filter_(), mean_(), covariance_(), + type_(type), rect_(rect), state_(STrackState::New), is_activated_(false), @@ -58,6 +59,21 @@ const size_t& byte_track::STrack::getTrackletLength() const return tracklet_len_; } +objtype_t byte_track::STrack::getType() const +{ + return type_; +} + +const Trace& byte_track::STrack::getTrace() const +{ + return trace_; +} + +cv::Vec byte_track::STrack::getVelocity() const +{ + return cv::Vec(mean_(4), mean_(5)); +} + byte_track::KalmanFilter::DetectBox GetXyah(const cv::Rect2f& rect) { return byte_track::KalmanFilter::DetectBox( diff --git a/src/Tracker/byte_track/STrack.h b/src/Tracker/byte_track/STrack.h index ae2d6503..65607f78 100644 --- a/src/Tracker/byte_track/STrack.h +++ b/src/Tracker/byte_track/STrack.h @@ -18,7 +18,7 @@ enum class STrackState { class STrack { public: - STrack(const cv::Rect2f& rect, const float& score, time_point_t currTime); + STrack(const cv::Rect2f& rect, const float& score, objtype_t type, time_point_t currTime); ~STrack() = default; const cv::Rect2f& getRect() const; @@ -30,6 +30,9 @@ class STrack const size_t& getFrameId() const; const size_t& getStartFrameId() const; const size_t& getTrackletLength() const; + objtype_t getType() const; + const Trace& getTrace() const; + cv::Vec getVelocity() const; void activate(const size_t& frame_id, const size_t& track_id, time_point_t currTime); void reActivate(const STrack &new_track, const size_t &frame_id, const int &new_track_id, time_point_t currTime); // new_track_id = -1 @@ -45,6 +48,7 @@ class STrack KalmanFilter::StateMean mean_; KalmanFilter::StateCov covariance_; + objtype_t type_ = bad_type; cv::Rect2f rect_; STrackState state_{ STrackState::New }; diff --git a/src/common/defines.h b/src/mtracking/defines.h similarity index 95% rename from src/common/defines.h rename to src/mtracking/defines.h index e67fd142..245fc0a2 100644 --- a/src/common/defines.h +++ b/src/mtracking/defines.h @@ -1,461 +1,461 @@ -#pragma once - -#include -#include -#include - -#ifdef HAVE_FILESYSTEM -#include -namespace fs = std::filesystem; -#else -#include -namespace fs = std::experimental::filesystem; -#endif - -#include -#include "object_types.h" - -// --------------------------------------------------------------------------- -// -// --------------------------------------------------------------------------- -typedef float track_t; -typedef cv::Point_ Point_t; -#define El_t CV_32F -#define Mat_t CV_32FC - -typedef std::vector assignments_t; -typedef std::vector distMatrix_t; - -typedef std::chrono::time_point time_point_t; - -/// -template -class TrackID -{ -public: - typedef T value_type; - - TrackID() = default; - TrackID(value_type val) - : m_val(val) - { - } - - bool operator==(const TrackID& id) const - { - return m_val == id.m_val; - } - - std::string ID2Str() const - { - return std::to_string(m_val); - } - static TrackID Str2ID(const std::string& id) - { - return TrackID(std::stoi(id)); - } - TrackID NextID() const - { - return TrackID(m_val + 1); - } - size_t ID2Module(size_t module) const - { - return m_val % module; - } - - value_type m_val{ 0 }; -}; - -typedef TrackID track_id_t; -namespace std -{ - template <> - struct hash - { - std::size_t operator()(const track_id_t& k) const - { - return std::hash()(k.m_val); - } - }; - -} - -/// -/// \brief config_t -/// -typedef std::multimap config_t; - -/// -/// \brief The CRegion class -/// -class CRegion -{ -public: - /// - CRegion() = default; - - /// - CRegion(const cv::Rect& rect) noexcept - : m_brect(rect) - { - B2RRect(); - } - - /// - CRegion(const cv::RotatedRect& rrect) noexcept - : m_rrect(rrect) - { - if (m_rrect.size.width < 1) - m_rrect.size.width = 1; - if (m_rrect.size.height < 1) - m_rrect.size.height = 1; - R2BRect(); - } - - /// - CRegion(const cv::RotatedRect& rrect, objtype_t type, float confidence) noexcept - : m_type(type), m_rrect(rrect), m_confidence(confidence) - { - if (m_rrect.size.width < 1) - m_rrect.size.width = 1; - if (m_rrect.size.height < 1) - m_rrect.size.height = 1; - R2BRect(); - } - - /// - CRegion(const cv::RotatedRect& rrect, const cv::Rect& brect, objtype_t type, float confidence, const cv::Mat& boxMask) noexcept - : m_type(type), m_rrect(rrect), m_brect(brect), m_confidence(confidence) - { - m_boxMask = boxMask; - - if (m_rrect.size.width < 1) - m_rrect.size.width = 1; - if (m_rrect.size.height < 1) - m_rrect.size.height = 1; - - if (!m_boxMask.empty() && m_boxMask.size() != m_brect.size()) - { - m_brect.width = m_boxMask.cols; - m_brect.height = m_boxMask.rows; - } - } - - /// - CRegion(const cv::Rect& brect, objtype_t type, float confidence) noexcept - : m_type(type), m_brect(brect), m_confidence(confidence) - { - B2RRect(); - } - - objtype_t m_type = bad_type; - cv::RotatedRect m_rrect; - cv::Rect m_brect; - track_t m_confidence = -1; - cv::Mat m_boxMask; - -private: - /// - /// \brief R2BRect - /// \return - /// - cv::Rect R2BRect() noexcept - { - m_brect = m_rrect.boundingRect(); - return m_brect; - } - /// - /// \brief B2RRect - /// \return - /// - cv::RotatedRect B2RRect() noexcept - { - m_rrect = cv::RotatedRect(m_brect.tl(), cv::Point2f(static_cast(m_brect.x + m_brect.width), static_cast(m_brect.y)), m_brect.br()); - if (m_rrect.size.width < 1) - m_rrect.size.width = 1; - if (m_rrect.size.height < 1) - m_rrect.size.height = 1; - return m_rrect; - } -}; - -typedef std::vector regions_t; - -/// -/// \brief sqr -/// \param val -/// \return -/// -template inline -T sqr(T val) -{ - return val * val; -} - -/// -/// \brief get_lin_regress_params -/// \param in_data -/// \param start_pos -/// \param in_data_size -/// \param kx -/// \param bx -/// \param ky -/// \param by -/// -template -void get_lin_regress_params( - const CONT& in_data, - size_t start_pos, - size_t in_data_size, - T& kx, T& bx, T& ky, T& by) -{ - T m1(0.), m2(0.); - T m3_x(0.), m4_x(0.); - T m3_y(0.), m4_y(0.); - - const T el_count = static_cast(in_data_size - start_pos); - for (size_t i = start_pos; i < in_data_size; ++i) - { - m1 += i; - m2 += sqr(i); - - m3_x += in_data[i].x; - m4_x += i * in_data[i].x; - - m3_y += in_data[i].y; - m4_y += i * in_data[i].y; - } - T det_1 = 1 / (el_count * m2 - sqr(m1)); - - m1 *= -1; - - kx = det_1 * (m1 * m3_x + el_count * m4_x); - bx = det_1 * (m2 * m3_x + m1 * m4_x); - - ky = det_1 * (m1 * m3_y + el_count * m4_y); - by = det_1 * (m2 * m3_y + m1 * m4_y); -} - -/// -/// \brief sqr: Euclid distance between two points -/// \param val -/// \return -/// -template inline -T distance(const POINT_TYPE& p1, const POINT_TYPE& p2) -{ - return sqrt((T)(sqr(p2.x - p1.x) + sqr(p2.y - p1.y))); -} - -/// -/// \brief Clamp: Fit rectangle to frame -/// \param rect -/// \param size -/// \return -/// -inline cv::Rect Clamp(cv::Rect rect, const cv::Size& size) -{ - if (rect.x < 0) - { - rect.width = std::min(rect.width, size.width - 1); - rect.x = 0; - } - else if (rect.x + rect.width >= size.width) - { - rect.x = std::max(0, size.width - rect.width - 1); - rect.width = std::min(rect.width, size.width - 1); - } - if (rect.y < 0) - { - rect.height = std::min(rect.height, size.height - 1); - rect.y = 0; - } - else if (rect.y + rect.height >= size.height) - { - rect.y = std::max(0, size.height - rect.height - 1); - rect.height = std::min(rect.height, size.height - 1); - } - return rect; -} - -/// -/// \brief SaveMat -/// \param m -/// \param name -/// \param path -/// -inline bool SaveMat(const cv::Mat& m, std::string prefix, const std::string& ext, const std::string& savePath, bool compressToImage) -{ - bool res = true; - - std::map depthDict; - depthDict.emplace(CV_8U, "uint8"); - depthDict.emplace(CV_8S, "int8"); - depthDict.emplace(CV_16U, "uint16"); - depthDict.emplace(CV_16S, "int16"); - depthDict.emplace(CV_32S, "int32"); - depthDict.emplace(CV_32F, "float32"); - depthDict.emplace(CV_64F, "float64"); - depthDict.emplace(CV_16F, "float16"); - - auto depth = depthDict.find(m.depth()); - if (depth == std::end(depthDict)) - { - std::cout << "File " << prefix << " has a unknown depth: " << m.depth() << std::endl; - res = false; - return res; - } - assert(depth != std::end(depthDict)); - - fs::path fullPath(savePath); - fullPath.append(prefix + "_" + std::to_string(m.cols) + "x" + std::to_string(m.rows) + "_" + depth->second + "_C" + std::to_string(m.channels()) + ext); - prefix = fullPath.generic_string(); - - if (compressToImage) - { - res = cv::imwrite(prefix, m); - } - else - { - FILE* f = 0; -#ifdef _WIN32 - fopen_s(&f, prefix.c_str(), "wb"); -#else - f = fopen(prefix.c_str(), "wb"); -#endif // _WIN32 - res = f != 0; - if (res) - { - for (int y = 0; y < m.rows; ++y) - { - fwrite(m.ptr(y), 1, m.cols * m.elemSize(), f); - } - fclose(f); - std::cout << "File " << prefix << " was writed" << std::endl; - } - } - if (res) - std::cout << "File " << prefix << " was writed" << std::endl; - else - std::cout << "File " << prefix << " can not be opened!" << std::endl; - return res; -} - -/// -/// \brief DrawFilledRect -/// -inline void DrawFilledRect(cv::Mat& frame, const cv::Rect& rect, cv::Scalar cl, int alpha) -{ - if (alpha) - { - const int alpha_1 = 255 - alpha; - const int nchans = frame.channels(); - int color[3] = { cv::saturate_cast(cl[0]), cv::saturate_cast(cl[1]), cv::saturate_cast(cl[2]) }; - for (int y = std::max(0, rect.y); y < std::min(rect.y + rect.height, frame.rows - 1); ++y) - { - uchar* ptr = frame.ptr(y) + nchans * rect.x; - for (int x = std::max(0, rect.x); x < std::min(rect.x + rect.width, frame.cols - 1); ++x) - { - for (int i = 0; i < nchans; ++i) - { - ptr[i] = cv::saturate_cast((alpha_1 * ptr[i] + alpha * color[i]) / 255); - } - ptr += nchans; - } - } - } - else - { - cv::rectangle(frame, rect, cl, cv::FILLED); - } -} - -/// -/// -/// -namespace tracking -{ -/// -/// \brief The Detectors enum -/// -enum Detectors -{ - Motion_VIBE = 0, - Motion_MOG = 1, - Motion_GMG = 2, - Motion_CNT = 3, - Motion_MOG2 = 4, - ONNX_TensorRT = 5, - DNN_OCV = 6, - DetectorsCount -}; - -/// -/// \brief The TrackerTemplate enum -/// -enum TrackerTemplate -{ - UniversalTracker = 0, - ByteTrack = 1 -}; - -/// -/// \brief The DistType enum -/// -enum DistType -{ - DistCenters, // Euclidean distance between centers, [0, 1] - DistRects, // Euclidean distance between bounding rectangles, [0, 1] - DistJaccard, // Intersection over Union, IoU, [0, 1] - DistHist, // Bhatacharia distance between histograms, [0, 1] - DistFeatureCos, // Cosine distance between embeddings, [0, 1] - DistMahalanobis, // Mahalanobis: https://ww2.mathworks.cn/help/vision/ug/motion-based-multiple-object-tracking.html - DistsCount -}; - -/// -/// \brief The FilterGoal enum -/// -enum FilterGoal -{ - FilterCenter, - FilterRect, - FilterRRect, - FiltersCount -}; - -/// -/// \brief The KalmanType enum -/// -enum KalmanType -{ - KalmanLinear, - KalmanUnscented, - KalmanAugmentedUnscented, - KalmanCount -}; - -/// -/// \brief The MatchType enum -/// -enum MatchType -{ - MatchHungrian, - MatchLAPJV, - MatchCount -}; - -/// -/// \brief The LostTrackType enum -/// -enum LostTrackType -{ - TrackNone, - TrackKCF, - TrackCSRT, - TrackDaSiamRPN, - TrackNano, - TrackVit, - SingleTracksCount -}; -} +#pragma once + +#include +#include +#include + +#ifdef HAVE_EXPERIMENTAL_FILESYSTEM +#include +namespace fs = std::experimental::filesystem; +#else +#include +namespace fs = std::filesystem; +#endif + +#include +#include "object_types.h" + +// --------------------------------------------------------------------------- +// +// --------------------------------------------------------------------------- +typedef float track_t; +typedef cv::Point_ Point_t; +#define El_t CV_32F +#define Mat_t CV_32FC + +typedef std::vector assignments_t; +typedef std::vector distMatrix_t; + +typedef std::chrono::time_point time_point_t; + +/// +template +class TrackID +{ +public: + typedef T value_type; + + TrackID() = default; + TrackID(value_type val) + : m_val(val) + { + } + + bool operator==(const TrackID& id) const + { + return m_val == id.m_val; + } + + std::string ID2Str() const + { + return std::to_string(m_val); + } + static TrackID Str2ID(const std::string& id) + { + return TrackID(std::stoi(id)); + } + TrackID NextID() const + { + return TrackID(m_val + 1); + } + size_t ID2Module(size_t module) const + { + return m_val % module; + } + + value_type m_val{ 0 }; +}; + +typedef TrackID track_id_t; +namespace std +{ + template <> + struct hash + { + std::size_t operator()(const track_id_t& k) const + { + return std::hash()(k.m_val); + } + }; + +} + +/// +/// \brief config_t +/// +typedef std::multimap config_t; + +/// +/// \brief The CRegion class +/// +class CRegion +{ +public: + /// + CRegion() = default; + + /// + CRegion(const cv::Rect& rect) noexcept + : m_brect(rect) + { + B2RRect(); + } + + /// + CRegion(const cv::RotatedRect& rrect) noexcept + : m_rrect(rrect) + { + if (m_rrect.size.width < 1) + m_rrect.size.width = 1; + if (m_rrect.size.height < 1) + m_rrect.size.height = 1; + R2BRect(); + } + + /// + CRegion(const cv::RotatedRect& rrect, objtype_t type, float confidence) noexcept + : m_type(type), m_rrect(rrect), m_confidence(confidence) + { + if (m_rrect.size.width < 1) + m_rrect.size.width = 1; + if (m_rrect.size.height < 1) + m_rrect.size.height = 1; + R2BRect(); + } + + /// + CRegion(const cv::RotatedRect& rrect, const cv::Rect& brect, objtype_t type, float confidence, const cv::Mat& boxMask) noexcept + : m_type(type), m_rrect(rrect), m_brect(brect), m_confidence(confidence) + { + m_boxMask = boxMask; + + if (m_rrect.size.width < 1) + m_rrect.size.width = 1; + if (m_rrect.size.height < 1) + m_rrect.size.height = 1; + + if (!m_boxMask.empty() && m_boxMask.size() != m_brect.size()) + { + m_brect.width = m_boxMask.cols; + m_brect.height = m_boxMask.rows; + } + } + + /// + CRegion(const cv::Rect& brect, objtype_t type, float confidence) noexcept + : m_type(type), m_brect(brect), m_confidence(confidence) + { + B2RRect(); + } + + objtype_t m_type = bad_type; + cv::RotatedRect m_rrect; + cv::Rect m_brect; + track_t m_confidence = -1; + cv::Mat m_boxMask; + +private: + /// + /// \brief R2BRect + /// \return + /// + cv::Rect R2BRect() noexcept + { + m_brect = m_rrect.boundingRect(); + return m_brect; + } + /// + /// \brief B2RRect + /// \return + /// + cv::RotatedRect B2RRect() noexcept + { + m_rrect = cv::RotatedRect(m_brect.tl(), cv::Point2f(static_cast(m_brect.x + m_brect.width), static_cast(m_brect.y)), m_brect.br()); + if (m_rrect.size.width < 1) + m_rrect.size.width = 1; + if (m_rrect.size.height < 1) + m_rrect.size.height = 1; + return m_rrect; + } +}; + +typedef std::vector regions_t; + +/// +/// \brief sqr +/// \param val +/// \return +/// +template inline +T sqr(T val) +{ + return val * val; +} + +/// +/// \brief get_lin_regress_params +/// \param in_data +/// \param start_pos +/// \param in_data_size +/// \param kx +/// \param bx +/// \param ky +/// \param by +/// +template +void get_lin_regress_params( + const CONT& in_data, + size_t start_pos, + size_t in_data_size, + T& kx, T& bx, T& ky, T& by) +{ + T m1(0.), m2(0.); + T m3_x(0.), m4_x(0.); + T m3_y(0.), m4_y(0.); + + const T el_count = static_cast(in_data_size - start_pos); + for (size_t i = start_pos; i < in_data_size; ++i) + { + m1 += i; + m2 += sqr(i); + + m3_x += in_data[i].x; + m4_x += i * in_data[i].x; + + m3_y += in_data[i].y; + m4_y += i * in_data[i].y; + } + T det_1 = 1 / (el_count * m2 - sqr(m1)); + + m1 *= -1; + + kx = det_1 * (m1 * m3_x + el_count * m4_x); + bx = det_1 * (m2 * m3_x + m1 * m4_x); + + ky = det_1 * (m1 * m3_y + el_count * m4_y); + by = det_1 * (m2 * m3_y + m1 * m4_y); +} + +/// +/// \brief sqr: Euclid distance between two points +/// \param val +/// \return +/// +template inline +T distance(const POINT_TYPE& p1, const POINT_TYPE& p2) +{ + return sqrt((T)(sqr(p2.x - p1.x) + sqr(p2.y - p1.y))); +} + +/// +/// \brief Clamp: Fit rectangle to frame +/// \param rect +/// \param size +/// \return +/// +inline cv::Rect Clamp(cv::Rect rect, const cv::Size& size) +{ + if (rect.x < 0) + { + rect.width = std::min(rect.width, size.width - 1); + rect.x = 0; + } + else if (rect.x + rect.width >= size.width) + { + rect.x = std::max(0, size.width - rect.width - 1); + rect.width = std::min(rect.width, size.width - 1); + } + if (rect.y < 0) + { + rect.height = std::min(rect.height, size.height - 1); + rect.y = 0; + } + else if (rect.y + rect.height >= size.height) + { + rect.y = std::max(0, size.height - rect.height - 1); + rect.height = std::min(rect.height, size.height - 1); + } + return rect; +} + +/// +/// \brief SaveMat +/// \param m +/// \param name +/// \param path +/// +inline bool SaveMat(const cv::Mat& m, std::string prefix, const std::string& ext, const std::string& savePath, bool compressToImage) +{ + bool res = true; + + std::map depthDict; + depthDict.emplace(CV_8U, "uint8"); + depthDict.emplace(CV_8S, "int8"); + depthDict.emplace(CV_16U, "uint16"); + depthDict.emplace(CV_16S, "int16"); + depthDict.emplace(CV_32S, "int32"); + depthDict.emplace(CV_32F, "float32"); + depthDict.emplace(CV_64F, "float64"); + depthDict.emplace(CV_16F, "float16"); + + auto depth = depthDict.find(m.depth()); + if (depth == std::end(depthDict)) + { + std::cout << "File " << prefix << " has a unknown depth: " << m.depth() << std::endl; + res = false; + return res; + } + assert(depth != std::end(depthDict)); + + fs::path fullPath(savePath); + fullPath.append(prefix + "_" + std::to_string(m.cols) + "x" + std::to_string(m.rows) + "_" + depth->second + "_C" + std::to_string(m.channels()) + ext); + prefix = fullPath.generic_string(); + + if (compressToImage) + { + res = cv::imwrite(prefix, m); + } + else + { + FILE* f = 0; +#ifdef _WIN32 + fopen_s(&f, prefix.c_str(), "wb"); +#else + f = fopen(prefix.c_str(), "wb"); +#endif // _WIN32 + res = f != 0; + if (res) + { + for (int y = 0; y < m.rows; ++y) + { + fwrite(m.ptr(y), 1, m.cols * m.elemSize(), f); + } + fclose(f); + std::cout << "File " << prefix << " was writed" << std::endl; + } + } + if (res) + std::cout << "File " << prefix << " was writed" << std::endl; + else + std::cout << "File " << prefix << " can not be opened!" << std::endl; + return res; +} + +/// +/// \brief DrawFilledRect +/// +inline void DrawFilledRect(cv::Mat& frame, const cv::Rect& rect, cv::Scalar cl, int alpha) +{ + if (alpha) + { + const int alpha_1 = 255 - alpha; + const int nchans = frame.channels(); + int color[3] = { cv::saturate_cast(cl[0]), cv::saturate_cast(cl[1]), cv::saturate_cast(cl[2]) }; + for (int y = std::max(0, rect.y); y < std::min(rect.y + rect.height, frame.rows - 1); ++y) + { + uchar* ptr = frame.ptr(y) + nchans * rect.x; + for (int x = std::max(0, rect.x); x < std::min(rect.x + rect.width, frame.cols - 1); ++x) + { + for (int i = 0; i < nchans; ++i) + { + ptr[i] = cv::saturate_cast((alpha_1 * ptr[i] + alpha * color[i]) / 255); + } + ptr += nchans; + } + } + } + else + { + cv::rectangle(frame, rect, cl, cv::FILLED); + } +} + +/// +/// +/// +namespace tracking +{ +/// +/// \brief The Detectors enum +/// +enum Detectors +{ + Motion_VIBE = 0, + Motion_MOG = 1, + Motion_GMG = 2, + Motion_CNT = 3, + Motion_MOG2 = 4, + ONNX_TensorRT = 5, + DNN_OCV = 6, + DetectorsCount +}; + +/// +/// \brief The TrackerTemplate enum +/// +enum TrackerTemplate +{ + UniversalTracker = 0, + ByteTrack = 1 +}; + +/// +/// \brief The DistType enum +/// +enum DistType +{ + DistCenters, // Euclidean distance between centers, [0, 1] + DistRects, // Euclidean distance between bounding rectangles, [0, 1] + DistJaccard, // Intersection over Union, IoU, [0, 1] + DistHist, // Bhatacharia distance between histograms, [0, 1] + DistFeatureCos, // Cosine distance between embeddings, [0, 1] + DistMahalanobis, // Mahalanobis: https://ww2.mathworks.cn/help/vision/ug/motion-based-multiple-object-tracking.html + DistsCount +}; + +/// +/// \brief The FilterGoal enum +/// +enum FilterGoal +{ + FilterCenter, + FilterRect, + FilterRRect, + FiltersCount +}; + +/// +/// \brief The KalmanType enum +/// +enum KalmanType +{ + KalmanLinear, + KalmanUnscented, + KalmanAugmentedUnscented, + KalmanCount +}; + +/// +/// \brief The MatchType enum +/// +enum MatchType +{ + MatchHungrian, + MatchLAPJV, + MatchCount +}; + +/// +/// \brief The LostTrackType enum +/// +enum LostTrackType +{ + TrackNone, + TrackKCF, + TrackCSRT, + TrackDaSiamRPN, + TrackNano, + TrackVit, + SingleTracksCount +}; +} diff --git a/src/common/nms.h b/src/mtracking/nms.h similarity index 100% rename from src/common/nms.h rename to src/mtracking/nms.h diff --git a/src/common/object_types.cpp b/src/mtracking/object_types.cpp similarity index 93% rename from src/common/object_types.cpp rename to src/mtracking/object_types.cpp index 0e2270c0..8484723c 100644 --- a/src/common/object_types.cpp +++ b/src/mtracking/object_types.cpp @@ -1,89 +1,89 @@ -#include "object_types.h" - -std::vector TypeConverter::m_typeNames = -{ - "person", - "bicycle", - "car", - "motorbike", - "aeroplane", - "bus", - "train", - "truck", - "boat", - "traffic_light", - "fire_hydrant", - "stop_sign", - "parking_meter", - "bench", - "bird", - "cat", - "dog", - "horse", - "sheep", - "cow", - "elephant", - "bear", - "zebra", - "giraffe", - "backpack", - "umbrella", - "handbag", - "tie", - "suitcase", - "frisbee", - "skis", - "snowboard", - "sports_ball", - "kite", - "baseball_bat", - "baseball_glove", - "skateboard", - "surfboard", - "tennis_racket", - "bottle", - "wine_glass", - "cup", - "fork", - "knife", - "spoon", - "bowl", - "banana", - "apple", - "sandwich", - "orange", - "broccoli", - "carrot", - "hot_dog", - "pizza", - "donut", - "cake", - "chair", - "sofa", - "pottedplant", - "bed", - "diningtable", - "toilet", - "tvmonitor", - "laptop", - "mouse", - "remote", - "keyboard", - "cell_phone", - "microwave", - "oven", - "toaster", - "sink", - "refrigerator", - "book", - "clock", - "vase", - "scissors", - "teddy_bear", - "hair_drier", - "toothbrush", - "vehicle", - "face" -}; - -std::string TypeConverter::m_badTypeName = "unknown"; +#include "object_types.h" + +std::vector TypeConverter::m_typeNames = +{ + "person", + "bicycle", + "car", + "motorbike", + "aeroplane", + "bus", + "train", + "truck", + "boat", + "traffic_light", + "fire_hydrant", + "stop_sign", + "parking_meter", + "bench", + "bird", + "cat", + "dog", + "horse", + "sheep", + "cow", + "elephant", + "bear", + "zebra", + "giraffe", + "backpack", + "umbrella", + "handbag", + "tie", + "suitcase", + "frisbee", + "skis", + "snowboard", + "sports_ball", + "kite", + "baseball_bat", + "baseball_glove", + "skateboard", + "surfboard", + "tennis_racket", + "bottle", + "wine_glass", + "cup", + "fork", + "knife", + "spoon", + "bowl", + "banana", + "apple", + "sandwich", + "orange", + "broccoli", + "carrot", + "hot_dog", + "pizza", + "donut", + "cake", + "chair", + "sofa", + "pottedplant", + "bed", + "diningtable", + "toilet", + "tvmonitor", + "laptop", + "mouse", + "remote", + "keyboard", + "cell_phone", + "microwave", + "oven", + "toaster", + "sink", + "refrigerator", + "book", + "clock", + "vase", + "scissors", + "teddy_bear", + "hair_drier", + "toothbrush", + "vehicle", + "face" +}; + +std::string TypeConverter::m_badTypeName = "unknown"; diff --git a/src/common/object_types.h b/src/mtracking/object_types.h similarity index 91% rename from src/common/object_types.h rename to src/mtracking/object_types.h index 1ecf7977..25f78a5e 100644 --- a/src/common/object_types.h +++ b/src/mtracking/object_types.h @@ -1,58 +1,58 @@ -#pragma once -#include -#include -#include - -typedef int objtype_t; -constexpr objtype_t bad_type = -1; - -/// -class TypeConverter -{ -public: - /// - static std::string Type2Str(objtype_t type) - { - return (type == bad_type) ? m_badTypeName : m_typeNames[type]; - } - - /// - static objtype_t Str2Type(const std::string& typeName) - { - for (size_t i = 0; i < m_typeNames.size(); ++i) - { - if (typeName == m_typeNames[i]) - { - //std::cout << "Str2Type: " << typeName << " exist: " << i << std::endl; - return static_cast(i); - } - } - m_typeNames.emplace_back(typeName); - //std::cout << "Str2Type: " << typeName << " new: " << (m_typeNames.size()) - 1 << std::endl; - return static_cast(m_typeNames.size()) - 1; - } - - static bool AddNewType(const std::string& typeName) - { - for (size_t i = 0; i < m_typeNames.size(); ++i) - { - if (typeName == m_typeNames[i]) - { - //std::cout << "AddNewType: " << typeName << ": false" << std::endl; - return false; - } - } - m_typeNames.emplace_back(typeName); - //std::cout << "AddNewType: " << typeName << ": " << (m_typeNames.size() - 1) << std::endl; - return true; - } - - static size_t TypesCount() - { - return m_typeNames.size(); - } - -private: - static std::vector m_typeNames; - static std::string m_badTypeName; -}; +#pragma once +#include +#include +#include + +typedef int objtype_t; +constexpr objtype_t bad_type = -1; + +/// +class TypeConverter +{ +public: + /// + static std::string Type2Str(objtype_t type) + { + return (type == bad_type) ? m_badTypeName : m_typeNames[(size_t)type]; + } + + /// + static objtype_t Str2Type(const std::string& typeName) + { + for (size_t i = 0; i < m_typeNames.size(); ++i) + { + if (typeName == m_typeNames[i]) + { + //std::cout << "Str2Type: " << typeName << " exist: " << i << std::endl; + return static_cast(i); + } + } + m_typeNames.emplace_back(typeName); + //std::cout << "Str2Type: " << typeName << " new: " << (m_typeNames.size()) - 1 << std::endl; + return static_cast(m_typeNames.size()) - 1; + } + + static bool AddNewType(const std::string& typeName) + { + for (size_t i = 0; i < m_typeNames.size(); ++i) + { + if (typeName == m_typeNames[i]) + { + //std::cout << "AddNewType: " << typeName << ": false" << std::endl; + return false; + } + } + m_typeNames.emplace_back(typeName); + //std::cout << "AddNewType: " << typeName << ": " << (m_typeNames.size() - 1) << std::endl; + return true; + } + + static size_t TypesCount() + { + return m_typeNames.size(); + } + +private: + static std::vector m_typeNames; + static std::string m_badTypeName; +}; diff --git a/src/python_bind/mtracker.cpp b/src/python_bind/mtracker.cpp index b95657ca..c9aa731b 100644 --- a/src/python_bind/mtracker.cpp +++ b/src/python_bind/mtracker.cpp @@ -7,7 +7,7 @@ #include -#include "../common/defines.h" +#include "../mtracking/defines.h" #include "../Tracker/Ctracker.h" #include "../Detector/BaseDetector.h" #include "../Detector/MotionDetector.h" diff --git a/thirdparty/inih/CMakeLists.txt b/thirdparty/inih/CMakeLists.txt index bdb1a93b..a4b78575 100644 --- a/thirdparty/inih/CMakeLists.txt +++ b/thirdparty/inih/CMakeLists.txt @@ -12,9 +12,9 @@ set_target_properties(inih PROPERTIES FOLDER "libs") install(TARGETS ${PROJECT_NAME} EXPORT MTTrackingExports - ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}) + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "libs") \ No newline at end of file diff --git a/thirdparty/ruclip/CMakeLists.txt b/thirdparty/ruclip/CMakeLists.txt index ce2657a3..74f99136 100644 --- a/thirdparty/ruclip/CMakeLists.txt +++ b/thirdparty/ruclip/CMakeLists.txt @@ -40,9 +40,9 @@ target_link_libraries(${PROJECT_NAME} ${RUCLIP_LIBS}) install(TARGETS ${PROJECT_NAME} EXPORT MTTrackingExports - ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib - RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}) + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin + PUBLIC_HEADER DESTINATION include/${PROJECT_NAME}) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "libs")