├── CMakeLists.txt ├── LICENSE ├── README.md ├── README_CN.md ├── demo ├── bboxes.yaml ├── config.yaml └── demo.mp4 ├── include ├── argparse │ └── argparser.h ├── image_utils │ ├── detect_process.h │ ├── lap │ │ ├── _lapjv.pyx │ │ ├── lapjv.cpp │ │ ├── lapjv.h │ │ └── lapmod.cpp │ └── tracker.h └── print_utils.h ├── scripts └── estimate_cam_param.py └── src └── main.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | project(trt_det LANGUAGES CXX) 4 | 5 | add_definitions(-std=c++11) 6 | 7 | set(CMAKE_CXX_STANDARD 11) 8 | set(CMAKE_BUILD_TYPE Release) 9 | 10 | include_directories( 11 | include 12 | ) 13 | 14 | find_package(OpenCV REQUIRED) 15 | include_directories(${OpenCV_INCLUDE_DIRS}) 16 | 17 | 18 | find_package(Eigen3 REQUIRED) 19 | include_directories(/usr/include/eigen3) 20 | 21 | 22 | add_executable(ucmc_track 23 | src/main.cpp 24 | include/image_utils/lap/lapjv.cpp 25 | include/image_utils/lap/lapmod.cpp 26 | ) 27 | 28 | target_link_libraries(ucmc_track yaml-cpp) 29 | target_link_libraries(ucmc_track ${OpenCV_LIBS}) 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UCMCTrack C++ Deployment 2 | 3 |
4 | 5 | [简体中文](README_CN.md) 6 | 7 |
8 | 9 | ![demo](https://github.com/LSH9832/UCMCTrack-cpp/assets/69880398/f732ec98-8449-4683-8f1a-a57cc9233416) 10 | 11 | 12 | ## Before using 13 | 14 | Please read [the paper](https://arxiv.org/abs/2312.08952) first! 15 | 16 | Please read [the paper](https://arxiv.org/abs/2312.08952) first! 17 | 18 | Please read [the paper](https://arxiv.org/abs/2312.08952) first! 19 | 20 | **DO NOT USE THIS PROJECT IF YOU KNOW NOTHING ABOUT THIS ALGORITHM!** 21 | 22 | ## Acknowledgements 23 | 24 | First and foremost, I would like to thank [corfyi](https://github.com/corfyi) for open-sourcing this multi-object tracking algorithm. However, as mentioned in the issues, while the author does have a C++ version of the code, it has not been made public. Despite my limited expertise, I spent four days converting the original project ([UCMCTrack](https://github.com/corfyi/UCMCTrack)) from Python to C++ code. And if you find any bugs or inconsistencies in the algorithm logic, please feel free to raise them in the issues section. 25 | 26 | 27 | ## C++ Is Blazing Fast! 28 | 29 | The measured FPS of the Python version of the code is less than 100Hz. In the original paper, the author mentioned that the C++ version of their code can achieve over 1000+ FPS in pure tracking scenarios. If my implementation is correct, it seems to reach around 3000FPS in the demo.mp4 (using an I5-11320H CPU), as shown in the image below. When combined with YOLO, the speed is even faster (due to the GPU inference latency reducing the CPU load). 30 | 31 | ![ucmcspeed](https://github.com/LSH9832/UCMCTrack-cpp/assets/69880398/0594d9c0-ef21-492e-ae78-ac267ba9be19) 32 | 33 | Even on an ARM platform (Orange Pi, RK3588 chip), the speed is almost unchanged (with CPU utilization below 10%). However, when combined with a neural network, there is a significant slowdown (as RKNN not only loads the NPU but also the CPU). With an average CPU utilization of 45%, the latency is approximately 8 ms. 34 | 35 | ![ucmcspeedrk3588](https://github.com/LSH9832/UCMCTrack-cpp/assets/69880398/7943d9ec-0f1a-447b-80d7-a8d8ba2b8820) 36 | 37 | 38 | ## Dependencies 39 | 40 | - libopencv-dev 41 | - libeigen3-dev 42 | - libyaml-cpp0.6 43 | 44 | 45 | ## Notes 46 | 47 | - This project does not provide code for the object detection part. The example code uses the detection results from the demo.mp4 provided by the original author, which are written to the demo/bboxes.yaml file. The parameters are written to the demo/config.yaml file. You can modify the parameters according to your needs and add detection code to replace the results in the file. The detection results in the file are from a model I trained on VisDrone-DET-2019, see my other project [edgeyolo](https://github.com/LSH9832/edgeyolo). The detection framework also uses the cpp source code provided in that project. 48 | 49 | - The code in include/image_utils/lap is from [gatagat/lap](https://github.com/gatagat/lap), which is the source code for the lap library in Python. 50 | 51 | 52 | ## Setup 53 | Modify the CMakeLists.txt file according to your needs, then 54 | ```shell 55 | mkdir build && cd build 56 | cmake .. 57 | make 58 | ``` 59 | ## Using the Demo 60 | Navigate to the build folder and then 61 | ```shell 62 | ./ucmc_track ../demo/bboxes.yaml -f ../demo/config.yaml -p # The '-p' flag pauses the video, press the spacebar to switch to autoplay, or any other key to play frame by frame 63 | 64 | # For detailed usage instructions 65 | ./ucmc_track -? 66 | ``` 67 | 68 | ## Adjusting Extrinsic Parameters 69 | ``` 70 | python scripts/estimate_cam_param.py --img /path/to/your/img.jpg \ # Use an image file 71 | --vid /path/to/your/video.mp4 \ # Or use a video file, do not use the above option if this one is selected 72 | --id number_of_frame_id \ # Use the number_of_frame_id frame from the video file 73 | --cam_para cfg/track.yaml \ # parameter file 74 | --test-only # Test mode only, without modifying the content of the parameter file 75 | ``` 76 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | # UCMCTrack的c++部署 2 | 3 | ![demo](https://github.com/LSH9832/UCMCTrack-cpp/assets/69880398/f732ec98-8449-4683-8f1a-a57cc9233416) 4 | 5 | ## 在使用之前 6 | 7 | 用之前请先阅读[原论文](https://arxiv.org/abs/2312.08952)! 8 | 9 | 用之前请先阅读[原论文](https://arxiv.org/abs/2312.08952)! 10 | 11 | 用之前请先阅读[原论文](https://arxiv.org/abs/2312.08952)! 12 | 13 | **不要在完全不了解算法原理的情况下使用本项目,效果不好概不负责,谢谢!** 14 | 15 | ## 致谢 16 | 17 | 首先感谢大佬[corfyi](https://github.com/corfyi)将此多目标跟踪算法开源,但在[issues](https://github.com/corfyi/UCMCTrack/issues/18)中看出大佬是有c++版本的代码的,却并没有开源。本人水平有限,工作之余花费4天时间将原项目([UCMCTrack](https://github.com/corfyi/UCMCTrack))由python转为c++代码,难免出错,如发现bug或算法逻辑不对的地方欢迎在issues中提出。 18 | 19 | 20 | ## C++真是太快啦! 21 | 22 | 实测python版本的代码FPS不到100Hz, 作者在原文中提到纯跟踪情况下他代码的C++版FPS能达到1000+FPS,如果我的代码没写错的话,在demo.mp4中貌似可以来到3000FPS左右哦,见下图(CPU型号为I5-11320H),和YOLO神经网络一起运行的时候速度更快(由于GPU推理延时使CPU负载反而变低了) 23 | 24 | ![ucmcspeed](https://github.com/LSH9832/UCMCTrack-cpp/assets/69880398/0594d9c0-ef21-492e-ae78-ac267ba9be19) 25 | 26 | 即使在arm平台(香橙派,RK3588芯片)速度也基本没差多少(CPU利用率10%以下),但和神经网络一起跑的时候速度有明显减慢(RKNN不仅让NPU有负载,CPU也有),CPU平均利用率45%时延迟约8毫秒, 27 | 28 | ![ucmcspeedrk3588](https://github.com/LSH9832/UCMCTrack-cpp/assets/69880398/7943d9ec-0f1a-447b-80d7-a8d8ba2b8820) 29 | 30 | 31 | ## 依赖库 32 | 33 | - libopencv-dev 34 | - libeigen3-dev 35 | - libyaml-cpp0.6 36 | 37 | 38 | ## 注意 39 | 40 | - 本项目不提供目标检测部分的代码,示例代码将原作者提供的demo.mp4中的检测结果写在demo/bboxes.yaml文件中了,参数写在demo/config.yaml中,可根据自己的需求改参数,并加入检测部分的代码以替换文件中的检测结果。文件中的检测结果使用的是本人在VisDrone-DET-2019上训练的模型,详见我的另一个项目[edgeyolo](https://github.com/LSH9832/edgeyolo)。其中检测结果的框架也是采用该项目提供的cpp源码。 41 | 42 | - include/image_utils/lap中的代码来自[gatagat/lap](https://github.com/gatagat/lap),也就是python中lap库源码。 43 | 44 | 45 | ## Setup 46 | 根据自己的需求改CMakeLists.txt,然后 47 | ```shell 48 | mkdir build && cd build 49 | cmake .. 50 | make 51 | ``` 52 | ## 使用Demo 53 | 进入build文件夹,然后 54 | ```shell 55 | ./ucmc_track ../demo/bboxes.yaml -f ../demo/config.yaml -p # -p 可以使画面暂停,按空格键切换为自动播放,按其他键逐帧播放 56 | 57 | # 看具体用法 58 | ./ucmc_track -? 59 | ``` 60 | 61 | ## 调整外参矩阵 62 | ``` 63 | python scripts/estimate_cam_param.py --img /path/to/your/img.jpg \ # 使用图像文件 64 | --vid /path/to/your/video.mp4 \ # 或者使用视频文件,上面选项就不要写了 65 | --id number_of_frame_id \ # 使用视频文件的第number_of_frame_id帧 66 | --cam_para cfg/track.yaml \ # 参数文件 67 | --test-only # 仅测试,不修改参数文件内容 68 | ``` 69 | -------------------------------------------------------------------------------- /demo/config.yaml: -------------------------------------------------------------------------------- 1 | Ki: [ 1040., 0., 680., 0., 2 | 0., 1040., 382., 0., 3 | 0., 0., 1., 0. ] 4 | 5 | Ko: [ -0.33962705646204017, -0.9403932802759871, -0.01771837833151917, 0, 6 | -0.49984364998094355, 0.1964143950108213, -0.843550657048088, 1, 7 | 0.7967495140209739, -0.2776362077328922, -0.5367572524549995, 33.64, 8 | 0, 0, 0, 1 ] 9 | 10 | a1: 100.0 11 | a2: 100.0 12 | wx: 5.0 13 | wy: 5.0 14 | vmax: 10.0 15 | max_age: 10.0 16 | high_score: 0.5 17 | conf_threshold: 0.01 18 | -------------------------------------------------------------------------------- /demo/demo.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSH9832/UCMCTrack-cpp/84f617d1815739bf117bcb098a80ef294ddf0cb6/demo/demo.mp4 -------------------------------------------------------------------------------- /include/argparse/argparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | * copy from https://github.com/0382/util/blob/main/cpp/argparse/argparse.hpp 3 | */ 4 | #pragma once 5 | #ifndef JSHL_ARGPARSE_HPP 6 | #define JSHL_ARGPARSE_HPP 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace argsutil 22 | { 23 | 24 | // 尽管使用编译器相关的 ABI 函数可以相对比较优雅的实现这个功能 25 | // 但是不同的编译器在某些类型下可能出现一些奇怪的行为 26 | // 最重要的是 std::string 还是免不了要模板特例化 27 | // 因此,不如在这里限定一些类型,免去不可控制的行为 28 | 29 | // 我们仅支持 bool, int, int64_t, double, std::string 30 | // 想要其他长度的类型,获取值之后自行转换 31 | // 当然,如果你愿意的话,自己定义模板特例化也不是不可以 32 | 33 | template 34 | inline std::string type_string() 35 | { 36 | return "null"; 37 | } 38 | 39 | template <> 40 | inline std::string type_string() 41 | { 42 | return "bool"; 43 | } 44 | 45 | template <> 46 | inline std::string type_string() 47 | { 48 | return "int"; 49 | } 50 | 51 | template <> 52 | inline std::string type_string() 53 | { 54 | return "int64_t"; 55 | } 56 | 57 | template <> 58 | inline std::string type_string() 59 | { 60 | return "double"; 61 | } 62 | 63 | template <> 64 | inline std::string type_string() 65 | { 66 | return "string"; 67 | } 68 | 69 | template 70 | std::string to_string(const T &value) 71 | { 72 | std::ostringstream oss; 73 | oss << value; 74 | return oss.str(); 75 | } 76 | 77 | template 78 | T parse_value(const std::string &value) 79 | { 80 | std::istringstream iss(value); 81 | T result; 82 | iss >> result; 83 | return result; 84 | } 85 | 86 | struct short_circuit_option 87 | { 88 | short_circuit_option(std::string sname, std::string lname, std::string help, std::function callback) 89 | : short_name(std::move(sname)), long_name(std::move(lname)), help(std::move(help)), 90 | callback(std::move(callback)) 91 | {} 92 | std::string short_name; 93 | std::string long_name; 94 | std::string help; 95 | std::function callback; 96 | }; 97 | 98 | struct option 99 | { 100 | option(std::string sname, std::string lname, std::string help, std::string type, std::string value) 101 | : short_name(std::move(sname)), long_name(std::move(lname)), help(std::move(help)), type(std::move(type)), 102 | value(std::move(value)) 103 | {} 104 | 105 | std::string short_name; 106 | std::string long_name; 107 | std::string help; 108 | std::string type; 109 | std::string value; 110 | }; 111 | 112 | struct argument 113 | { 114 | argument(std::string name, std::string help, std::string type) 115 | : name(std::move(name)), help(std::move(help)), type(std::move(type)) 116 | {} 117 | 118 | std::string name; 119 | std::string help; 120 | std::string type; 121 | std::string value; 122 | }; 123 | 124 | class argparser 125 | { 126 | private: 127 | std::string description; 128 | std::string program_name; 129 | std::vector short_circuit_options; 130 | std::vector