├── .gitignore
├── CMakeLists.txt
├── LICENSE
├── README.en.md
├── README.md
├── algorithm
├── color_line
│ └── sv_color_line.cpp
├── common_det
│ ├── cuda
│ │ ├── common_det_cuda_impl.cpp
│ │ ├── common_det_cuda_impl.h
│ │ └── yolov7
│ │ │ ├── calibrator.cpp
│ │ │ ├── calibrator.h
│ │ │ ├── config.h
│ │ │ ├── cuda_utils.h
│ │ │ ├── logging.h
│ │ │ ├── macros.h
│ │ │ ├── model.cpp
│ │ │ ├── model.h
│ │ │ ├── postprocess.cpp
│ │ │ ├── postprocess.h
│ │ │ ├── preprocess.cu
│ │ │ ├── preprocess.h
│ │ │ ├── types.h
│ │ │ ├── utils.h
│ │ │ ├── yololayer.cu
│ │ │ └── yololayer.h
│ ├── intel
│ │ ├── common_det_intel_impl.cpp
│ │ └── common_det_intel_impl.h
│ └── sv_common_det.cpp
├── ellipse_det
│ ├── ellipse_detector.cpp
│ └── ellipse_detector.h
├── landing_det
│ ├── cuda
│ │ ├── landing_det_cuda_impl.cpp
│ │ └── landing_det_cuda_impl.h
│ ├── intel
│ │ ├── landing_det_intel_impl.cpp
│ │ └── landing_det_intel_impl.h
│ └── sv_landing_det.cpp
├── mde
│ ├── cuda
│ │ ├── depth_est_cuda_impl.cpp
│ │ ├── depth_est_cuda_impl.h
│ │ ├── utils.cpp
│ │ └── utils.h
│ └── sv_depth_est.cpp
├── mot
│ ├── bytetrack
│ │ ├── BYTETracker.cpp
│ │ ├── BYTETracker.h
│ │ ├── BytekalmanFilter.cpp
│ │ ├── BytekalmanFilter.h
│ │ ├── STrack.cpp
│ │ ├── STrack.h
│ │ ├── dataType.h
│ │ ├── lapjv.cpp
│ │ ├── lapjv.h
│ │ └── utils.cpp
│ ├── sort
│ │ ├── sort.cpp
│ │ └── sort.h
│ └── sv_mot.cpp
├── sot
│ ├── ocv470
│ │ ├── sot_ocv470_impl.cpp
│ │ └── sot_ocv470_impl.h
│ └── sv_sot.cpp
├── sv_algorithm_base.cpp
└── veri
│ ├── cuda
│ ├── veri_det_cuda_impl.cpp
│ └── veri_det_cuda_impl.h
│ ├── intel
│ ├── veri_det_intel_impl.cpp
│ └── veri_det_intel_impl.h
│ └── sv_veri_det.cpp
├── build_on_jetson.sh
├── build_on_x86_cuda.sh
├── build_on_x86_intel.sh
├── gimbal_ctrl
├── IOs
│ └── serial
│ │ ├── README.md
│ │ ├── include
│ │ └── serial
│ │ │ ├── impl
│ │ │ ├── unix.h
│ │ │ └── win.h
│ │ │ ├── serial.h
│ │ │ └── v8stdint.h
│ │ └── src
│ │ ├── impl
│ │ ├── list_ports
│ │ │ ├── list_ports_linux.cc
│ │ │ ├── list_ports_osx.cc
│ │ │ └── list_ports_win.cc
│ │ ├── unix.cc
│ │ └── win.cc
│ │ └── serial.cc
├── driver
│ └── src
│ │ ├── AT10
│ │ ├── AT10_gimbal_crc32.h
│ │ ├── AT10_gimbal_driver.cpp
│ │ ├── AT10_gimbal_driver.h
│ │ ├── AT10_gimbal_funtion.cpp
│ │ └── AT10_gimbal_struct.h
│ │ ├── FIFO
│ │ ├── Ring_Fifo.cpp
│ │ └── Ring_Fifo.h
│ │ ├── G1
│ │ ├── g1_gimbal_crc32.h
│ │ ├── g1_gimbal_driver.cpp
│ │ ├── g1_gimbal_driver.h
│ │ ├── g1_gimbal_funtion.cpp
│ │ └── g1_gimbal_struct.h
│ │ ├── GX40
│ │ ├── GX40_gimbal_crc16.h
│ │ ├── GX40_gimbal_driver.cpp
│ │ ├── GX40_gimbal_driver.h
│ │ ├── GX40_gimbal_funtion.cpp
│ │ └── GX40_gimbal_struct.h
│ │ ├── Q10f
│ │ ├── Q10f_gimbal_crc32.h
│ │ ├── Q10f_gimbal_driver.cpp
│ │ ├── Q10f_gimbal_driver.h
│ │ ├── Q10f_gimbal_funtion.cpp
│ │ └── Q10f_gimbal_struct.h
│ │ ├── SU17-G
│ │ ├── SU17_gimbal_crc.h
│ │ ├── SU17_gimbal_driver.cpp
│ │ ├── SU17_gimbal_driver.h
│ │ ├── SU17_gimbal_funtion.cpp
│ │ └── SU17_gimbal_struct.h
│ │ ├── amovGimbal
│ │ ├── amov_gimbal.h
│ │ ├── amov_gimbal_c.h
│ │ └── amov_gimbal_struct.h
│ │ ├── amov_gimbal_factory.cpp
│ │ ├── amov_gimbal_interface.cpp
│ │ ├── amov_gimbal_interface_c.cpp
│ │ ├── amov_gimbal_private.h
│ │ ├── amov_gimbal_realize.cpp
│ │ └── amov_tool.h
├── sv_gimbal.cpp
└── sv_gimbal_io.hpp
├── include
├── sv_algorithm_base.h
├── sv_color_line.h
├── sv_common_det.h
├── sv_core.h
├── sv_depth_est.h
├── sv_gimbal.h
├── sv_landing_det.h
├── sv_mot.h
├── sv_sot.h
├── sv_veri_det.h
├── sv_video_base.h
├── sv_video_input.h
├── sv_video_output.h
└── sv_world.h
├── params
├── a-params
│ ├── sv_algorithm_params.json
│ ├── sv_algorithm_params_1280_wo_mask.json
│ ├── sv_algorithm_params_640_w_mask.json
│ ├── sv_algorithm_params_640_wo_mask.json
│ ├── sv_algorithm_params_coco_1280.json
│ ├── sv_algorithm_params_coco_640.json
│ ├── sv_algorithm_params_csrt.json
│ ├── sv_algorithm_params_kcf.json
│ ├── sv_algorithm_params_nano.json
│ └── sv_algorithm_params_siamrpn.json
└── c-params
│ ├── calib_webcam_1280x720.yaml
│ ├── calib_webcam_1920x1080.yaml
│ └── calib_webcam_640x480.yaml
├── samples
├── SpireCVDet.cpp
├── SpireCVSeg.cpp
├── calib
│ ├── aruco_samples_utility.hpp
│ ├── calibrate_camera_charuco.cpp
│ └── create_marker.cpp
├── demo
│ ├── aruco_detection.cpp
│ ├── camera_reading.cpp
│ ├── color_line_detect.cpp
│ ├── common_object_detection.cpp
│ ├── detection_with_clicked_tracking.cpp
│ ├── ellipse_detection.cpp
│ ├── eval_MOT_metric.cpp
│ ├── gimbal_aruco_detection.cpp
│ ├── gimbal_detection_with_clicked_tracking.cpp
│ ├── gimbal_landing_marker_detection.cpp
│ ├── gimbal_udp_detection_info_sender.cpp
│ ├── landing_marker_detection.cpp
│ ├── mon_depth_estimation.cpp
│ ├── multiple_object_tracking.cpp
│ ├── single_object_tracking.cpp
│ ├── udp_detection_info_receiver.cpp
│ ├── udp_detection_info_receiver_by_airborne.cpp
│ ├── udp_detection_info_sender.cpp
│ ├── veri.cpp
│ ├── video_saving.cpp
│ └── video_streaming.cpp
└── test
│ ├── eval_fps_on_video.cpp
│ ├── eval_mAP_on_coco_val
│ ├── coco_eval.py
│ ├── eval_mAP_on_coco_val.cpp
│ └── pd2cocojson.py
│ ├── gimbal_test.cpp
│ └── mot_metrics
│ ├── eval_mot_metric_gt.sh
│ ├── run_mot_challenge.py
│ └── write_val_seqmap.py
├── scripts
├── common
│ ├── configs-downloading.sh
│ ├── download_test_videos.sh
│ ├── ffmpeg425-install.sh
│ ├── gst-install-orin.sh
│ ├── gst-install.sh
│ ├── models-converting.sh
│ ├── models-downloading.sh
│ └── opencv470-install.sh
├── jetson
│ ├── configs-downloading.sh
│ ├── gst-install-orin.sh
│ ├── gst-install.sh
│ ├── opencv470-install.sh
│ ├── opencv470-jetpack511-cuda-install.sh
│ └── opencv470-jetpack511-install.sh
├── model_sync.py
├── test
│ ├── test_fps.sh
│ └── test_gimbal.sh
├── x86-cuda
│ ├── configs-downloading.sh
│ ├── ffmpeg425-install.sh
│ ├── gst-install.sh
│ ├── opencv470-cuda-install.sh
│ ├── opencv470-install.sh
│ ├── ubuntu1804-cuda-cudnn-11-1.sh
│ ├── ubuntu2004-cuda-cudnn-11-6.sh
│ └── ubuntu2004-cudnn-12-0-for40series.sh
└── x86-intel
│ ├── configs-downloading.sh
│ ├── ffmpeg-install.sh
│ ├── gst-install.sh
│ ├── libva-install-gst.sh
│ ├── libva-install.sh
│ ├── opencl-vaapi-install.sh
│ ├── opencv470-intel-install.sh
│ ├── openvino-install.sh
│ └── zlm-server-install.sh
├── utils
├── gason.cpp
├── gason.h
├── sv_crclib.cpp
├── sv_crclib.h
├── sv_util.cpp
└── sv_util.h
└── video_io
├── ffmpeg
├── x86_cuda
│ ├── bs_common.h
│ ├── bs_push_streamer.cpp
│ ├── bs_push_streamer.h
│ ├── bs_video_saver.cpp
│ └── bs_video_saver.h
└── x86_intel
│ ├── bs_common.h
│ ├── bs_push_streamer.cpp
│ ├── bs_push_streamer.h
│ ├── bs_video_saver.cpp
│ └── bs_video_saver.h
├── gstreamer
├── streamer_gstreamer_impl.cpp
├── streamer_gstreamer_impl.h
├── writer_gstreamer_impl.cpp
└── writer_gstreamer_impl.h
├── sv_video_base.cpp
├── sv_video_input.cpp
└── sv_video_output.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | # Distribution / packaging
2 | .Python
3 | build/
4 | models/
5 | confs/
6 | ZLM/
7 | ZLMediaKit/
8 | ffmpeg-4.2.5/
9 | nv-codec-headers/
10 | *.bz2
11 | develop-eggs/
12 | dist/
13 | eggs/
14 | .eggs/
15 | parts/
16 | sdist/
17 | var/
18 | wheels/
19 | share/python-wheels/
20 | *.egg-info/
21 | .installed.cfg
22 | *.egg
23 | MANIFEST
24 | .idea/
25 | calib_webcam_1280x720.yaml
26 | calib_webcam_640x480.yaml
27 | sv_algorithm_params.json
28 | sv_algorithm_params_coco_1280.json
29 | sv_algorithm_params_coco_640.json
30 |
31 | # Prerequisites
32 | *.d
33 |
34 | # Compiled Object files
35 | *.slo
36 | *.lo
37 | *.o
38 | *.obj
39 |
40 | # Precompiled Headers
41 | *.gch
42 | *.pch
43 |
44 | # Compiled Dynamic libraries
45 | *.so
46 | *.dylib
47 | *.dll
48 |
49 | # Fortran module files
50 | *.mod
51 | *.smod
52 |
53 | # Compiled Static libraries
54 | *.lai
55 | *.la
56 | *.a
57 | *.lib
58 |
59 | # Executables
60 | *.exe
61 | *.out
62 | *.app
63 |
64 | # Byte-compiled / optimized / DLL files
65 | __pycache__/
66 | *.py[cod]
67 | *$py.class
68 |
69 | # PyInstaller
70 | # Usually these files are written by a python script from a template
71 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
72 | *.manifest
73 | *.spec
74 |
75 | # Environments
76 | .env
77 | .venv
78 | env/
79 | venv/
80 | ENV/
81 | env.bak/
82 | venv.bak/
83 |
84 | # VSCode Editor
85 | .vscode/
86 |
87 |
--------------------------------------------------------------------------------
/README.en.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # SpireCV Intelligent perception algorithm library
4 |
5 | ## Overview
6 |
7 | SpireCV is an **real-time edge perception SDK** built for **intelligent unmanned systems**, with main functions including **camera/pod control**, **video saving and push streaming**, **target detection and tracking**, and **edge data management**. It aims to provide mobile robot developers with high-performance, high-reliability, simple interface and feature-rich visual perception capabilities.
8 |
9 | - Github:https://github.com/amov-lab/SpireCV
10 | - Gitee:https://gitee.com/amovlab/SpireCV
11 | - **Maintaining an open-source project is not easy, please click star to support us, thanks! **
12 |
13 | ## Quick start
14 |
15 | - Installation and use: [SpireCV user manual](https://docs.amovlab.com/Spire_CV_Amov/#/)、[SpireCV developer kit guide](https://docs.amovlab.com/spirecvkit/#/)
16 | - Basic knowledge of C++ language and CMake compilation tool is required
17 | - Need to master the foundation of OpenCV vision library and understand the computational libraries such as CUDA, OpenVINO, RKNN and CANN
18 | - Need to understand the basic concepts and basic operation of ROS
19 |
20 | - Q&A and communication:
21 | - Q&A forum (official regular Q&A, recommended): [Amovlab Community - SpireCV Q&A Zone](https://bbs.amovlab.com/)
22 | - Add WeChat yinyue199506 (note: SpireCV) into the SpireCV intelligent perception algorithm library exchange group
23 | - Search and subscribe "Amovlab" on YouTube.com, we will update the video from time to time
24 |
25 | ## Framework
26 |
27 | #### The main framework is shown in the figure:
28 |
29 |
30 |
31 | #### Current support:
32 | - **Functional level**:
33 | - [x] Video algorithm module (providing perceptual algorithms with unified interfaces, efficient performance and diverse functions)
34 | - [x] Video input, save and push stream module (to provide stable, cross-platform video reading and writing capabilities)
35 | - [x] Camera and pod control module (for the typical hardware ecology to open the interface, easy to use)
36 | - [x] Sensing information interaction module (providing UDP communication protocol)
37 | - [x] [ROS interface](https://gitee.com/amovlab1/spirecv-ros.git)
38 | - **Platform level**:
39 | - [x] X86 + Nvidia GPUs (10 series, 20 series, and 30 series graphics cards recommended)
40 | - [x] Jetson (AGX Orin/Xavier、Orin NX/Nano、Xavier NX)
41 | - [x] Intel CPU
42 | - [ ] HUAWEI Ascend (coming soon)
43 | - [ ] Rockchip (coming soon)
44 |
45 | ## Demos
46 | - **QR code detection**
47 |
48 |
49 | - **Landing mark detection**
50 |
51 |
52 | - **Ellipse detection**
53 |
54 |
55 | - **Target click tracking (including target detection and tracking)**
56 |
57 |
58 | - **Low latency push streaming**
59 |
60 |
61 |
62 | ## Copyright statement
63 |
64 | - SpireCV is protected under the Apache License 2.0.
65 | - SpireCV is for personal use only, please do NOT use it for commercial purposes.
66 | - If this project is used for profit, Amovlab will pursue infringement.
67 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # SpireCV 智能感知算法库
4 |
5 | ## 项目概况
6 |
7 | SpireCV是一个专为**智能无人系统**打造的**边缘实时感知SDK**,主要功能包括**相机/吊舱控制**、**视频保存与推流**、**目标探测识别与跟踪**、**边缘数据管理迭代**等。旨在为移动机器人开发者提供高性能、高可靠、接口简洁、功能丰富的视觉感知能力。
8 |
9 | - Github:https://github.com/amov-lab/SpireCV
10 | - Gitee:https://gitee.com/amovlab/SpireCV
11 | - **开源项目,维护不易,还烦请点一个star收藏,谢谢支持!**
12 |
13 | ## 快速入门
14 |
15 | - 安装及使用:[SpireCV使用手册](https://docs.amovlab.com/Spire_CV_Amov/#/)([wolai版本](https://www.wolai.com/4qWFM6aZmtpQE6jj7hnNMW))、[SpireCV开发者套件指南](https://docs.amovlab.com/spirecvkit/#/)
16 | - 需掌握C++语言基础、CMake编译工具基础。
17 | - 需要掌握OpenCV视觉库基础,了解CUDA、OpenVINO、RKNN和CANN等计算库。
18 | - 需要了解ROS基本概念及基本操作。
19 |
20 | - 答疑及交流:
21 | - 答疑论坛(官方定期答疑,推荐):[阿木社区-SpireCV问答专区](https://bbs.amovlab.com/)
22 | - 添加微信yinyue199506(备注消息:SpireCV)进入SpireCV智能感知算法库交流群。
23 | - B站搜索并关注“阿木社区”,开发团队定期直播答疑。
24 |
25 | ## 项目框架
26 |
27 | #### 主要框架如图所示:
28 |
29 |
30 |
31 | #### 目前支持情况:
32 | - **功能层**:
33 | - [x] 视频算法模块(提供接口统一、性能高效、功能多样的感知算法)
34 | - [x] 视频输入、保存与推流模块(提供稳定、跨平台的视频读写能力)
35 | - [x] 相机、吊舱控制模块(针对典型硬件生态打通接口,易使用)
36 | - [x] 感知信息交互模块(提供UDP通信协议)
37 | - [x] [ROS接口](https://gitee.com/amovlab1/spirecv-ros.git)
38 | - **平台层**:
39 | - [x] X86+Nvidia GPU(推荐10系、20系、30系显卡)
40 | - [x] Jetson(AGX Orin/Xavier、Orin NX/Nano、Xavier NX)
41 | - [x] Intel CPU
42 | - [ ] HUAWEI Ascend(推进中)
43 | - [ ] Rockchip(推进中)
44 |
45 | ## 功能展示
46 | - **二维码检测**
47 |
48 |
49 | - **起降标志检测**
50 |
51 |
52 | - **椭圆检测**
53 |
54 |
55 | - **目标点击跟踪(含目标检测、目标跟踪)**
56 |
57 |
58 | - **低延迟推流**
59 |
60 |
61 |
62 | ## 版权声明
63 |
64 | - 本项目受 Apache License 2.0 协议保护。
65 | - 本项目仅限个人使用,请勿用于商业用途。
66 | - 如利用本项目进行营利活动,阿木实验室将追究侵权行为。
67 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/common_det_cuda_impl.h:
--------------------------------------------------------------------------------
1 | #ifndef __SV_COMMON_DET_CUDA__
2 | #define __SV_COMMON_DET_CUDA__
3 |
4 | #include "sv_core.h"
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 |
12 |
13 | #ifdef WITH_CUDA
14 | #include
15 | #include
16 | #endif
17 |
18 |
19 |
20 | namespace sv {
21 |
22 |
23 | class CommonObjectDetectorCUDAImpl
24 | {
25 | public:
26 | CommonObjectDetectorCUDAImpl();
27 | ~CommonObjectDetectorCUDAImpl();
28 |
29 | bool cudaSetup(CommonObjectDetectorBase* base_, bool input_4k_);
30 | void cudaDetect(
31 | CommonObjectDetectorBase* base_,
32 | cv::Mat img_,
33 | std::vector& boxes_x_,
34 | std::vector& boxes_y_,
35 | std::vector& boxes_w_,
36 | std::vector& boxes_h_,
37 | std::vector& boxes_label_,
38 | std::vector& boxes_score_,
39 | std::vector& boxes_seg_,
40 | bool input_4k_
41 | );
42 |
43 | #ifdef WITH_CUDA
44 | void _prepare_buffers_seg(int input_h, int input_w, int batchsize);
45 | void _prepare_buffers(int input_h, int input_w, int batchsize);
46 | nvinfer1::IExecutionContext* _context;
47 | nvinfer1::IRuntime* _runtime;
48 | nvinfer1::ICudaEngine* _engine;
49 | cudaStream_t _stream;
50 | float* _gpu_buffers[3];
51 | float* _cpu_output_buffer;
52 | float* _cpu_output_buffer1;
53 | float* _cpu_output_buffer2;
54 | #endif
55 | };
56 |
57 |
58 | }
59 | #endif
60 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/calibrator.cpp:
--------------------------------------------------------------------------------
1 | #include "calibrator.h"
2 | #include "cuda_utils.h"
3 | #include "utils.h"
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | static cv::Mat preprocess_img(cv::Mat& img, int input_w, int input_h) {
12 | int w, h, x, y;
13 | float r_w = input_w / (img.cols * 1.0);
14 | float r_h = input_h / (img.rows * 1.0);
15 | if (r_h > r_w) {
16 | w = input_w;
17 | h = r_w * img.rows;
18 | x = 0;
19 | y = (input_h - h) / 2;
20 | } else {
21 | w = r_h * img.cols;
22 | h = input_h;
23 | x = (input_w - w) / 2;
24 | y = 0;
25 | }
26 | cv::Mat re(h, w, CV_8UC3);
27 | cv::resize(img, re, re.size(), 0, 0, cv::INTER_LINEAR);
28 | cv::Mat out(input_h, input_w, CV_8UC3, cv::Scalar(128, 128, 128));
29 | re.copyTo(out(cv::Rect(x, y, re.cols, re.rows)));
30 | return out;
31 | }
32 |
33 | Int8EntropyCalibrator2::Int8EntropyCalibrator2(int batchsize, int input_w, int input_h, const char* img_dir, const char* calib_table_name, const char* input_blob_name, bool read_cache)
34 | : batchsize_(batchsize),
35 | input_w_(input_w),
36 | input_h_(input_h),
37 | img_idx_(0),
38 | img_dir_(img_dir),
39 | calib_table_name_(calib_table_name),
40 | input_blob_name_(input_blob_name),
41 | read_cache_(read_cache) {
42 | input_count_ = 3 * input_w * input_h * batchsize;
43 | CUDA_CHECK(cudaMalloc(&device_input_, input_count_ * sizeof(float)));
44 | read_files_in_dir(img_dir, img_files_);
45 | }
46 |
47 | Int8EntropyCalibrator2::~Int8EntropyCalibrator2() {
48 | CUDA_CHECK(cudaFree(device_input_));
49 | }
50 |
51 | int Int8EntropyCalibrator2::getBatchSize() const TRT_NOEXCEPT {
52 | return batchsize_;
53 | }
54 |
55 | bool Int8EntropyCalibrator2::getBatch(void* bindings[], const char* names[], int nbBindings) TRT_NOEXCEPT {
56 | if (img_idx_ + batchsize_ > (int)img_files_.size()) {
57 | return false;
58 | }
59 |
60 | std::vector input_imgs_;
61 | for (int i = img_idx_; i < img_idx_ + batchsize_; i++) {
62 | std::cout << img_files_[i] << " " << i << std::endl;
63 | cv::Mat temp = cv::imread(img_dir_ + img_files_[i]);
64 | if (temp.empty()) {
65 | std::cerr << "Fatal error: image cannot open!" << std::endl;
66 | return false;
67 | }
68 | cv::Mat pr_img = preprocess_img(temp, input_w_, input_h_);
69 | input_imgs_.push_back(pr_img);
70 | }
71 | img_idx_ += batchsize_;
72 | cv::Mat blob = cv::dnn::blobFromImages(input_imgs_, 1.0 / 255.0, cv::Size(input_w_, input_h_), cv::Scalar(0, 0, 0), true, false);
73 |
74 | CUDA_CHECK(cudaMemcpy(device_input_, blob.ptr(0), input_count_ * sizeof(float), cudaMemcpyHostToDevice));
75 | assert(!strcmp(names[0], input_blob_name_));
76 | bindings[0] = device_input_;
77 | return true;
78 | }
79 |
80 | const void* Int8EntropyCalibrator2::readCalibrationCache(size_t& length) TRT_NOEXCEPT {
81 | std::cout << "reading calib cache: " << calib_table_name_ << std::endl;
82 | calib_cache_.clear();
83 | std::ifstream input(calib_table_name_, std::ios::binary);
84 | input >> std::noskipws;
85 | if (read_cache_ && input.good()) {
86 | std::copy(std::istream_iterator(input), std::istream_iterator(), std::back_inserter(calib_cache_));
87 | }
88 | length = calib_cache_.size();
89 | return length ? calib_cache_.data() : nullptr;
90 | }
91 |
92 | void Int8EntropyCalibrator2::writeCalibrationCache(const void* cache, size_t length) TRT_NOEXCEPT {
93 | std::cout << "writing calib cache: " << calib_table_name_ << " size: " << length << std::endl;
94 | std::ofstream output(calib_table_name_, std::ios::binary);
95 | output.write(reinterpret_cast(cache), length);
96 | }
97 |
98 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/calibrator.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "macros.h"
4 | #include
5 | #include
6 |
7 | //! \class Int8EntropyCalibrator2
8 | //!
9 | //! \brief Implements Entropy calibrator 2.
10 | //! CalibrationAlgoType is kENTROPY_CALIBRATION_2.
11 | //!
12 | class Int8EntropyCalibrator2 : public nvinfer1::IInt8EntropyCalibrator2 {
13 | public:
14 | Int8EntropyCalibrator2(int batchsize, int input_w, int input_h, const char* img_dir, const char* calib_table_name, const char* input_blob_name, bool read_cache = true);
15 |
16 | virtual ~Int8EntropyCalibrator2();
17 | int getBatchSize() const TRT_NOEXCEPT override;
18 | bool getBatch(void* bindings[], const char* names[], int nbBindings) TRT_NOEXCEPT override;
19 | const void* readCalibrationCache(size_t& length) TRT_NOEXCEPT override;
20 | void writeCalibrationCache(const void* cache, size_t length) TRT_NOEXCEPT override;
21 |
22 | private:
23 | int batchsize_;
24 | int input_w_;
25 | int input_h_;
26 | int img_idx_;
27 | std::string img_dir_;
28 | std::vector img_files_;
29 | size_t input_count_;
30 | std::string calib_table_name_;
31 | const char* input_blob_name_;
32 | bool read_cache_;
33 | void* device_input_;
34 | std::vector calib_cache_;
35 | };
36 |
37 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/config.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | /* --------------------------------------------------------
4 | * These configs are related to tensorrt model, if these are changed,
5 | * please re-compile and re-serialize the tensorrt model.
6 | * --------------------------------------------------------*/
7 |
8 | // For INT8, you need prepare the calibration dataset, please refer to
9 | // https://github.com/wang-xinyu/tensorrtx/tree/master/yolov5#int8-quantization
10 | #define USE_FP16 // set USE_INT8 or USE_FP16 or USE_FP32
11 |
12 | // These are used to define input/output tensor names,
13 | // you can set them to whatever you want.
14 | const static char* kInputTensorName = "data";
15 | const static char* kOutputTensorName = "prob";
16 |
17 | // Detection model and Segmentation model' number of classes
18 | // constexpr static int kNumClass = 80;
19 |
20 | // Classfication model's number of classes
21 | constexpr static int kClsNumClass = 1000;
22 |
23 | constexpr static int kBatchSize = 1;
24 |
25 | // Yolo's input width and height must by divisible by 32
26 | // constexpr static int kInputH = 640;
27 | // constexpr static int kInputW = 640;
28 |
29 | // Classfication model's input shape
30 | constexpr static int kClsInputH = 224;
31 | constexpr static int kClsInputW = 224;
32 |
33 | // Maximum number of output bounding boxes from yololayer plugin.
34 | // That is maximum number of output bounding boxes before NMS.
35 | constexpr static int kMaxNumOutputBbox = 1000;
36 |
37 | constexpr static int kNumAnchor = 3;
38 |
39 | // The bboxes whose confidence is lower than kIgnoreThresh will be ignored in yololayer plugin.
40 | constexpr static float kIgnoreThresh = 0.1f;
41 |
42 | /* --------------------------------------------------------
43 | * These configs are NOT related to tensorrt model, if these are changed,
44 | * please re-compile, but no need to re-serialize the tensorrt model.
45 | * --------------------------------------------------------*/
46 |
47 | // NMS overlapping thresh and final detection confidence thresh
48 | const static float kNmsThresh = 0.45f;
49 | const static float kConfThresh = 0.5f;
50 |
51 | const static int kGpuId = 0;
52 |
53 | // If your image size is larger than 4096 * 3112, please increase this value
54 | const static int kMaxInputImageSize = 4096 * 3112;
55 |
56 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/cuda_utils.h:
--------------------------------------------------------------------------------
1 | #ifndef TRTX_CUDA_UTILS_H_
2 | #define TRTX_CUDA_UTILS_H_
3 |
4 | #include
5 |
6 | #ifndef CUDA_CHECK
7 | #define CUDA_CHECK(callstr)\
8 | {\
9 | cudaError_t error_code = callstr;\
10 | if (error_code != cudaSuccess) {\
11 | std::cerr << "CUDA error " << error_code << " at " << __FILE__ << ":" << __LINE__;\
12 | assert(0);\
13 | }\
14 | }
15 | #endif // CUDA_CHECK
16 |
17 | #endif // TRTX_CUDA_UTILS_H_
18 |
19 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/macros.h:
--------------------------------------------------------------------------------
1 | #ifndef __MACROS_H
2 | #define __MACROS_H
3 |
4 | #include
5 |
6 | #ifdef API_EXPORTS
7 | #if defined(_MSC_VER)
8 | #define API __declspec(dllexport)
9 | #else
10 | #define API __attribute__((visibility("default")))
11 | #endif
12 | #else
13 |
14 | #if defined(_MSC_VER)
15 | #define API __declspec(dllimport)
16 | #else
17 | #define API
18 | #endif
19 | #endif // API_EXPORTS
20 |
21 | #if NV_TENSORRT_MAJOR >= 8
22 | #define TRT_NOEXCEPT noexcept
23 | #define TRT_CONST_ENQUEUE const
24 | #else
25 | #define TRT_NOEXCEPT
26 | #define TRT_CONST_ENQUEUE
27 | #endif
28 |
29 | #endif // __MACROS_H
30 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/model.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | nvinfer1::ICudaEngine* build_det_engine(unsigned int maxBatchSize, nvinfer1::IBuilder* builder,
7 | nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt,
8 | float& gd, float& gw, std::string& wts_name, int input_h, int input_w, int n_classes);
9 |
10 | nvinfer1::ICudaEngine* build_det_p6_engine(unsigned int maxBatchSize, nvinfer1::IBuilder* builder,
11 | nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt,
12 | float& gd, float& gw, std::string& wts_name, int input_h, int input_w, int n_classes);
13 |
14 | nvinfer1::ICudaEngine* build_cls_engine(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, float& gd, float& gw, std::string& wts_name);
15 |
16 | nvinfer1::ICudaEngine* build_seg_engine(unsigned int maxBatchSize, nvinfer1::IBuilder* builder, nvinfer1::IBuilderConfig* config, nvinfer1::DataType dt, float& gd, float& gw, std::string& wts_name, int input_h, int input_w, int n_classes);
17 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/postprocess.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "types.h"
4 | #include
5 |
6 | cv::Rect get_rect(cv::Mat& img, float bbox[4], int input_h, int input_w);
7 |
8 | void nms(std::vector& res, float *output, float conf_thresh, float nms_thresh = 0.5);
9 |
10 | void batch_nms(std::vector>& batch_res, float *output, int batch_size, int output_size, float conf_thresh, float nms_thresh = 0.5);
11 |
12 | void draw_bbox(std::vector& img_batch, std::vector>& res_batch, int input_h, int input_w);
13 |
14 | std::vector process_mask(const float* proto, int proto_size, std::vector& dets, int input_h, int input_w);
15 |
16 | void draw_mask_bbox(cv::Mat& img, std::vector& dets, std::vector& masks, std::unordered_map& labels_map, int input_h, int input_w);
17 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/preprocess.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include
6 |
7 | void cuda_preprocess_init(int max_image_size);
8 | void cuda_preprocess_destroy();
9 | void cuda_preprocess(uint8_t* src, int src_width, int src_height,
10 | float* dst, int dst_width, int dst_height,
11 | cudaStream_t stream);
12 | void cuda_batch_preprocess(std::vector& img_batch,
13 | float* dst, int dst_width, int dst_height,
14 | cudaStream_t stream);
15 |
16 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/types.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "config.h"
4 |
5 | struct YoloKernel {
6 | int width;
7 | int height;
8 | float anchors[kNumAnchor * 2];
9 | };
10 |
11 | struct alignas(float) Detection {
12 | float bbox[4]; // center_x center_y w h
13 | float conf; // bbox_conf * cls_conf
14 | float class_id;
15 | float mask[32];
16 | };
17 |
18 |
--------------------------------------------------------------------------------
/algorithm/common_det/cuda/yolov7/utils.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | static inline int read_files_in_dir(const char* p_dir_name, std::vector& file_names) {
12 | DIR *p_dir = opendir(p_dir_name);
13 | if (p_dir == nullptr) {
14 | return -1;
15 | }
16 |
17 | struct dirent* p_file = nullptr;
18 | while ((p_file = readdir(p_dir)) != nullptr) {
19 | if (strcmp(p_file->d_name, ".") != 0 &&
20 | strcmp(p_file->d_name, "..") != 0) {
21 | //std::string cur_file_name(p_dir_name);
22 | //cur_file_name += "/";
23 | //cur_file_name += p_file->d_name;
24 | std::string cur_file_name(p_file->d_name);
25 | file_names.push_back(cur_file_name);
26 | }
27 | }
28 |
29 | closedir(p_dir);
30 | return 0;
31 | }
32 |
33 | // Function to trim leading and trailing whitespace from a string
34 | static inline std::string trim_leading_whitespace(const std::string& str) {
35 | size_t first = str.find_first_not_of(' ');
36 | if (std::string::npos == first) {
37 | return str;
38 | }
39 | size_t last = str.find_last_not_of(' ');
40 | return str.substr(first, (last - first + 1));
41 | }
42 |
43 | // Src: https://stackoverflow.com/questions/16605967
44 | static inline std::string to_string_with_precision(const float a_value, const int n = 2) {
45 | std::ostringstream out;
46 | out.precision(n);
47 | out << std::fixed << a_value;
48 | return out.str();
49 | }
50 |
51 | static inline int read_labels(const std::string labels_filename, std::unordered_map& labels_map) {
52 |
53 | std::ifstream file(labels_filename);
54 | // Read each line of the file
55 | std::string line;
56 | int index = 0;
57 | while (std::getline(file, line)) {
58 | // Strip the line of any leading or trailing whitespace
59 | line = trim_leading_whitespace(line);
60 |
61 | // Add the stripped line to the labels_map, using the loop index as the key
62 | labels_map[index] = line;
63 | index++;
64 | }
65 | // Close the file
66 | file.close();
67 |
68 | return 0;
69 | }
70 |
71 |
--------------------------------------------------------------------------------
/algorithm/common_det/intel/common_det_intel_impl.h:
--------------------------------------------------------------------------------
1 | #ifndef __SV_COMMON_DET_INTEL__
2 | #define __SV_COMMON_DET_INTEL__
3 |
4 | #include "sv_core.h"
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 |
13 | #ifdef WITH_INTEL
14 | #include
15 | #endif
16 |
17 | struct Resize
18 | {
19 | cv::Mat resized_image;
20 | int dw;
21 | int dh;
22 | };
23 |
24 | struct Detection
25 | {
26 | int class_id;
27 | float confidence;
28 | cv::Rect box;
29 | };
30 |
31 | namespace sv
32 | {
33 |
34 | class CommonObjectDetectorIntelImpl
35 | {
36 | public:
37 | CommonObjectDetectorIntelImpl();
38 | ~CommonObjectDetectorIntelImpl();
39 |
40 | bool intelSetup(CommonObjectDetectorBase *base_, bool input_4k_);
41 | void intelDetect(
42 | CommonObjectDetectorBase *base_,
43 | cv::Mat img_,
44 | std::vector &boxes_x_,
45 | std::vector &boxes_y_,
46 | std::vector &boxes_w_,
47 | std::vector &boxes_h_,
48 | std::vector &boxes_label_,
49 | std::vector &boxes_score_,
50 | std::vector &boxes_seg_,
51 | bool input_4k_);
52 | void preprocess_img(cv::Mat &img_);
53 | void preprocess_img_seg(cv::Mat &img_, std::vector &paddings);
54 | void postprocess_img_seg(cv::Mat &img_,
55 | std::vector &paddings,
56 | std::vector &boxes_x_,
57 | std::vector &boxes_y_,
58 | std::vector &boxes_w_,
59 | std::vector &boxes_h_,
60 | std::vector &boxes_label_,
61 | std::vector &boxes_score_,
62 | std::vector &boxes_seg_,
63 | double &thrs_conf,
64 | double &thrs_nms);
65 |
66 | void postprocess_img(std::vector &boxes_x_,
67 | std::vector &boxes_y_,
68 | std::vector &boxes_w_,
69 | std::vector &boxes_h_,
70 | std::vector &boxes_label_,
71 | std::vector &boxes_score_,
72 | double &thrs_conf,
73 | double &thrs_nms);
74 |
75 | #ifdef WITH_INTEL
76 | int inpWidth;
77 | int inpHeight;
78 | bool with_segmentation;
79 | float rx; // the width ratio of original image and resized image
80 | float ry; // the height ratio of original image and resized image
81 | Resize resize;
82 | ov::Tensor input_tensor;
83 | ov::InferRequest infer_request;
84 | ov::CompiledModel compiled_model;
85 | #endif
86 | };
87 |
88 | }
89 | #endif
90 |
--------------------------------------------------------------------------------
/algorithm/common_det/sv_common_det.cpp:
--------------------------------------------------------------------------------
1 | #include "sv_common_det.h"
2 | #include
3 | #include
4 |
5 | #ifdef WITH_CUDA
6 | #include
7 | #include
8 | #include "common_det_cuda_impl.h"
9 | #endif
10 |
11 | #ifdef WITH_INTEL
12 | #include
13 | #include "common_det_intel_impl.h"
14 | #endif
15 |
16 | namespace sv {
17 |
18 |
19 | CommonObjectDetector::CommonObjectDetector(bool input_4k)
20 | {
21 | this->_input_4k = input_4k;
22 | #ifdef WITH_CUDA
23 | this->_cuda_impl = new CommonObjectDetectorCUDAImpl;
24 | #endif
25 |
26 | #ifdef WITH_INTEL
27 | this->_intel_impl = new CommonObjectDetectorIntelImpl;
28 | #endif
29 | }
30 | CommonObjectDetector::~CommonObjectDetector()
31 | {
32 | }
33 |
34 | bool CommonObjectDetector::setupImpl()
35 | {
36 | #ifdef WITH_CUDA
37 | return this->_cuda_impl->cudaSetup(this, this->_input_4k);
38 | #endif
39 |
40 | #ifdef WITH_INTEL
41 | return this->_intel_impl->intelSetup(this, this->_input_4k);
42 | #endif
43 | return false;
44 | }
45 |
46 | void CommonObjectDetector::detectImpl(
47 | cv::Mat img_,
48 | std::vector& boxes_x_,
49 | std::vector& boxes_y_,
50 | std::vector& boxes_w_,
51 | std::vector& boxes_h_,
52 | std::vector& boxes_label_,
53 | std::vector& boxes_score_,
54 | std::vector& boxes_seg_
55 | )
56 | {
57 | #ifdef WITH_CUDA
58 | this->_cuda_impl->cudaDetect(
59 | this,
60 | img_,
61 | boxes_x_,
62 | boxes_y_,
63 | boxes_w_,
64 | boxes_h_,
65 | boxes_label_,
66 | boxes_score_,
67 | boxes_seg_,
68 | this->_input_4k);
69 | #endif
70 |
71 | #ifdef WITH_INTEL
72 | this->_intel_impl->intelDetect(
73 | this,
74 | img_,
75 | boxes_x_,
76 | boxes_y_,
77 | boxes_w_,
78 | boxes_h_,
79 | boxes_label_,
80 | boxes_score_,
81 | boxes_seg_,
82 | this->_input_4k);
83 | #endif
84 | }
85 |
86 | }
87 |
88 |
--------------------------------------------------------------------------------
/algorithm/landing_det/cuda/landing_det_cuda_impl.h:
--------------------------------------------------------------------------------
1 | #ifndef __SV_LANDING_DET_CUDA__
2 | #define __SV_LANDING_DET_CUDA__
3 |
4 | #include "sv_core.h"
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 |
12 |
13 | #ifdef WITH_CUDA
14 | #include
15 | #include
16 | #endif
17 |
18 |
19 |
20 | namespace sv {
21 |
22 |
23 | class LandingMarkerDetectorCUDAImpl
24 | {
25 | public:
26 | LandingMarkerDetectorCUDAImpl();
27 | ~LandingMarkerDetectorCUDAImpl();
28 |
29 | bool cudaSetup();
30 | void cudaRoiCNN(
31 | std::vector& input_rois_,
32 | std::vector& output_labels_
33 | );
34 |
35 | #ifdef WITH_CUDA
36 | float *_p_data;
37 | float *_p_prob;
38 | nvinfer1::IExecutionContext *_trt_context;
39 | int _input_index;
40 | int _output_index;
41 | void *_p_buffers[2];
42 | cudaStream_t _cu_stream;
43 | #endif
44 | };
45 |
46 |
47 | }
48 | #endif
49 |
--------------------------------------------------------------------------------
/algorithm/landing_det/intel/landing_det_intel_impl.cpp:
--------------------------------------------------------------------------------
1 | #include "landing_det_intel_impl.h"
2 | #include
3 | #include
4 | #include "sv_util.h"
5 |
6 | #define SV_MODEL_DIR "/SpireCV/models/"
7 | #define SV_ROOT_DIR "/SpireCV/"
8 |
9 | namespace sv
10 | {
11 |
12 | #ifdef WITH_INTEL
13 | using namespace cv;
14 | using namespace std;
15 | using namespace dnn;
16 | #endif
17 |
18 | LandingMarkerDetectorIntelImpl::LandingMarkerDetectorIntelImpl()
19 | {
20 | }
21 |
22 | LandingMarkerDetectorIntelImpl::~LandingMarkerDetectorIntelImpl()
23 | {
24 | }
25 |
26 | bool LandingMarkerDetectorIntelImpl::intelSetup()
27 | {
28 | #ifdef WITH_INTEL
29 | std::string onnx_model_fn = get_home() + SV_MODEL_DIR + "LandingMarker.onnx";
30 | std::vector files;
31 | list_dir(get_home() + SV_MODEL_DIR, files, "-online.onnx", "Int-LandingMarker-resnet34");
32 | if (files.size() > 0)
33 | {
34 | std::sort(files.rbegin(), files.rend(), _comp_str_lesser);
35 | onnx_model_fn = get_home() + SV_MODEL_DIR + files[0];
36 | }
37 |
38 | if (!is_file_exist(onnx_model_fn))
39 | {
40 | throw std::runtime_error("SpireCV (104) Error loading the LandingMarker ONNX model (File Not Exist)");
41 | }
42 |
43 | // OpenVINO
44 | ov::Core core;
45 | std::shared_ptr model = core.read_model(onnx_model_fn);
46 | ov::preprocess::PrePostProcessor ppp = ov::preprocess::PrePostProcessor(model);
47 | ppp.input().tensor().set_element_type(ov::element::u8).set_layout("NHWC").set_color_format(ov::preprocess::ColorFormat::RGB);
48 | ppp.input().preprocess().convert_element_type(ov::element::f32).convert_color(ov::preprocess::ColorFormat::RGB).scale({255, 255, 255}); // .scale({ 112, 112, 112 });
49 | ppp.input().model().set_layout("NCHW");
50 | ppp.output().tensor().set_element_type(ov::element::f32);
51 | model = ppp.build();
52 | this->compiled_model = core.compile_model(model, "GPU");
53 | this->infer_request = compiled_model.create_infer_request();
54 |
55 | return true;
56 | #endif
57 | return false;
58 | }
59 |
60 | void LandingMarkerDetectorIntelImpl::intelRoiCNN(
61 | std::vector &input_rois_,
62 | std::vector &output_labels_)
63 | {
64 | #ifdef WITH_INTEL
65 | output_labels_.clear();
66 |
67 | for (int i = 0; i < input_rois_.size(); i++)
68 | {
69 | cv::Mat e_roi = input_rois_[i];
70 |
71 | // Get input port for model with one input
72 | auto input_port = compiled_model.input();
73 | // Create tensor from external memory
74 | ov::Tensor input_tensor(input_port.get_element_type(), input_port.get_shape(), e_roi.ptr(0));
75 | // Set input tensor for model with one input
76 | infer_request.set_input_tensor(input_tensor);
77 | //preprocess_img(e_roi);
78 |
79 | // infer_request.infer();
80 | infer_request.start_async();
81 | infer_request.wait();
82 |
83 | const ov::Tensor &output_tensor = infer_request.get_output_tensor();
84 | ov::Shape output_shape = output_tensor.get_shape();
85 | this->_p_prob = output_tensor.data();
86 |
87 | // Find max index
88 | double max = 0;
89 | int label = 0;
90 | for (int i = 0; i < 11; ++i)
91 | {
92 | if (max < this->_p_prob[i])
93 | {
94 | max = this->_p_prob[i];
95 | label = i;
96 | }
97 | }
98 | output_labels_.push_back(label);
99 | }
100 |
101 | #endif
102 | }
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/algorithm/landing_det/intel/landing_det_intel_impl.h:
--------------------------------------------------------------------------------
1 | #ifndef __SV_LANDING_DET_INTEL__
2 | #define __SV_LANDING_DET_INTEL__
3 |
4 | #include "sv_core.h"
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | #ifdef WITH_INTEL
12 | #include
13 | #endif
14 |
15 | namespace sv
16 | {
17 | class LandingMarkerDetectorIntelImpl
18 | {
19 | public:
20 | LandingMarkerDetectorIntelImpl();
21 | ~LandingMarkerDetectorIntelImpl();
22 |
23 | bool intelSetup();
24 | void intelRoiCNN(
25 | std::vector &input_rois_,
26 | std::vector &output_labels_);
27 |
28 | #ifdef WITH_INTEL
29 | float *_p_prob;
30 |
31 | ov::Tensor input_tensor;
32 | ov::InferRequest infer_request;
33 | ov::CompiledModel compiled_model;
34 | #endif
35 | };
36 | }
37 | #endif
38 |
--------------------------------------------------------------------------------
/algorithm/landing_det/sv_landing_det.cpp:
--------------------------------------------------------------------------------
1 | #include "sv_landing_det.h"
2 | #include
3 | #include
4 | #ifdef WITH_CUDA
5 | #include
6 | #include
7 | #include "landing_det_cuda_impl.h"
8 | #endif
9 |
10 | #ifdef WITH_INTEL
11 | #include
12 | #include "landing_det_intel_impl.h"
13 | #endif
14 |
15 | namespace sv {
16 |
17 |
18 | LandingMarkerDetector::LandingMarkerDetector()
19 | {
20 | #ifdef WITH_CUDA
21 | this->_cuda_impl = new LandingMarkerDetectorCUDAImpl;
22 | #endif
23 |
24 | #ifdef WITH_INTEL
25 | this->_intel_impl = new LandingMarkerDetectorIntelImpl;
26 | #endif
27 | }
28 | LandingMarkerDetector::~LandingMarkerDetector()
29 | {
30 | }
31 |
32 | bool LandingMarkerDetector::setupImpl()
33 | {
34 | #ifdef WITH_CUDA
35 | return this->_cuda_impl->cudaSetup();
36 | #endif
37 |
38 | #ifdef WITH_INTEL
39 | return this->_intel_impl->intelSetup();
40 | #endif
41 | return false;
42 | }
43 |
44 | void LandingMarkerDetector::roiCNN(
45 | std::vector& input_rois_,
46 | std::vector& output_labels_
47 | )
48 | {
49 | #ifdef WITH_CUDA
50 | this->_cuda_impl->cudaRoiCNN(
51 | input_rois_,
52 | output_labels_
53 | );
54 | #endif
55 |
56 | #ifdef WITH_INTEL
57 | this->_intel_impl->intelRoiCNN(
58 | input_rois_,
59 | output_labels_
60 | );
61 | #endif
62 | }
63 | }
64 |
65 |
--------------------------------------------------------------------------------
/algorithm/mde/cuda/depth_est_cuda_impl.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "utils.h"
9 | #include "sv_core.h"
10 |
11 | #ifdef WITH_CUDA
12 | #include
13 | #include
14 | #endif
15 |
16 | namespace sv
17 | {
18 | class DepthEstimationCUDAImpl
19 | {
20 | public:
21 | DepthEstimationCUDAImpl();
22 | ~DepthEstimationCUDAImpl();
23 |
24 | void predict(cv::Mat& img_, cv::Mat& mde_);
25 | bool cudaSetup(MonocularDepthEstimationBase* base_);
26 |
27 | private:
28 | int input_w = 406;
29 | int input_h = 406;
30 | float mean[3] = { 123.675, 116.28, 103.53 };
31 | float std[3] = { 58.395, 57.12, 57.375 };
32 |
33 | std::vector offset;
34 |
35 | nvinfer1::IRuntime* runtime;
36 | nvinfer1::ICudaEngine* engine;
37 | nvinfer1::IExecutionContext* context;
38 |
39 | void* buffer[2];
40 | float* depth_data;
41 | cudaStream_t stream;
42 |
43 | std::vector preprocess(cv::Mat& img_);
44 | };
45 | }
--------------------------------------------------------------------------------
/algorithm/mde/cuda/utils.cpp:
--------------------------------------------------------------------------------
1 | #include "utils.h"
2 |
3 | std::tuple resize_depth(cv::Mat& img, int w, int h)
4 | {
5 | cv::Mat result;
6 | int nw, nh;
7 | int ih = img.rows;
8 | int iw = img.cols;
9 | float aspectRatio = (float)img.cols / (float)img.rows;
10 |
11 | if (aspectRatio >= 1)
12 | {
13 | nw = w;
14 | nh = int(h / aspectRatio);
15 | }
16 | else
17 | {
18 | nw = int(w * aspectRatio);
19 | nh = h;
20 | }
21 | cv::resize(img, img, cv::Size(nw, nh));
22 | result = cv::Mat::ones(cv::Size(w, h), CV_8UC1) * 128;
23 | cv::cvtColor(result, result, cv::COLOR_GRAY2RGB);
24 | cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
25 |
26 | cv::Mat re(h, w, CV_8UC3);
27 | cv::resize(img, re, re.size(), 0, 0, cv::INTER_LINEAR);
28 | cv::Mat out(h, w, CV_8UC3, 0.0);
29 | re.copyTo(out(cv::Rect(0, 0, re.cols, re.rows)));
30 |
31 | std::tuple res_tuple = std::make_tuple(out, (w - nw) / 2, (h - nh) / 2);
32 |
33 | return res_tuple;
34 | }
--------------------------------------------------------------------------------
/algorithm/mde/cuda/utils.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | struct DepthEstimation
8 | {
9 | int x;
10 | int y;
11 | int label;
12 |
13 | DepthEstimation()
14 | {
15 | x = 0;
16 | y = 0;
17 | label = -1;
18 | }
19 | };
20 |
21 |
22 | std::tuple resize_depth(cv::Mat& img, int w, int h);
23 |
24 |
--------------------------------------------------------------------------------
/algorithm/mde/sv_depth_est.cpp:
--------------------------------------------------------------------------------
1 | #include "sv_depth_est.h"
2 | #include
3 | #include
4 | #include "gason.h"
5 | #include "sv_util.h"
6 |
7 | #ifdef WITH_CUDA
8 | #include
9 | #include
10 | #include "depth_est_cuda_impl.h"
11 | #endif
12 |
13 | #define SV_ROOT_DIR "/SpireCV/"
14 |
15 | namespace sv
16 | {
17 |
18 | MonocularDepthEstimation::MonocularDepthEstimation()
19 | {
20 | #ifdef WITH_CUDA
21 | this->_cuda_impl = new DepthEstimationCUDAImpl;
22 | #endif
23 | }
24 | MonocularDepthEstimation::~MonocularDepthEstimation()
25 | {
26 | }
27 |
28 | bool MonocularDepthEstimation::setupImpl()
29 | {
30 | #ifdef WITH_CUDA
31 | return this->_cuda_impl->cudaSetup(this);
32 | #endif
33 | return false;
34 | }
35 |
36 | void MonocularDepthEstimation::predictImpl(cv::Mat img_, cv::Mat& mde_)
37 | {
38 | #ifdef WITH_CUDA
39 | this->_cuda_impl->predict(img_, mde_);
40 | #endif
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/algorithm/mot/bytetrack/BYTETracker.h:
--------------------------------------------------------------------------------
1 | #ifndef __SV_BYTETrack__
2 | #define __SV_BYTETrack__
3 |
4 | #include "sv_core.h"
5 | #include "STrack.h"
6 |
7 | class detect_result
8 | {
9 | public:
10 | int classId;
11 | float confidence;
12 | cv::Rect_ box;
13 | };
14 |
15 | namespace sv {
16 |
17 | class BYTETracker
18 | {
19 | public:
20 | BYTETracker(int frame_rate = 30, int track_buffer = 30);
21 | ~BYTETracker();
22 |
23 | void update(TargetsInFrame &tgts);
24 | cv::Scalar get_color(int idx);
25 |
26 | private:
27 | std::vector joint_stracks( std::vector &tlista, std::vector &tlistb);
28 | std::vector joint_stracks( std::vector &tlista, std::vector &tlistb);
29 |
30 | std::vector sub_stracks( std::vector &tlista, std::vector &tlistb);
31 | void remove_duplicate_stracks( std::vector &resa, std::vector &resb, std::vector &stracksa, std::vector &stracksb);
32 |
33 | void linear_assignment( std::vector< std::vector > &cost_matrix, int cost_matrix_size, int cost_matrix_size_size, float thresh,
34 | std::vector< std::vector > &matches, std::vector &unmatched_a, std::vector &unmatched_b);
35 | std::vector< std::vector > iou_distance( std::vector &atracks, std::vector &btracks, int &dist_size, int &dist_size_size);
36 | std::vector< std::vector > iou_distance( std::vector &atracks, std::vector &btracks);
37 | std::vector< std::vector > ious( std::vector< std::vector > &atlbrs, std::vector< std::vector > &btlbrs);
38 |
39 | double lapjv(const std::vector< std::vector > &cost, std::vector &rowsol, std::vector &colsol,
40 | bool extend_cost = false, float cost_limit = LONG_MAX, bool return_cost = true);
41 |
42 | private:
43 |
44 | float track_thresh;
45 | float high_thresh;
46 | float match_thresh;
47 | int frame_id;
48 | int max_time_lost;
49 |
50 | std::vector tracked_stracks;
51 | std::vector lost_stracks;
52 | std::vector removed_stracks;
53 | byte_kalman::ByteKalmanFilter kalman_filter;
54 | };
55 | }
56 | #endif
--------------------------------------------------------------------------------
/algorithm/mot/bytetrack/BytekalmanFilter.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "dataType.h"
4 |
5 | namespace byte_kalman
6 | {
7 | class ByteKalmanFilter
8 | {
9 | public:
10 | static const double chi2inv95[10];
11 | ByteKalmanFilter();
12 | KAL_DATA initiate(const DETECTBOX& measurement);
13 | void predict(KAL_MEAN& mean, KAL_COVA& covariance);
14 | KAL_HDATA project(const KAL_MEAN& mean, const KAL_COVA& covariance);
15 | KAL_DATA update(const KAL_MEAN& mean,
16 | const KAL_COVA& covariance,
17 | const DETECTBOX& measurement);
18 |
19 | Eigen::Matrix gating_distance(
20 | const KAL_MEAN& mean,
21 | const KAL_COVA& covariance,
22 | const std::vector& measurements,
23 | bool only_position = false);
24 |
25 | private:
26 | Eigen::Matrix _motion_mat;
27 | Eigen::Matrix _update_mat;
28 | float _std_weight_position;
29 | float _std_weight_velocity;
30 | };
31 | }
--------------------------------------------------------------------------------
/algorithm/mot/bytetrack/STrack.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include "BytekalmanFilter.h"
5 |
6 | enum TrackState { New = 0, Tracked, Lost, Removed };
7 |
8 | class STrack
9 | {
10 | public:
11 | STrack( std::vector tlwh_, float score);
12 | ~STrack();
13 |
14 | std::vector static tlbr_to_tlwh( std::vector &tlbr);
15 | void static multi_predict( std::vector &stracks, byte_kalman::ByteKalmanFilter &kalman_filter);
16 | void static_tlwh();
17 | void static_tlbr();
18 | std::vector tlwh_to_xyah( std::vector tlwh_tmp);
19 | std::vector to_xyah();
20 | void mark_lost();
21 | void mark_removed();
22 | int next_id();
23 | int end_frame();
24 |
25 | void activate(byte_kalman::ByteKalmanFilter &kalman_filter, int frame_id);
26 | void re_activate(STrack &new_track, int frame_id, bool new_id = false);
27 | void update(STrack &new_track, int frame_id);
28 |
29 | public:
30 | bool is_activated;
31 | int track_id;
32 | int state;
33 |
34 | std::vector _tlwh;
35 | std::vector tlwh;
36 | std::vector tlbr;
37 | int frame_id;
38 | int tracklet_len;
39 | int start_frame;
40 |
41 | KAL_MEAN mean;
42 | KAL_COVA covariance;
43 | float score;
44 |
45 | private:
46 | byte_kalman::ByteKalmanFilter kalman_filter;
47 | };
--------------------------------------------------------------------------------
/algorithm/mot/bytetrack/dataType.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | #include
7 | #include
8 |
9 |
10 |
11 | typedef Eigen::Matrix DETECTBOX;
12 | typedef Eigen::Matrix DETECTBOXSS;
13 |
14 |
15 | //Kalmanfilter
16 | typedef Eigen::Matrix KAL_FILTER;
17 | typedef Eigen::Matrix KAL_MEAN;
18 | typedef Eigen::Matrix KAL_COVA;
19 | typedef Eigen::Matrix KAL_HMEAN;
20 | typedef Eigen::Matrix KAL_HCOVA;
21 | using KAL_DATA = std::pair;
22 | using KAL_HDATA = std::pair;
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/algorithm/mot/bytetrack/lapjv.h:
--------------------------------------------------------------------------------
1 | #ifndef LAPJV_H
2 | #define LAPJV_H
3 |
4 | #define LARGE 1000000
5 |
6 | #if !defined TRUE
7 | #define TRUE 1
8 | #endif
9 | #if !defined FALSE
10 | #define FALSE 0
11 | #endif
12 |
13 | #define NEW(x, t, n) if ((x = (t *)malloc(sizeof(t) * (n))) == 0) { return -1; }
14 | #define FREE(x) if (x != 0) { free(x); x = 0; }
15 | #define SWAP_INDICES(a, b) { int_t _temp_index = a; a = b; b = _temp_index; }
16 |
17 | #if 0
18 | #include
19 | #define ASSERT(cond) assert(cond)
20 | #define PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__)
21 | #define PRINT_COST_ARRAY(a, n) \
22 | while (1) { \
23 | printf(#a" = ["); \
24 | if ((n) > 0) { \
25 | printf("%f", (a)[0]); \
26 | for (uint_t j = 1; j < n; j++) { \
27 | printf(", %f", (a)[j]); \
28 | } \
29 | } \
30 | printf("]\n"); \
31 | break; \
32 | }
33 | #define PRINT_INDEX_ARRAY(a, n) \
34 | while (1) { \
35 | printf(#a" = ["); \
36 | if ((n) > 0) { \
37 | printf("%d", (a)[0]); \
38 | for (uint_t j = 1; j < n; j++) { \
39 | printf(", %d", (a)[j]); \
40 | } \
41 | } \
42 | printf("]\n"); \
43 | break; \
44 | }
45 | #else
46 | #define ASSERT(cond)
47 | #define PRINTF(fmt, ...)
48 | #define PRINT_COST_ARRAY(a, n)
49 | #define PRINT_INDEX_ARRAY(a, n)
50 | #endif
51 |
52 |
53 | typedef signed int int_t;
54 | typedef unsigned int uint_t;
55 | typedef double cost_t;
56 | typedef char boolean;
57 | typedef enum fp_t { FP_1 = 1, FP_2 = 2, FP_DYNAMIC = 3 } fp_t;
58 |
59 | extern int_t lapjv_internal(
60 | const uint_t n, cost_t *cost[],
61 | int_t *x, int_t *y);
62 |
63 | #endif // LAPJV_H
--------------------------------------------------------------------------------
/algorithm/mot/sort/sort.h:
--------------------------------------------------------------------------------
1 | #ifndef __SV_SORT__
2 | #define __SV_SORT__
3 |
4 | #include "sv_core.h"
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 |
13 | namespace sv {
14 |
15 |
16 |
17 | // define the tracklet struct to store the tracked objects.
18 | struct Tracklet
19 | {
20 | /* data */
21 | public:
22 | Eigen::Vector4d bbox; // double x, y, w, h;
23 | int id = 0;
24 | int age;
25 | int hits;
26 | int misses;
27 | int frame_id = 0;
28 | int category_id;
29 | bool tentative;
30 | std::vector features;
31 | Eigen::Matrix mean;
32 | Eigen::Matrix covariance;
33 | };
34 |
35 |
36 | class KalmanFilter {
37 | public:
38 | KalmanFilter();
39 | ~KalmanFilter();
40 | std::pair, Eigen::Matrix > initiate(Eigen::Vector4d &bbox);
41 | std::pair, Eigen::Matrix > update(Eigen::Matrix mean, Eigen::Matrix covariances, Box &box);
42 | std::pair, Eigen::Matrix > predict(Eigen::Matrix mean, Eigen::Matrix covariances);
43 | std::pair, Eigen::Matrix > project(Eigen::Matrix mean, Eigen::Matrix covariances);
44 | private:
45 | Eigen::Matrix _F;
46 | Eigen::Matrix _H;
47 | Eigen::Matrix _chi2inv95;
48 | double _std_weight_position;
49 | double _std_weight_vel;
50 | };
51 |
52 |
53 | class SORT {
54 | public:
55 | SORT(double iou_threshold, int max_age, int min_hits): _iou_threshold(iou_threshold), _max_age(max_age), _min_hits(min_hits), _next_tracklet_id(0) {};
56 | ~SORT();
57 | void update(TargetsInFrame &tgts);
58 | std::vector getTracklets() const;
59 | private:
60 | double _iou(Tracklet &tracklet, Box &box);
61 | std::vector> _hungarian(std::vector> costMatrix);
62 | double _findMin(const std::vector& vec);
63 | void _subtractMinFromRows(std::vector>& costMatrix);
64 | void _subtractMinFromCols(std::vector>& costMatrix);
65 | //bool _augment(const std::vector>& costMatrix, int row, std::vector& rowMatch, std::vector& colMatch, std::vector& visited);
66 |
67 | double _iou_threshold;
68 | int _max_age;
69 | int _min_hits;
70 | int _next_tracklet_id;
71 | std::vector _tracklets;
72 | std::vector _new_tracklets;
73 | };
74 |
75 |
76 | }
77 | #endif
78 |
--------------------------------------------------------------------------------
/algorithm/sot/ocv470/sot_ocv470_impl.h:
--------------------------------------------------------------------------------
1 | #ifndef __SV_TRACKING_OCV470__
2 | #define __SV_TRACKING_OCV470__
3 |
4 | #include "sv_core.h"
5 | #include
6 | #include
7 | #include
8 | #include