From 52258249b93bd1349be0ff5ebcac3c81330d3601 Mon Sep 17 00:00:00 2001 From: jario-jin Date: Mon, 14 Aug 2023 11:33:02 +0800 Subject: [PATCH] add mot & tracking -> sot --- CMakeLists.txt | 12 ++- algorithm/mot/sv_mot.cpp | 98 +++++++++++++++++++ .../ocv470/sot_ocv470_impl.cpp} | 2 +- .../ocv470/sot_ocv470_impl.h} | 0 .../sv_tracking.cpp => sot/sv_sot.cpp} | 4 +- include/sv_mot.h | 42 ++++++++ include/{sv_tracking.h => sv_sot.h} | 0 include/sv_world.h | 3 +- params/a-params/sv_algorithm_params.json | 10 ++ .../sv_algorithm_params_1280_wo_mask.json | 10 ++ .../sv_algorithm_params_640_w_mask.json | 10 ++ .../sv_algorithm_params_640_wo_mask.json | 10 ++ .../sv_algorithm_params_coco_1280.json | 10 ++ .../sv_algorithm_params_coco_640.json | 10 ++ params/a-params/sv_algorithm_params_csrt.json | 10 ++ params/a-params/sv_algorithm_params_kcf.json | 10 ++ params/a-params/sv_algorithm_params_nano.json | 10 ++ .../a-params/sv_algorithm_params_siamrpn.json | 10 ++ samples/demo/multiple_object_tracking.cpp | 41 ++++++++ 19 files changed, 294 insertions(+), 8 deletions(-) create mode 100644 algorithm/mot/sv_mot.cpp rename algorithm/{tracking/ocv470/tracking_ocv470_impl.cpp => sot/ocv470/sot_ocv470_impl.cpp} (99%) rename algorithm/{tracking/ocv470/tracking_ocv470_impl.h => sot/ocv470/sot_ocv470_impl.h} (100%) rename algorithm/{tracking/sv_tracking.cpp => sot/sv_sot.cpp} (92%) create mode 100644 include/sv_mot.h rename include/{sv_tracking.h => sv_sot.h} (100%) create mode 100644 samples/demo/multiple_object_tracking.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e35dffe..e640190 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,7 +71,7 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/gimbal_ctrl ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/common_det/cuda ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/landing_det/cuda - ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/tracking/ocv470 + ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/sot/ocv470 ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/color_line ${CMAKE_CURRENT_SOURCE_DIR}/video_io ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/ellipse_det @@ -111,7 +111,8 @@ set( include/sv_algorithm_base.h include/sv_common_det.h include/sv_landing_det.h - include/sv_tracking.h + include/sv_sot.h + include/sv_mot.h include/sv_color_line.h include/sv_video_input.h include/sv_video_output.h @@ -152,11 +153,12 @@ set(spirecv_SRCS algorithm/ellipse_det/ellipse_detector.cpp algorithm/common_det/sv_common_det.cpp algorithm/landing_det/sv_landing_det.cpp - algorithm/tracking/sv_tracking.cpp + algorithm/sot/sv_sot.cpp + algorithm/mot/sv_mot.cpp algorithm/color_line/sv_color_line.cpp ) -file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/tracking/ocv470/*.cpp) +file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/sot/ocv470/*.cpp) list(APPEND spirecv_SRCS ${ALG_SRC_FILES}) file(GLOB ALG_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/algorithm/color_line/*.cpp) list(APPEND spirecv_SRCS ${ALG_SRC_FILES}) @@ -249,6 +251,8 @@ add_executable(LandingMarkerDetection samples/demo/landing_marker_detection.cpp) target_link_libraries(LandingMarkerDetection sv_world) add_executable(SingleObjectTracking samples/demo/single_object_tracking.cpp) target_link_libraries(SingleObjectTracking sv_world) +add_executable(MultipleObjectTracking samples/demo/multiple_object_tracking.cpp) +target_link_libraries(MultipleObjectTracking sv_world) add_executable(ColorLineDetection samples/demo/color_line_detect.cpp) target_link_libraries(ColorLineDetection sv_world) add_executable(UdpDetectionInfoReceiver samples/demo/udp_detection_info_receiver.cpp) diff --git a/algorithm/mot/sv_mot.cpp b/algorithm/mot/sv_mot.cpp new file mode 100644 index 0000000..a954b62 --- /dev/null +++ b/algorithm/mot/sv_mot.cpp @@ -0,0 +1,98 @@ +#include "sv_mot.h" +#include +#include +#include "gason.h" +#include "sv_util.h" + + +namespace sv { + + +MultipleObjectTracker::MultipleObjectTracker() +{ + this->_params_loaded = false; +} +MultipleObjectTracker::~MultipleObjectTracker() +{ +} + +void MultipleObjectTracker::track(cv::Mat img_, TargetsInFrame& tgts_) +{ + if (!this->_params_loaded) + { + this->_load(); + this->_params_loaded = true; + } + +} + +void MultipleObjectTracker::init() +{ + if (!this->_params_loaded) + { + this->_load(); + this->_params_loaded = true; + } +} + +void MultipleObjectTracker::_load() +{ + JsonValue all_value; + JsonAllocator allocator; + _load_all_json(this->alg_params_fn, all_value, allocator); + + JsonValue tracker_params_value; + _parser_algorithm_params("MultipleObjectTracker", all_value, tracker_params_value); + + for (auto i : tracker_params_value) { + if ("algorithm" == std::string(i->key)) { + this->_algorithm = i->value.toString(); + std::cout << "algorithm: " << this->_algorithm << std::endl; + } + else if ("with_reid" == std::string(i->key)) { + if (i->value.getTag() == JSON_TRUE) + this->_with_reid = true; + else + this->_with_reid = false; + + std::cout << "with_reid: " << this->_with_reid << std::endl; + } + else if ("reid_input_size" == std::string(i->key) && i->value.getTag() == JSON_ARRAY) { + int jcnt = 0; + for (auto j : i->value) { + if (jcnt == 0) { + this->_reid_input_w = j->value.toNumber(); + } + if (jcnt == 1) { + this->_reid_input_h = j->value.toNumber(); + } + jcnt += 1; + } + std::cout << "reid_input_w: " << this->_reid_input_w << std::endl; + std::cout << "reid_input_h: " << this->_reid_input_h << std::endl; + } + else if ("reid_num_samples" == std::string(i->key)) { + this->_reid_num_samples = i->value.toNumber(); + std::cout << "reid_num_samples: " << this->_reid_num_samples << std::endl; + } + else if ("reid_match_thres" == std::string(i->key)) { + this->_reid_match_thres = i->value.toNumber(); + std::cout << "reid_match_thres: " << this->_reid_match_thres << std::endl; + } + else if ("iou_thres" == std::string(i->key)) { + this->_iou_thres = i->value.toNumber(); + std::cout << "iou_thres: " << this->_iou_thres << std::endl; + } + else if ("max_age" == std::string(i->key)) { + this->_max_age = i->value.toNumber(); + std::cout << "max_age: " << this->_max_age << std::endl; + } + else if ("min_hits" == std::string(i->key)) { + this->_min_hits = i->value.toNumber(); + std::cout << "min_hits: " << this->_min_hits << std::endl; + } + } +} + +} + diff --git a/algorithm/tracking/ocv470/tracking_ocv470_impl.cpp b/algorithm/sot/ocv470/sot_ocv470_impl.cpp similarity index 99% rename from algorithm/tracking/ocv470/tracking_ocv470_impl.cpp rename to algorithm/sot/ocv470/sot_ocv470_impl.cpp index 6e46f23..b711f2e 100644 --- a/algorithm/tracking/ocv470/tracking_ocv470_impl.cpp +++ b/algorithm/sot/ocv470/sot_ocv470_impl.cpp @@ -1,4 +1,4 @@ -#include "tracking_ocv470_impl.h" +#include "sot_ocv470_impl.h" #include #include diff --git a/algorithm/tracking/ocv470/tracking_ocv470_impl.h b/algorithm/sot/ocv470/sot_ocv470_impl.h similarity index 100% rename from algorithm/tracking/ocv470/tracking_ocv470_impl.h rename to algorithm/sot/ocv470/sot_ocv470_impl.h diff --git a/algorithm/tracking/sv_tracking.cpp b/algorithm/sot/sv_sot.cpp similarity index 92% rename from algorithm/tracking/sv_tracking.cpp rename to algorithm/sot/sv_sot.cpp index 094a7a7..8a60045 100644 --- a/algorithm/tracking/sv_tracking.cpp +++ b/algorithm/sot/sv_sot.cpp @@ -1,7 +1,7 @@ -#include "sv_tracking.h" +#include "sv_sot.h" #include #include -#include "tracking_ocv470_impl.h" +#include "sot_ocv470_impl.h" namespace sv { diff --git a/include/sv_mot.h b/include/sv_mot.h new file mode 100644 index 0000000..e708e3b --- /dev/null +++ b/include/sv_mot.h @@ -0,0 +1,42 @@ +#ifndef __SV_MOT__ +#define __SV_MOT__ + +#include "sv_core.h" +#include +#include +#include +#include +#include + + + +namespace sv { + + +class MultipleObjectTracker : public CameraAlgorithm +{ +public: + MultipleObjectTracker(); + ~MultipleObjectTracker(); + + void init(); + void track(cv::Mat img_, TargetsInFrame& tgts_); + +private: + void _load(); + bool _params_loaded; + + std::string _algorithm; + bool _with_reid; + int _reid_input_h; + int _reid_input_w; + int _reid_num_samples; + double _reid_match_thres; + double _iou_thres; + int _max_age; + int _min_hits; +}; + + +} +#endif diff --git a/include/sv_tracking.h b/include/sv_sot.h similarity index 100% rename from include/sv_tracking.h rename to include/sv_sot.h diff --git a/include/sv_world.h b/include/sv_world.h index 14fccd1..29ccde5 100644 --- a/include/sv_world.h +++ b/include/sv_world.h @@ -4,7 +4,8 @@ #include "sv_core.h" #include "sv_common_det.h" #include "sv_landing_det.h" -#include "sv_tracking.h" +#include "sv_sot.h" +#include "sv_mot.h" #include "sv_color_line.h" #include "sv_video_input.h" #include "sv_video_output.h" diff --git a/params/a-params/sv_algorithm_params.json b/params/a-params/sv_algorithm_params.json index 0999353..6d2bd9d 100644 --- a/params/a-params/sv_algorithm_params.json +++ b/params/a-params/sv_algorithm_params.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_1280_wo_mask.json b/params/a-params/sv_algorithm_params_1280_wo_mask.json index 13dc7d1..1aa6685 100644 --- a/params/a-params/sv_algorithm_params_1280_wo_mask.json +++ b/params/a-params/sv_algorithm_params_1280_wo_mask.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_640_w_mask.json b/params/a-params/sv_algorithm_params_640_w_mask.json index 8ccf8a7..8d07775 100644 --- a/params/a-params/sv_algorithm_params_640_w_mask.json +++ b/params/a-params/sv_algorithm_params_640_w_mask.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_640_wo_mask.json b/params/a-params/sv_algorithm_params_640_wo_mask.json index 6fdd4cd..f87fb59 100644 --- a/params/a-params/sv_algorithm_params_640_wo_mask.json +++ b/params/a-params/sv_algorithm_params_640_wo_mask.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_coco_1280.json b/params/a-params/sv_algorithm_params_coco_1280.json index 13dc7d1..1aa6685 100644 --- a/params/a-params/sv_algorithm_params_coco_1280.json +++ b/params/a-params/sv_algorithm_params_coco_1280.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_coco_640.json b/params/a-params/sv_algorithm_params_coco_640.json index 8ccf8a7..8d07775 100644 --- a/params/a-params/sv_algorithm_params_coco_640.json +++ b/params/a-params/sv_algorithm_params_coco_640.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_csrt.json b/params/a-params/sv_algorithm_params_csrt.json index 732bb74..e45bda2 100644 --- a/params/a-params/sv_algorithm_params_csrt.json +++ b/params/a-params/sv_algorithm_params_csrt.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_kcf.json b/params/a-params/sv_algorithm_params_kcf.json index 6dbf6fc..b9baa2d 100644 --- a/params/a-params/sv_algorithm_params_kcf.json +++ b/params/a-params/sv_algorithm_params_kcf.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_nano.json b/params/a-params/sv_algorithm_params_nano.json index 51533ae..816504d 100644 --- a/params/a-params/sv_algorithm_params_nano.json +++ b/params/a-params/sv_algorithm_params_nano.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/params/a-params/sv_algorithm_params_siamrpn.json b/params/a-params/sv_algorithm_params_siamrpn.json index 8ccf8a7..8d07775 100644 --- a/params/a-params/sv_algorithm_params_siamrpn.json +++ b/params/a-params/sv_algorithm_params_siamrpn.json @@ -116,6 +116,16 @@ "backend": 0, "target": 0 }, + "MultipleObjectTracker": { + "algorithm": "sort", + "with_reid": false, + "reid_input_size": [128, 128], + "reid_num_samples": 10, + "reid_match_thres": 2.0, + "iou_thres": 0.5, + "max_age": 10, + "min_hits": 3 + }, "LandingMarkerDetector": { "labels": ["x", "h"], "maxCandidates": 5 diff --git a/samples/demo/multiple_object_tracking.cpp b/samples/demo/multiple_object_tracking.cpp new file mode 100644 index 0000000..613012a --- /dev/null +++ b/samples/demo/multiple_object_tracking.cpp @@ -0,0 +1,41 @@ +#include +#include +// 包含SpireCV SDK头文件 +#include + +using namespace std; + +int main(int argc, char *argv[]) { + // 实例化 + sv::MultipleObjectTracker mot; + // 手动导入相机参数,如果使用Amov的G1等吊舱或相机,则可以忽略该步骤,将自动下载相机参数文件 + mot.loadCameraParams(sv::get_home() + "/SpireCV/calib_webcam_640x480.yaml"); + + // 打开摄像头 + sv::Camera cap; + // cap.setWH(640, 480); + // cap.setFps(30); + cap.open(sv::CameraType::WEBCAM, 0); // CameraID 0 + // 实例化OpenCV的Mat类,用于内存单帧图像 + cv::Mat img; + int frame_id = 0; + while (1) + { + // 实例化SpireCV的 单帧检测结果 接口类 TargetsInFrame + sv::TargetsInFrame tgts(frame_id++); + // 读取一帧图像到img + cap.read(img); + cv::resize(img, img, cv::Size(mot.image_width, mot.image_height)); + + // 执行通用目标检测 + mot.track(img, tgts); + // 可视化检测结果,叠加到img上 + sv::drawTargetsInFrame(img, tgts); + + // 显示检测结果img + cv::imshow("img", img); + cv::waitKey(10); + } + + return 0; +}