Files
ANSCORE/modules/ANSODEngine/ANSTENSORRTSEG.cpp

1191 lines
54 KiB
C++

#include "ANSTENSORRTSEG.h"
#include "Utility.h"
#include <opencv2/cudaimgproc.hpp>
#include <future>
namespace ANSCENTER
{
bool TENSORRTSEG::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) {
return false;
}
if (!FileExist(_modelFilePath)) {
this->_logger.LogFatal("TENSORRTSEG::OptimizeModel", "Raw model file path does not exist", __FILE__, __LINE__);
return false;
}
try {
_fp16 = fp16;
optimizedModelFolder = GetParentFolder(_modelFilePath);
// Check if the engine already exists to avoid reinitializing
if (!m_trtEngine) {
// Fixed batch size of 1 for this model
m_options.optBatchSize = _modelConfig.gpuOptBatchSize;
m_options.maxBatchSize = _modelConfig.gpuMaxBatchSize;
m_options.deviceIndex = _modelConfig.gpuDeviceIndex;
m_options.maxInputHeight = _modelConfig.maxInputHeight;
m_options.minInputHeight = _modelConfig.minInputHeight;
m_options.optInputHeight = _modelConfig.optInputHeight;
m_options.maxInputWidth = _modelConfig.maxInputWidth;
m_options.minInputWidth = _modelConfig.minInputWidth;
m_options.optInputWidth = _modelConfig.optInputWidth;
m_options.engineFileDir = optimizedModelFolder;
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
// Create the TensorRT inference engine
m_trtEngine = std::make_unique<Engine<float>>(m_options);
}
// Build the TensorRT engine
auto succ = m_trtEngine->buildWithRetry(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE);
if (!succ) {
const std::string errMsg =
"Error: Unable to build the TensorRT engine. "
"Try increasing TensorRT log severity to kVERBOSE.";
this->_logger.LogError("TENSORRTSEG::OptimizeModel", errMsg, __FILE__, __LINE__);
_modelLoadValid = false;
return false;
}
_modelLoadValid = true;
return true;
}
catch (const std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::OptimizeModel", e.what(), __FILE__, __LINE__);
optimizedModelFolder.clear();
return false;
}
}
bool TENSORRTSEG::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
bool result = ANSODBase::LoadModel(modelZipFilePath, modelZipPassword);
if (!result) return false;
_modelConfig.detectionType = ANSCENTER::DetectionType::SEGMENTATION;
_modelConfig.modelType = ModelType::RTSEG;
_modelConfig.inpHeight = 640;
_modelConfig.inpWidth = 640;
if (_modelConfig.modelMNSThreshold < 0.2)
_modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5;
if (_modelConfig.numKPS <= 0 || _modelConfig.numKPS > 133) // 133 = COCO wholebody max
_modelConfig.numKPS = 17;
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = true; // Load Model from Here
// Load Model from Here
TOP_K = 100;
SEG_CHANNELS = 32;
PROBABILITY_THRESHOLD = _modelConfig.detectionScoreThreshold;
NMS_THRESHOLD = _modelConfig.modelMNSThreshold;
SEGMENTATION_THRESHOLD = 0.5f;
SEG_H = 160;
SEG_W = 160;
NUM_KPS = _modelConfig.numKPS;
KPS_THRESHOLD = _modelConfig.kpsThreshold;
SEG_CHANNELS = 32; // For segmentation
if (!m_trtEngine) {
// Fixed batch size of 1 for this model
m_options.optBatchSize = _modelConfig.gpuOptBatchSize;
m_options.maxBatchSize = _modelConfig.gpuMaxBatchSize;
m_options.deviceIndex = _modelConfig.gpuDeviceIndex;
m_options.maxInputHeight = _modelConfig.maxInputHeight;
m_options.minInputHeight = _modelConfig.minInputHeight;
m_options.optInputHeight = _modelConfig.optInputHeight;
m_options.maxInputWidth = _modelConfig.maxInputWidth;
m_options.minInputWidth = _modelConfig.minInputWidth;
m_options.optInputWidth = _modelConfig.optInputWidth;
m_options.engineFileDir = _modelFolder;
// Use FP16 or FP32 precision based on the input flag
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
// Create the TensorRT inference engine
m_trtEngine = std::make_unique<Engine<float>>(m_options);
}
// 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0)_modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0)_modelConfig.inpWidth = inputShape[1];
}
}
else {// This is old version of model zip file
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValidFileName(_classFilePath);
if (!isValidFileName)
{
this->_logger.LogDebug("TENSORRTSEG::Initialize. Load classes from string", _classFilePath, __FILE__, __LINE__);
LoadClassesFromString();
}
else {
this->_logger.LogDebug("TENSORRTSEG::Initialize. Load classes from file", _classFilePath, __FILE__, __LINE__);
LoadClassesFromFile();
}
}
// Load the TensorRT engine file
if (this->_loadEngineOnCreation) {
auto succ = m_trtEngine->buildLoadNetwork(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
if (!succ) {
const std::string errMsg = "Error: Unable to load TensorRT engine weights into memory. " + _modelFilePath;
this->_logger.LogError("TENSORRTSEG::Initialize", errMsg, __FILE__, __LINE__);
_modelLoadValid = false;
return false;
}
}
_modelLoadValid = true;
_isInitialized = true;
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::LoadModel", e.what(), __FILE__, __LINE__);
return false;
}
}
bool TENSORRTSEG::LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try
{
bool result = ANSODBase::LoadModelFromFolder(licenseKey, modelConfig, modelName, className, modelFolder, labelMap);
if (!result) return false;
_modelConfig = modelConfig;
_modelConfig.detectionType = ANSCENTER::DetectionType::SEGMENTATION;
_modelConfig.modelType = ModelType::RTSEG;
_modelConfig.inpHeight = 640;
_modelConfig.precisionType = PrecisionType::FP32; // Default to FP16 for TensorRT models
if (_modelConfig.numKPS <= 0 || _modelConfig.numKPS > 133) // 133 = COCO wholebody max
_modelConfig.numKPS = 17;
_modelConfig.inpWidth = 640;
if (_modelConfig.modelMNSThreshold < 0.2)
_modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5;
if (_modelConfig.kpsThreshold <= 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = true; // Load Model from Here
// Load Model from Here
TOP_K = 100;
SEG_CHANNELS = 32;
PROBABILITY_THRESHOLD = _modelConfig.detectionScoreThreshold;
NMS_THRESHOLD = _modelConfig.modelMNSThreshold;
SEGMENTATION_THRESHOLD = 0.5f;
SEG_H = 160;
SEG_W = 160;
NUM_KPS = _modelConfig.numKPS;
KPS_THRESHOLD = _modelConfig.kpsThreshold;
SEG_CHANNELS = 32; // For segmentation
std::string _modelName = modelName;
if (_modelName.empty()) {
_modelName = "train_last";
}
std::string modelFullName = _modelName + ".onnx";
if (!m_trtEngine) {
// Fixed batch size of 1 for this model
m_options.optBatchSize = _modelConfig.gpuOptBatchSize;
m_options.maxBatchSize = _modelConfig.gpuMaxBatchSize;
m_options.deviceIndex = _modelConfig.gpuDeviceIndex;
m_options.maxInputHeight = _modelConfig.maxInputHeight;
m_options.minInputHeight = _modelConfig.minInputHeight;
m_options.optInputHeight = _modelConfig.optInputHeight;
m_options.maxInputWidth = _modelConfig.maxInputWidth;
m_options.minInputWidth = _modelConfig.minInputWidth;
m_options.optInputWidth = _modelConfig.optInputWidth;
m_options.engineFileDir = _modelFolder;
// Use FP16 or FP32 precision based on the input flag
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
// Create the TensorRT inference engine
m_trtEngine = std::make_unique<Engine<float>>(m_options);
}
// 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0)_modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0)_modelConfig.inpWidth = inputShape[1];
}
}
else {// This is old version of model zip file
_modelFilePath = CreateFilePath(_modelFolder, modelFullName);
_classFilePath = CreateFilePath(_modelFolder, className);
std::ifstream isValidFileName(_classFilePath);
if (!isValidFileName)
{
this->_logger.LogDebug("TENSORRTSEG::Initialize. Load classes from string", _classFilePath, __FILE__, __LINE__);
LoadClassesFromString();
}
else {
this->_logger.LogDebug("TENSORRTSEG::Initialize. Load classes from file", _classFilePath, __FILE__, __LINE__);
LoadClassesFromFile();
}
}
// 1. Load labelMap and engine
labelMap.clear();
if (!_classes.empty())
labelMap = VectorToCommaSeparatedString(_classes);
// Load the TensorRT engine file
if (this->_loadEngineOnCreation) {
auto succ = m_trtEngine->buildLoadNetwork(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
if (!succ) {
const std::string errMsg = "Error: Unable to load TensorRT engine weights into memory. " + _modelFilePath;
this->_logger.LogError("TENSORRTSEG::Initialize", errMsg, __FILE__, __LINE__);
_modelLoadValid = false;
return false;
}
}
_modelLoadValid = true;
_isInitialized = true;
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::LoadModelFromFolder", e.what(), __FILE__, __LINE__);
return false;
}
}
bool TENSORRTSEG::Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
try {
const bool engineAlreadyLoaded = _modelLoadValid && _isInitialized && m_trtEngine != nullptr;
_modelLoadValid = false;
bool result = ANSODBase::Initialize(licenseKey, modelConfig, modelZipFilePath, modelZipPassword, labelMap);
if (!result) return false;
// Parsing for YOLO only here
_modelConfig = modelConfig;
_modelConfig.detectionType = ANSCENTER::DetectionType::SEGMENTATION;
_modelConfig.modelType = ModelType::RTSEG;;
_modelConfig.inpHeight = 640;
_modelConfig.inpWidth = 640;
if (_modelConfig.numKPS <= 0 || _modelConfig.numKPS > 133) // 133 = COCO wholebody max
_modelConfig.numKPS = 17;
_modelConfig.precisionType = PrecisionType::FP32; // Default to FP16 for TensorRT models
if (_modelConfig.modelMNSThreshold < 0.2)
_modelConfig.modelMNSThreshold = 0.5;
if (_modelConfig.modelConfThreshold < 0.2)
_modelConfig.modelConfThreshold = 0.5;
if (_modelConfig.kpsThreshold == 0)_modelConfig.kpsThreshold = 0.5; // If not define
_fp16 = true; // Load Model from Here
// Load Model from Here
TOP_K = 100;
SEG_CHANNELS = 32;
PROBABILITY_THRESHOLD = _modelConfig.detectionScoreThreshold;
NMS_THRESHOLD = _modelConfig.modelMNSThreshold;
SEGMENTATION_THRESHOLD = 0.5f;
SEG_H = 160;
SEG_W = 160;
NUM_KPS = _modelConfig.numKPS;
KPS_THRESHOLD = _modelConfig.kpsThreshold;
SEG_CHANNELS = 32; // For segmentation
if (!m_trtEngine) {
// Fixed batch size of 1 for this model
m_options.optBatchSize = _modelConfig.gpuOptBatchSize;
m_options.maxBatchSize = _modelConfig.gpuMaxBatchSize;
m_options.deviceIndex = _modelConfig.gpuDeviceIndex;
m_options.maxInputHeight = _modelConfig.maxInputHeight;
m_options.minInputHeight = _modelConfig.minInputHeight;
m_options.optInputHeight = _modelConfig.optInputHeight;
m_options.maxInputWidth = _modelConfig.maxInputWidth;
m_options.minInputWidth = _modelConfig.minInputWidth;
m_options.optInputWidth = _modelConfig.optInputWidth;
m_options.engineFileDir = _modelFolder;
// Use FP16 or FP32 precision based on the input flag
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
// Create the TensorRT inference engine
m_trtEngine = std::make_unique<Engine<float>>(m_options);
}
// 0. Check if the configuration file exist
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0)_modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0)_modelConfig.inpWidth = inputShape[1];
}
}
else {// This is old version of model zip file
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValidFileName(_classFilePath);
if (!isValidFileName)
{
this->_logger.LogDebug("TENSORRTSEG::Initialize. Load classes from string", _classFilePath, __FILE__, __LINE__);
LoadClassesFromString();
}
else {
this->_logger.LogDebug("TENSORRTSEG::Initialize. Load classes from file", _classFilePath, __FILE__, __LINE__);
LoadClassesFromFile();
}
}
// 1. Load labelMap and engine
labelMap.clear();
if (!_classes.empty())
labelMap = VectorToCommaSeparatedString(_classes);
// Load the TensorRT engine file
if (this->_loadEngineOnCreation && !engineAlreadyLoaded) {
auto succ = m_trtEngine->buildLoadNetwork(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
if (!succ) {
const std::string errMsg = "Error: Unable to load TensorRT engine weights into memory. " + _modelFilePath;
this->_logger.LogError("TENSORRTSEG::Initialize", errMsg, __FILE__, __LINE__);
_modelLoadValid = false;
return false;
}
}
_modelLoadValid = true;
_isInitialized = true;
return true;
}
catch (std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::Initialize", e.what(), __FILE__, __LINE__);
return false;
}
}
std::vector<Object> TENSORRTSEG::RunInference(const cv::Mat& inputImgBGR) {
return RunInference(inputImgBGR, "");
}
std::vector<Object> TENSORRTSEG::RunInference(const cv::Mat& inputImgBGR,const std::string& camera_id)
{
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_modelLoadValid) {
_logger.LogError("TENSORRTSEG::RunInference",
"Cannot load TensorRT model", __FILE__, __LINE__);
return {};
}
if (!_licenseValid) {
_logger.LogError("TENSORRTSEG::RunInference",
"Invalid license", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
_logger.LogError("TENSORRTSEG::RunInference",
"Model not initialized", __FILE__, __LINE__);
return {};
}
if (inputImgBGR.empty() || inputImgBGR.cols < 10 || inputImgBGR.rows < 10) {
return {};
}
}
try {
return DetectObjects(inputImgBGR, camera_id);
}
catch (const std::exception& e) {
_logger.LogFatal("TENSORRTSEG::RunInference", e.what(), __FILE__, __LINE__);
return {};
}
}
TENSORRTSEG::~TENSORRTSEG() {
try {
Destroy();
}
catch (std::exception& e) {
this->_logger.LogError("TENSORRTSEG::~TENSORRTSEG()", e.what(), __FILE__, __LINE__);
}
}
bool TENSORRTSEG::Destroy() {
try {
m_trtEngine.reset(); // Releases the current engine and sets m_trtEngine to nullptr.
return true;
}
catch (std::exception& e) {
this->_logger.LogError("TENSORRTSEG::~TENSORRTSEG()", e.what(), __FILE__, __LINE__);
return false;
}
}
// private
std::vector<cv::Point2f> TENSORRTSEG::maskToPolygon(const cv::Mat& binaryMask,const cv::Rect& boundingBox,float simplificationEpsilon,int minContourArea)
{
std::vector<cv::Point2f> polygon;
try {
// Validate input
if (binaryMask.empty() || binaryMask.type() != CV_8UC1) {
return polygon;
}
// Extract region of interest from mask
cv::Rect roi = boundingBox & cv::Rect(0, 0, binaryMask.cols, binaryMask.rows);
if (roi.area() <= 0) {
return polygon;
}
cv::Mat maskROI = binaryMask(roi);
// Find contours in the mask
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(maskROI.clone(), contours, hierarchy,
cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
if (contours.empty()) {
return polygon;
}
// Find the largest contour (main object)
int largestIdx = 0;
double largestArea = 0.0;
for (size_t i = 0; i < contours.size(); ++i) {
double area = cv::contourArea(contours[i]);
if (area > largestArea && area >= minContourArea) {
largestArea = area;
largestIdx = static_cast<int>(i);
}
}
if (largestArea < minContourArea) {
return polygon;
}
// Simplify the contour to reduce number of points
std::vector<cv::Point> simplifiedContour;
cv::approxPolyDP(contours[largestIdx], simplifiedContour,
simplificationEpsilon, true);
// Convert to Point2f and offset by ROI position
polygon.reserve(simplifiedContour.size());
for (const auto& pt : simplifiedContour) {
polygon.emplace_back(
static_cast<float>(pt.x + roi.x),
static_cast<float>(pt.y + roi.y)
);
}
return polygon;
}
catch (const cv::Exception& e) {
// Log error if logger available
polygon.clear();
return polygon;
}
}
std::vector<Object> TENSORRTSEG::DetectObjects(const cv::Mat& inputImage, const std::string& camera_id) {
try {
// --- 1. Set GPU device context ---
if (m_trtEngine) {
m_trtEngine->setDeviceContext();
}
// --- 1b. CUDA context health check ---
if (!m_nv12Helper.isCudaContextHealthy(_logger, "TENSORRTSEG")) {
return {};
}
// --- 2. Preprocess under lock ---
ImageMetadata meta;
std::vector<std::vector<cv::cuda::GpuMat>> input;
bool usedNV12 = false;
float bgrFullResScaleX = 1.0f, bgrFullResScaleY = 1.0f;
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
const int inferenceGpu = m_trtEngine ? m_trtEngine->getPreferredDeviceIndex() : 0;
const auto& inputDims = m_trtEngine->getInputDims();
const int inputW = inputDims[0].d[2];
const int inputH = inputDims[0].d[1];
auto nv12 = m_nv12Helper.tryNV12(inputImage, inferenceGpu, inputW, inputH,
NV12PreprocessHelper::defaultYOLOLauncher(),
_logger, "TENSORRTSEG");
if (nv12.succeeded) {
meta.imgWidth = nv12.metaWidth;
meta.imgHeight = nv12.metaHeight;
meta.ratio = nv12.ratio;
input = {{ std::move(nv12.gpuRGB) }};
usedNV12 = true;
}
else if (nv12.useBgrFullRes) {
input = Preprocess(nv12.bgrFullResImg, meta);
usedNV12 = !input.empty();
bgrFullResScaleX = nv12.bgrFullResScaleX;
bgrFullResScaleY = nv12.bgrFullResScaleY;
}
if (input.empty()) {
input = Preprocess(inputImage, meta);
}
m_nv12Helper.tickInference();
}
if (input.empty()) return {};
// Phase 2: Inference — mutex released; pool dispatches to idle GPU slot
std::vector<std::vector<std::vector<float>>> featureVectors;
auto succ = m_trtEngine->runInference(input, featureVectors);
if (!succ) {
this->_logger.LogError("TENSORRTSEG::DetectObjects", "Error running inference", __FILE__, __LINE__);
return {};
}
// Phase 3: Postprocess under lock
std::lock_guard<std::recursive_mutex> lock(_mutex);
std::vector<std::vector<float>> featureVector;
Engine<float>::transformOutput(featureVectors, featureVector);
auto ret = PostProcessSegmentation(featureVector, camera_id, meta);
// --- 4b. Rescale coords from full-res to display-res ---
if (bgrFullResScaleX != 1.0f || bgrFullResScaleY != 1.0f) {
for (auto& obj : ret) {
obj.box.x = static_cast<int>(obj.box.x * bgrFullResScaleX);
obj.box.y = static_cast<int>(obj.box.y * bgrFullResScaleY);
obj.box.width = static_cast<int>(obj.box.width * bgrFullResScaleX);
obj.box.height = static_cast<int>(obj.box.height * bgrFullResScaleY);
for (auto& pt : obj.polygon) {
pt.x *= bgrFullResScaleX;
pt.y *= bgrFullResScaleY;
}
}
}
if (_trackerEnabled) {
ret = ApplyTracking(ret, camera_id);
if (_stabilizationEnabled) ret = StabilizeDetections(ret, camera_id);
}
return ret;
}
catch (std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::DetectObjects", e.what(), __FILE__, __LINE__);
return {};
}
}
std::vector<std::vector<cv::cuda::GpuMat>> TENSORRTSEG::Preprocess(const cv::Mat& inputImage, ImageMetadata& outMeta) {
try {
if (!_licenseValid) {
this->_logger.LogFatal("TENSORRTSEG::Preprocess", "Invalid license", __FILE__, __LINE__);
return {};
}
// Get model input dimensions
const auto& inputDims = m_trtEngine->getInputDims();
const int inputH = inputDims[0].d[1];
const int inputW = inputDims[0].d[2];
// Upload the image to GPU memory
cv::cuda::Stream stream;
cv::cuda::GpuMat img;
if (inputImage.channels() == 1) {
// Convert grayscale to 3-channel BGR before uploading
cv::Mat img3Channel;
cv::cvtColor(inputImage, img3Channel, cv::COLOR_GRAY2BGR);
img.upload(img3Channel, stream);
}
else {
img.upload(inputImage, stream);
}
// Convert to RGB
cv::cuda::GpuMat imgRGB;
cv::cuda::cvtColor(img, imgRGB, cv::COLOR_BGR2RGB, 0, stream);
stream.waitForCompletion();
// Set image size parameters
outMeta.imgHeight = imgRGB.rows;
outMeta.imgWidth = imgRGB.cols;
if (outMeta.imgHeight > 0 && outMeta.imgWidth > 0) {
outMeta.ratio = 1.f / std::min(inputDims[0].d[2] / static_cast<float>(imgRGB.cols),
inputDims[0].d[1] / static_cast<float>(imgRGB.rows));
cv::cuda::GpuMat resized = imgRGB;
// Resize to the model's expected input size while maintaining aspect ratio with padding
if (resized.rows != inputDims[0].d[1] || resized.cols != inputDims[0].d[2]) {
resized = Engine<float>::resizeKeepAspectRatioPadRightBottom(imgRGB, inputDims[0].d[1], inputDims[0].d[2]);
}
// Convert to format expected by our inference engine
std::vector<cv::cuda::GpuMat> input{ std::move(resized) };
std::vector<std::vector<cv::cuda::GpuMat>> inputs{ std::move(input) };
return inputs;
}
else {
this->_logger.LogFatal("TENSORRTCL::Preprocess",
"Image height or width is zero after processing (Width: " + std::to_string(outMeta.imgWidth) +
", Height: " + std::to_string(outMeta.imgHeight) + ")",
__FILE__, __LINE__);
return {};
}
}
catch (const std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::Preprocess", e.what(), __FILE__, __LINE__);
return {};
}
}
std::vector<Object> TENSORRTSEG::PostProcessSegmentation(std::vector<std::vector<float>>& featureVectors, const std::string& camera_id, const ImageMetadata& meta) {
try {
if (!_licenseValid) {
this->_logger.LogFatal("TENSORRTSEG::PostProcessSegmentation", "Invalid license", __FILE__, __LINE__);
std::vector<Object> result;
result.clear();
return result;
}
const auto& outputDims = m_trtEngine->getOutputDims();
int numChannels = outputDims[0].d[1];
int numAnchors = outputDims[0].d[2];
const auto numClasses = numChannels - SEG_CHANNELS - 4;
// Ensure the output lengths are correct
if (featureVectors[0].size() == static_cast<size_t>(numChannels) * numAnchors &&
featureVectors[1].size() == static_cast<size_t>(SEG_CHANNELS) * SEG_H * SEG_W) {
cv::Mat output = cv::Mat(numChannels, numAnchors, CV_32F, featureVectors[0].data());
output = output.t();
cv::Mat protos = cv::Mat(SEG_CHANNELS, SEG_H * SEG_W, CV_32F, featureVectors[1].data());
std::vector<int> labels;
std::vector<float> scores;
std::vector<cv::Rect> bboxes;
std::vector<cv::Mat> maskConfs;
std::vector<int> indices;
// Object the bounding boxes and class labels
for (int i = 0; i < numAnchors; i++) {
auto rowPtr = output.row(i).ptr<float>();
auto bboxesPtr = rowPtr;
auto scoresPtr = rowPtr + 4;
auto maskConfsPtr = rowPtr + 4 + numClasses;
auto maxSPtr = std::max_element(scoresPtr, scoresPtr + numClasses);
float score = *maxSPtr;
if (score > this->_modelConfig.detectionScoreThreshold)
{
float x = *bboxesPtr++;
float y = *bboxesPtr++;
float w = *bboxesPtr++;
float h = *bboxesPtr;
float x0 = std::clamp((x - 0.5f * w) * meta.ratio, 0.f, meta.imgWidth);
float y0 = std::clamp((y - 0.5f * h) * meta.ratio, 0.f, meta.imgHeight);
float x1 = std::clamp((x + 0.5f * w) * meta.ratio, 0.f, meta.imgWidth);
float y1 = std::clamp((y + 0.5f * h) * meta.ratio, 0.f, meta.imgHeight);
int label = maxSPtr - scoresPtr;
cv::Rect_<float> bbox;
bbox.x = x0;
bbox.y = y0;
bbox.width = x1 - x0;
bbox.height = y1 - y0;
bbox.x = std::max(0.f, bbox.x);
bbox.y = std::max(0.f, bbox.y);
bbox.width = std::min(meta.imgWidth - bbox.x, bbox.width);
bbox.height = std::min(meta.imgHeight - bbox.y, bbox.height);
cv::Mat maskConf = cv::Mat(1, SEG_CHANNELS, CV_32F, maskConfsPtr);
bboxes.push_back(bbox);
labels.push_back(label);
scores.push_back(score);
maskConfs.push_back(maskConf);
}
}
cv::dnn::NMSBoxesBatched(bboxes, scores, labels, PROBABILITY_THRESHOLD, NMS_THRESHOLD, indices);
cv::Mat masks;
int classNameSize = static_cast<int>(_classes.size());
std::vector<Object> objs;
for (auto& i : indices) {
if (scores[i] > _modelConfig.detectionScoreThreshold) {
cv::Rect tmp = bboxes[i];
Object obj;
obj.classId = labels[i];
if (!_classes.empty()) {
if (obj.classId < classNameSize) {
obj.className = _classes[obj.classId];
}
else {
obj.className = _classes[classNameSize - 1]; // Use last valid class name if out of range
}
}
else {
obj.className = "Unknown"; // Fallback if _classes is empty
}
obj.box = tmp;
obj.confidence = scores[i];
obj.className = _classes[labels[i]];
masks.push_back(maskConfs[i]);
objs.push_back(obj);
}
}
if (!masks.empty()) {
cv::Mat matmulRes = (masks * protos).t();
cv::Mat maskMat = matmulRes.reshape(indices.size(), { _modelConfig.inpWidth, _modelConfig.inpHeight });
std::vector<cv::Mat> maskChannels;
cv::split(maskMat, maskChannels);
const auto inputDims = m_trtEngine->getInputDims();
cv::Rect roi;
if (meta.imgHeight > meta.imgWidth) {
roi = cv::Rect(0, 0, _modelConfig.inpWidth * meta.imgWidth / meta.imgHeight, _modelConfig.inpHeight);
}
else {
roi = cv::Rect(0, 0, _modelConfig.inpWidth, _modelConfig.inpHeight * meta.imgHeight / meta.imgWidth);
}
for (size_t i = 0; i < indices.size(); i++)
{
cv::Mat dest, mask;
cv::exp(-maskChannels[i], dest);
dest = 1.0 / (1.0 + dest);
dest = dest(roi);
objs[i].cameraId = camera_id;
cv::resize(
dest,
mask,
cv::Size(static_cast<int>(meta.imgWidth), static_cast<int>(meta.imgHeight)),
cv::INTER_LINEAR
);
objs[i].mask = mask(objs[i].box) > _modelConfig.modelConfThreshold;// Need to check segmentation
objs[i].polygon = maskToPolygon(objs[i].mask, objs[i].box, 2.0f, 10);
// Alternative: Get multiple polygons if needed
// auto polygons = maskToPolygons(finalMask, result.box, 2.0f, 10, 3);
// result.polygon = polygons.empty() ? std::vector<cv::Point2f>() : polygons[0];
// Validate polygon
if (objs[i].polygon.size() < 3) {
// Fallback to bounding box if polygon extraction failed
objs[i].polygon = {
cv::Point2f(objs[i].box.x, objs[i].box.y),
cv::Point2f(objs[i].box.x + objs[i].box.width, objs[i].box.y),
cv::Point2f(objs[i].box.x + objs[i].box.width, objs[i].box.y + objs[i].box.height),
cv::Point2f(objs[i].box.x, objs[i].box.y + objs[i].box.height)
};
}
}
}
//EnqueueDetection(objs, camera_id);
return objs;
}
else {
return std::vector<Object>();
}
}
catch (std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::PostProcessSegmentation", e.what(), __FILE__, __LINE__);
std::vector<Object>result;
result.clear();
return result;
}
}
std::vector<std::vector<Object>> TENSORRTSEG::DetectObjectsBatch(const std::vector<cv::Mat>& inputImages, const std::string& camera_id) {
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (inputImages.empty()) {
_logger.LogFatal("TENSORRTSEG::DetectObjectsBatch", "Empty input images vector", __FILE__, __LINE__);
return {};
}
}
// Auto-split if batch exceeds engine capacity
const int maxBatch = m_options.maxBatchSize > 0 ? m_options.maxBatchSize : 1;
if (static_cast<int>(inputImages.size()) > maxBatch) {
const size_t numImages = inputImages.size();
std::vector<std::vector<Object>> allResults;
allResults.reserve(numImages);
// Process chunks sequentially to avoid GPU contention on the same engine
for (size_t start = 0; start < numImages; start += static_cast<size_t>(maxBatch)) {
const size_t end = std::min(start + static_cast<size_t>(maxBatch), numImages);
std::vector<cv::Mat> chunk(inputImages.begin() + start, inputImages.begin() + end);
auto chunkResults = DetectObjectsBatch(chunk, camera_id);
if (chunkResults.size() == chunk.size()) {
for (auto& r : chunkResults) allResults.push_back(std::move(r));
}
else {
_logger.LogError("TENSORRTSEG::DetectObjectsBatch",
"Chunk returned " + std::to_string(chunkResults.size()) +
" results, expected " + std::to_string(chunk.size()) +
". Padding with empty results.", __FILE__, __LINE__);
for (auto& r : chunkResults) allResults.push_back(std::move(r));
for (size_t pad = chunkResults.size(); pad < chunk.size(); ++pad) {
allResults.push_back({});
}
}
}
return allResults;
}
_logger.LogDebug("TENSORRTSEG::DetectObjectsBatch",
"Processing batch of " + std::to_string(inputImages.size()) + " images",
__FILE__, __LINE__);
// Phase 1: Preprocess under brief lock
BatchMetadata metadata;
std::vector<std::vector<cv::cuda::GpuMat>> inputs;
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
inputs = PreprocessBatch(inputImages, metadata);
}
if (inputs.empty() || inputs[0].empty()) {
_logger.LogFatal("TENSORRTSEG::DetectObjectsBatch", "Preprocessing failed", __FILE__, __LINE__);
return {};
}
// Phase 2: Inference -- mutex released; pool dispatches to idle GPU slot
std::vector<std::vector<std::vector<float>>> featureVectors;
auto succ = m_trtEngine->runInference(inputs, featureVectors);
if (!succ) {
_logger.LogError("TENSORRTSEG::DetectObjectsBatch", "Error running inference", __FILE__, __LINE__);
return {};
}
// Phase 3: Parallel postprocessing -- each image is independent
const size_t numBatch = featureVectors.size();
std::vector<std::vector<Object>> batchDetections(numBatch);
std::vector<std::future<std::vector<Object>>> postFutures;
postFutures.reserve(numBatch);
for (size_t batchIdx = 0; batchIdx < numBatch; ++batchIdx) {
const auto& batchOutput = featureVectors[batchIdx];
std::vector<std::vector<float>> fv =
batchOutput.empty() ? std::vector<std::vector<float>>{} : batchOutput;
postFutures.push_back(std::async(std::launch::async,
[this, fv = std::move(fv), cid = camera_id,
idx = batchIdx, &metadata]() mutable {
return PostProcessSegmentationBatch(fv, cid, idx, metadata);
}));
}
for (size_t i = 0; i < numBatch; ++i)
batchDetections[i] = postFutures[i].get();
if (_trackerEnabled) {
for (auto& dets : batchDetections) {
if (!dets.empty()) {
dets = ApplyTracking(dets, camera_id);
if (_stabilizationEnabled) dets = StabilizeDetections(dets, camera_id);
}
}
}
_logger.LogDebug("TENSORRTSEG::DetectObjectsBatch",
"Batch processing complete. Images: " + std::to_string(numBatch),
__FILE__, __LINE__);
return batchDetections;
}
std::vector<std::vector<cv::cuda::GpuMat>> TENSORRTSEG::PreprocessBatch(const std::vector<cv::Mat>& inputImages, BatchMetadata& outMetadata) {
try {
if (!_licenseValid) {
_logger.LogFatal("TENSORRTSEG::PreprocessBatch", "Invalid license", __FILE__, __LINE__);
return {};
}
const auto& inputDims = m_trtEngine->getInputDims();
const int inputH = inputDims[0].d[1];
const int inputW = inputDims[0].d[2];
// Store original image dimensions for each image in batch
outMetadata.imgHeights.resize(inputImages.size());
outMetadata.imgWidths.resize(inputImages.size());
outMetadata.ratios.resize(inputImages.size());
std::vector<cv::cuda::GpuMat> batchProcessed;
batchProcessed.reserve(inputImages.size());
cv::cuda::Stream stream;
// Process each image
for (size_t i = 0; i < inputImages.size(); ++i) {
const auto& inputImage = inputImages[i];
if (inputImage.empty()) {
_logger.LogFatal("TENSORRTSEG::PreprocessBatch",
"Empty input image at index " + std::to_string(i), __FILE__, __LINE__);
return {};
}
// Upload to GPU
cv::cuda::GpuMat img;
// Convert grayscale to BGR if needed
if (inputImage.channels() == 1) {
cv::Mat img3Channel;
cv::cvtColor(inputImage, img3Channel, cv::COLOR_GRAY2BGR);
img.upload(img3Channel, stream);
}
else {
img.upload(inputImage, stream);
}
// Convert to RGB
cv::cuda::GpuMat imgRGB;
cv::cuda::cvtColor(img, imgRGB, cv::COLOR_BGR2RGB, 0, stream);
// Store original dimensions
outMetadata.imgHeights[i] = imgRGB.rows;
outMetadata.imgWidths[i] = imgRGB.cols;
if (outMetadata.imgHeights[i] <= 0 || outMetadata.imgWidths[i] <= 0) {
_logger.LogFatal("TENSORRTSEG::PreprocessBatch",
"Image " + std::to_string(i) + " has invalid dimensions (Width: " +
std::to_string(outMetadata.imgWidths[i]) + ", Height: " +
std::to_string(outMetadata.imgHeights[i]) + ")",
__FILE__, __LINE__);
return {};
}
// Calculate ratio for this image
outMetadata.ratios[i] = 1.f / std::min(inputW / static_cast<float>(imgRGB.cols),
inputH / static_cast<float>(imgRGB.rows));
// Resize with padding
cv::cuda::GpuMat resized = imgRGB;
if (resized.rows != inputH || resized.cols != inputW) {
resized = Engine<float>::resizeKeepAspectRatioPadRightBottom(imgRGB, inputH, inputW);
}
batchProcessed.push_back(std::move(resized));
}
stream.waitForCompletion();
// Return as required format
std::vector<std::vector<cv::cuda::GpuMat>> inputs;
inputs.push_back(std::move(batchProcessed));
return inputs;
}
catch (const std::exception& e) {
_logger.LogFatal("TENSORRTSEG::PreprocessBatch", e.what(), __FILE__, __LINE__);
return {};
}
}
std::vector<Object> TENSORRTSEG::PostProcessSegmentationBatch(std::vector<std::vector<float>>& featureVectors, const std::string& camera_id, size_t batchIdx, const BatchMetadata& metadata) {
try {
if (!_licenseValid) {
_logger.LogFatal("TENSORRTSEG::PostProcessSegmentationBatch", "Invalid license", __FILE__, __LINE__);
return {};
}
const auto& outputDims = m_trtEngine->getOutputDims();
int numChannels = outputDims[0].d[1];
int numAnchors = outputDims[0].d[2];
const auto numClasses = numChannels - SEG_CHANNELS - 4;
// Get batch-specific dimensions
float ratio = metadata.ratios[batchIdx];
float imgWidth = static_cast<float>(metadata.imgWidths[batchIdx]);
float imgHeight = static_cast<float>(metadata.imgHeights[batchIdx]);
// Ensure the output lengths are correct
if (featureVectors.size() < 2) {
_logger.LogError("TENSORRTSEG::PostProcessSegmentationBatch",
"Invalid feature vectors size: " + std::to_string(featureVectors.size()),
__FILE__, __LINE__);
return {};
}
if (featureVectors[0].size() != static_cast<size_t>(numChannels) * numAnchors) {
_logger.LogError("TENSORRTSEG::PostProcessSegmentationBatch",
"Detection output size mismatch", __FILE__, __LINE__);
return {};
}
if (featureVectors[1].size() != static_cast<size_t>(SEG_CHANNELS) * SEG_H * SEG_W) {
_logger.LogError("TENSORRTSEG::PostProcessSegmentationBatch",
"Segmentation mask size mismatch", __FILE__, __LINE__);
return {};
}
cv::Mat output = cv::Mat(numChannels, numAnchors, CV_32F, featureVectors[0].data());
output = output.t();
cv::Mat protos = cv::Mat(SEG_CHANNELS, SEG_H * SEG_W, CV_32F, featureVectors[1].data());
std::vector<int> labels;
std::vector<float> scores;
std::vector<cv::Rect> bboxes;
std::vector<cv::Mat> maskConfs;
std::vector<int> indices;
// Extract bounding boxes and class labels
for (int i = 0; i < numAnchors; i++) {
auto rowPtr = output.row(i).ptr<float>();
auto bboxesPtr = rowPtr;
auto scoresPtr = rowPtr + 4;
auto maskConfsPtr = rowPtr + 4 + numClasses;
auto maxSPtr = std::max_element(scoresPtr, scoresPtr + numClasses);
float score = *maxSPtr;
if (score > _modelConfig.detectionScoreThreshold) {
float x = *bboxesPtr++;
float y = *bboxesPtr++;
float w = *bboxesPtr++;
float h = *bboxesPtr;
// Use batch-specific ratio and dimensions
float x0 = std::clamp((x - 0.5f * w) * ratio, 0.f, imgWidth);
float y0 = std::clamp((y - 0.5f * h) * ratio, 0.f, imgHeight);
float x1 = std::clamp((x + 0.5f * w) * ratio, 0.f, imgWidth);
float y1 = std::clamp((y + 0.5f * h) * ratio, 0.f, imgHeight);
int label = maxSPtr - scoresPtr;
cv::Rect_<float> bbox;
bbox.x = x0;
bbox.y = y0;
bbox.width = x1 - x0;
bbox.height = y1 - y0;
// Clamp to image boundaries
bbox.x = std::max(0.f, bbox.x);
bbox.y = std::max(0.f, bbox.y);
bbox.width = std::min(imgWidth - bbox.x, bbox.width);
bbox.height = std::min(imgHeight - bbox.y, bbox.height);
cv::Mat maskConf = cv::Mat(1, SEG_CHANNELS, CV_32F, maskConfsPtr);
bboxes.push_back(bbox);
labels.push_back(label);
scores.push_back(score);
maskConfs.push_back(maskConf);
}
}
// Run NMS
cv::dnn::NMSBoxesBatched(bboxes, scores, labels, PROBABILITY_THRESHOLD, NMS_THRESHOLD, indices);
cv::Mat masks;
int classNameSize = static_cast<int>(_classes.size());
std::vector<Object> objs;
for (auto& i : indices) {
if (scores[i] > _modelConfig.detectionScoreThreshold) {
cv::Rect tmp = bboxes[i];
Object obj;
obj.classId = labels[i];
if (!_classes.empty()) {
if (obj.classId < classNameSize) {
obj.className = _classes[obj.classId];
}
else {
obj.className = _classes[classNameSize - 1];
}
}
else {
obj.className = "Unknown";
}
obj.box = tmp;
obj.confidence = scores[i];
masks.push_back(maskConfs[i]);
objs.push_back(obj);
}
}
// Process segmentation masks
if (!masks.empty()) {
cv::Mat matmulRes = (masks * protos).t();
cv::Mat maskMat = matmulRes.reshape(indices.size(), { _modelConfig.inpWidth, _modelConfig.inpHeight });
std::vector<cv::Mat> maskChannels;
cv::split(maskMat, maskChannels);
cv::Rect roi;
if (imgHeight > imgWidth) {
roi = cv::Rect(0, 0, _modelConfig.inpWidth * imgWidth / imgHeight, _modelConfig.inpHeight);
}
else {
roi = cv::Rect(0, 0, _modelConfig.inpWidth, _modelConfig.inpHeight * imgHeight / imgWidth);
}
for (size_t i = 0; i < indices.size(); i++) {
cv::Mat dest, mask;
cv::exp(-maskChannels[i], dest);
dest = 1.0 / (1.0 + dest);
dest = dest(roi);
objs[i].cameraId = camera_id;
cv::resize(
dest,
mask,
cv::Size(static_cast<int>(imgWidth), static_cast<int>(imgHeight)),
cv::INTER_LINEAR
);
objs[i].mask = mask(objs[i].box) > _modelConfig.modelConfThreshold;
objs[i].polygon = maskToPolygon(objs[i].mask, objs[i].box, 2.0f, 10);
// Validate polygon
if (objs[i].polygon.size() < 3) {
// Fallback to bounding box if polygon extraction failed
objs[i].polygon = {
cv::Point2f(objs[i].box.x, objs[i].box.y),
cv::Point2f(objs[i].box.x + objs[i].box.width, objs[i].box.y),
cv::Point2f(objs[i].box.x + objs[i].box.width, objs[i].box.y + objs[i].box.height),
cv::Point2f(objs[i].box.x, objs[i].box.y + objs[i].box.height)
};
}
}
}
return objs;
}
catch (std::exception& e) {
_logger.LogFatal("TENSORRTSEG::PostProcessSegmentationBatch", e.what(), __FILE__, __LINE__);
return {};
}
}
std::vector<std::vector<Object>> TENSORRTSEG::RunInferencesBatch(
const std::vector<cv::Mat>& inputs, const std::string& camera_id)
{
{
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_modelLoadValid) {
_logger.LogError("TENSORRTSEG::RunInferencesBatch",
"Model not loaded", __FILE__, __LINE__);
return {};
}
if (!_licenseValid) {
_logger.LogError("TENSORRTSEG::RunInferencesBatch",
"Invalid license", __FILE__, __LINE__);
return {};
}
if (!_isInitialized) {
_logger.LogError("TENSORRTSEG::RunInferencesBatch",
"Engine not initialized", __FILE__, __LINE__);
return {};
}
if (inputs.empty()) return {};
}
try {
return DetectObjectsBatch(inputs, camera_id);
}
catch (const std::exception& e) {
_logger.LogFatal("TENSORRTSEG::RunInferencesBatch", e.what(), __FILE__, __LINE__);
return {};
}
}
}
/*std::vector<Object> TENSORRTSEG::RunInference(const cv::Mat& inputImgBGR, const std::string& camera_id) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!_modelLoadValid) {
this->_logger.LogFatal("TENSORRTSEG::RunInference", "Cannot load the TensorRT model. Please check if it is exist", __FILE__, __LINE__);
std::vector<Object> result;
result.clear();
return result;
}
if (!_licenseValid) {
this->_logger.LogFatal("TENSORRTSEG::RunInference", "Runtime license is not valid or expired. Please contact ANSCENTER", __FILE__, __LINE__);
std::vector<Object> result;
result.clear();
return result;
}
if (!_isInitialized) {
this->_logger.LogFatal("TENSORRTSEG::RunInference", "Model is not initialized", __FILE__, __LINE__);
std::vector<Object> result;
result.clear();
return result;
}
try {
std::vector<Object> result;
if (inputImgBGR.empty()) return result;
if ((inputImgBGR.cols < 10) || (inputImgBGR.rows < 10)) return result;
return DetectObjects(inputImgBGR, camera_id);
}
catch (std::exception& e) {
this->_logger.LogFatal("TENSORRTSEG::RunInference", e.what(), __FILE__, __LINE__);
std::vector<Object> result;
result.clear();
return result;
}
}*/