NVIDIA DeepStream SDK API Reference

9.2 Release
sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef CAMERA_CALIBRATION_PARSER_H
19 #define CAMERA_CALIBRATION_PARSER_H
20 
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <tuple>
25 #include <nlohmann/json.hpp> // JSON parsing library
26 #include <Eigen/Dense> // For matrix operations
27 #include <variant>
28 #include <iostream>
29 #include <fstream>
30 #include <regex>
31 
33 using namespace Eigen;
34 
35 // Type definitions
36 using Matrix4f = Eigen::Matrix<float, 4, 4>;
37 using Matrix3f = Eigen::Matrix<float, 3, 3>;
38 using Matrix4d = Eigen::Matrix<double, 4, 4>;
39 using Matrix3d = Eigen::Matrix<double, 3, 3>;
40 using Vector3d = Eigen::Matrix<double, 3, 1>;
43 using AugConfig = std::map<std::string, std::variant<float, std::vector<int>, bool>>;
44 
45 // Constants
46 const float FPS = 30.0;
47 
48 // Function declarations
49 
50 // Helper function to convert an Eigen matrix to JSON
51 template <typename Derived>
52 json eigenMatrixToJson(const Eigen::MatrixBase<Derived> &matrix);
53 
54 // Extract all numbers from a string
55 std::vector<int> extractNumbers(const std::string& text);
56 
57 // Extract camera name from a string based on numerical values
58 std::string getCamNameFromString(const std::string& str, int numIndex = -1);
59 
60 // Load camera calibration information for BEVFormer scene from a JSON file
61 json loadCalibBEVFormerScene(const std::string& calibFilePath);
62 
63 // Load camera calibration data from a synthetic JSON file and convert it to a calibration dictionary
64 json loadCalibSyntheticJson(const std::string& calibFilePath, bool recentering = false);
65 
66 // Loads group information from a dictionary if the type matches the provided key
67 std::pair<std::string, std::map<std::string, std::vector<float>>> loadGroup(
68  const json& groupDict, const std::string& key);
69 
70 // Load calibration information for synthetic camera groups from a JSON file
71 std::pair<std::map<std::string, json>, std::map<std::string, json>>
72 loadCalibSyntheticCameraGroupsJson(const std::string &calibFilePath, bool recentering = false);
73 
74 // Load camera calibration data from file (supports different modes and grouping)
75 std::pair<json, json> loadCalib(const std::string& calibFilePath, const std::string& calibMode = "synthetic",
76  bool useCameraGroups = true, bool recentering = false);
77 
78 // Function to normalize matrices (replace -0.0 with +0.0)
79 void normalizeMatrix(Matrix4d& matrix);
80 
81 // Read and parse camera calibration information from a calibration file
82 json fillCameraInfos(const std::string& calibFilePath, const std::string& bevGroupName,
83  const std::string& calibMode = "synthetic", bool useCameraGroups = true,
84  bool recentering = false);
85 
86 // Compute camera calibration matrices from camera information
87 std::vector<Matrix4f> getCameraCalibrationMatrix(const json& info);
88 
89 // Parse calibration file and return lidar-to-image transformation matrices, camera keys and group origin
90 std::tuple<std::vector<Matrix4f>, std::vector<std::string>, std::vector<float>> parseCalibFile(
91  const std::string& calibFilePath, const std::string& bevGroupName,
92  const std::string& calibMode = "synthetic", bool useCameraGroups = true,
93  bool recentering = false);
94 
95 // Fetch scene map information (scale factor and translation to global coordinates)
96 std::pair<float, std::vector<float>> fetchSceneMapInfo(const std::string& calibFilePath);
97 
98 // Helper function to compute object velocities based on positions in consecutive frames
99 std::vector<std::vector<float>> getObjectVelocity(
100  const std::vector<std::vector<float>>& locations,
101  const std::vector<int>& instanceIds,
102  const json& annotationsPrev);
103 
104 // Fills camera information with ground truth data for BEV scene analysis
105 std::vector<json> fillCameraInfosGT(
106  const std::string& calibFilePath, const std::string& bevGroupName,
107  const std::string& groundTruthFilePath, const std::string& sceneName,
108  const std::vector<std::string>& classNames, int startFrame = 0, int frames = 1000);
109 
110 // Transform lidar information based on augmentation configurations
111 std::vector<Matrix4f> transformLidarInfo(
112  const std::vector<Matrix4f> &lidar2img,
113  AugConfig &augConfigs, int height = 1080, int width = 1920);
114 
115 #endif // CAMERA_CALIBRATION_PARSER_H
Vector3f
Eigen::Vector3f Vector3f
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:41
transformLidarInfo
std::vector< Matrix4f > transformLidarInfo(const std::vector< Matrix4f > &lidar2img, AugConfig &augConfigs, int height=1080, int width=1920)
width
JDIMENSION JDIMENSION * width
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jpeglib.h:1001
loadCalibBEVFormerScene
json loadCalibBEVFormerScene(const std::string &calibFilePath)
Matrix4d
Eigen::Matrix< double, 4, 4 > Matrix4d
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:38
getCamNameFromString
std::string getCamNameFromString(const std::string &str, int numIndex=-1)
FPS
const float FPS
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:46
json
nlohmann::json json
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:32
eigenMatrixToJson
json eigenMatrixToJson(const Eigen::MatrixBase< Derived > &matrix)
MatrixXd
Eigen::MatrixXd MatrixXd
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:42
loadCalib
std::pair< json, json > loadCalib(const std::string &calibFilePath, const std::string &calibMode="synthetic", bool useCameraGroups=true, bool recentering=false)
loadGroup
std::pair< std::string, std::map< std::string, std::vector< float > > > loadGroup(const json &groupDict, const std::string &key)
AugConfig
std::map< std::string, std::variant< float, std::vector< int >, bool > > AugConfig
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:43
fillCameraInfosGT
std::vector< json > fillCameraInfosGT(const std::string &calibFilePath, const std::string &bevGroupName, const std::string &groundTruthFilePath, const std::string &sceneName, const std::vector< std::string > &classNames, int startFrame=0, int frames=1000)
extractNumbers
std::vector< int > extractNumbers(const std::string &text)
Matrix4f
Eigen::Matrix< float, 4, 4 > Matrix4f
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/customlib_impl.h:56
fetchSceneMapInfo
std::pair< float, std::vector< float > > fetchSceneMapInfo(const std::string &calibFilePath)
getObjectVelocity
std::vector< std::vector< float > > getObjectVelocity(const std::vector< std::vector< float >> &locations, const std::vector< int > &instanceIds, const json &annotationsPrev)
fillCameraInfos
json fillCameraInfos(const std::string &calibFilePath, const std::string &bevGroupName, const std::string &calibMode="synthetic", bool useCameraGroups=true, bool recentering=false)
loadCalibSyntheticCameraGroupsJson
std::pair< std::map< std::string, json >, std::map< std::string, json > > loadCalibSyntheticCameraGroupsJson(const std::string &calibFilePath, bool recentering=false)
Vector3d
Eigen::Matrix< double, 3, 1 > Vector3d
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:40
Matrix3f
Eigen::Matrix< float, 3, 3 > Matrix3f
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:37
Matrix3d
Eigen::Matrix< double, 3, 3 > Matrix3d
Definition: sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/preprocess/calibration_parser.h:39
loadCalibSyntheticJson
json loadCalibSyntheticJson(const std::string &calibFilePath, bool recentering=false)
normalizeMatrix
void normalizeMatrix(Matrix4d &matrix)
getCameraCalibrationMatrix
std::vector< Matrix4f > getCameraCalibrationMatrix(const json &info)
parseCalibFile
std::tuple< std::vector< Matrix4f >, std::vector< std::string >, std::vector< float > > parseCalibFile(const std::string &calibFilePath, const std::string &bevGroupName, const std::string &calibMode="synthetic", bool useCameraGroups=true, bool recentering=false)