diff --git a/cpp/OcvYoloDetection/Frame.cpp b/cpp/OcvYoloDetection/Frame.cpp index 52ae67ad..d4b40e69 100644 --- a/cpp/OcvYoloDetection/Frame.cpp +++ b/cpp/OcvYoloDetection/Frame.cpp @@ -26,6 +26,8 @@ #include "Frame.h" +#include + using namespace MPF::COMPONENT; cv::Mat Frame::getDataAsResizedFloat( @@ -43,9 +45,23 @@ cv::Mat Frame::getDataAsResizedFloat( // limited by target x scaleFactor = targetSize.width / static_cast(data.cols); } + + if (scaleFactor * static_cast(data.rows) <= 0.5) { + throw MPFDetectionException(MPF_BAD_FRAME_SIZE, "Unable to resize. Image height (" + + std::to_string(data.rows) + ") too short vs. width (" + std::to_string(data.cols) + ")."); + } + if (scaleFactor * static_cast(data.cols) <= 0.5) { + throw MPFDetectionException(MPF_BAD_FRAME_SIZE, "Unable to resize. Image width (" + + std::to_string(data.cols) + ") too narrow vs. height (" + std::to_string(data.rows) + ")."); + } cv::Mat resizedData; - cv::resize(data, resizedData, cv::Size(), scaleFactor, scaleFactor); + try { + cv::resize(data, resizedData, cv::Size(), scaleFactor, scaleFactor); + } + catch(const std::exception &ex) { + throw MPFDetectionException(MPF_BAD_FRAME_SIZE, ex.what()); + } int leftPadding = (targetSize.width - resizedData.cols) / 2; int topPadding = (targetSize.height - resizedData.rows) / 2; diff --git a/cpp/OcvYoloDetection/triton/TritonInferencer.cpp b/cpp/OcvYoloDetection/triton/TritonInferencer.cpp index 506e12e4..700b111a 100644 --- a/cpp/OcvYoloDetection/triton/TritonInferencer.cpp +++ b/cpp/OcvYoloDetection/triton/TritonInferencer.cpp @@ -238,8 +238,12 @@ void TritonInferencer::infer( shape[0] = size; // get a client from pool - int clientId = acquireClientId(); - TritonClient &client = *clients_[clientId]; + auto releaser = [this](int* ptr) { + releaseClientId(*ptr); + delete ptr; + }; + std::shared_ptr clientId(new int(acquireClientId()), releaser); + TritonClient &client = *clients_[*clientId]; // create blob directly, in client input shm region if appropriate, // with code similar to opencv's blobFromImages @@ -274,14 +278,13 @@ void TritonInferencer::infer( try { std::vector outBlobs; for (auto & i : outputsMeta) { - outBlobs.push_back(clients_[clientId]->getOutput(i)); + outBlobs.push_back(clients_[*clientId]->getOutput(i)); } extractDetectionsCallback(outBlobs, begin, end); } catch (const std::exception &ex) { - eptr = std::current_exception(); + setClientException(std::current_exception()); } - releaseClientId(clientId, eptr); }); } } @@ -304,12 +307,19 @@ int TritonInferencer::acquireClientId() { } -void TritonInferencer::releaseClientId(int clientId, const std::exception_ptr& newClientEptr) { +void TritonInferencer::setClientException(const std::exception_ptr& newClientEptr) { { std::lock_guard lk(freeClientIdsMtx_); if (newClientEptr && !clientEptr_) { clientEptr_ = newClientEptr; } + } +} + + +void TritonInferencer::releaseClientId(int clientId) { + { + std::lock_guard lk(freeClientIdsMtx_); freeClientIds_.insert(clientId); LOG_TRACE("Freeing client[" << clientId << "]"); } diff --git a/cpp/OcvYoloDetection/triton/TritonInferencer.h b/cpp/OcvYoloDetection/triton/TritonInferencer.h index d895b97c..c03cfdc3 100644 --- a/cpp/OcvYoloDetection/triton/TritonInferencer.h +++ b/cpp/OcvYoloDetection/triton/TritonInferencer.h @@ -81,7 +81,9 @@ class TritonInferencer { int acquireClientId(); - void releaseClientId(int clientId, const std::exception_ptr& eptr); + void setClientException(const std::exception_ptr& eptr); + + void releaseClientId(int clientId); void waitTillAllClientsReleased() noexcept;