Skip to content

Commit

Permalink
Catch resize exception and release client lock. (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobble authored Jun 28, 2023
1 parent 310f439 commit 388b0d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
18 changes: 17 additions & 1 deletion cpp/OcvYoloDetection/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "Frame.h"

#include <MPFDetectionException.h>

using namespace MPF::COMPONENT;

cv::Mat Frame::getDataAsResizedFloat(
Expand All @@ -43,9 +45,23 @@ cv::Mat Frame::getDataAsResizedFloat(
// limited by target x
scaleFactor = targetSize.width / static_cast<double>(data.cols);
}

if (scaleFactor * static_cast<double>(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<double>(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;
Expand Down
22 changes: 16 additions & 6 deletions cpp/OcvYoloDetection/triton/TritonInferencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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
Expand Down Expand Up @@ -274,14 +278,13 @@ void TritonInferencer::infer(
try {
std::vector<cv::Mat> 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);
});
}
}
Expand All @@ -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<std::mutex> lk(freeClientIdsMtx_);
if (newClientEptr && !clientEptr_) {
clientEptr_ = newClientEptr;
}
}
}


void TritonInferencer::releaseClientId(int clientId) {
{
std::lock_guard<std::mutex> lk(freeClientIdsMtx_);
freeClientIds_.insert(clientId);
LOG_TRACE("Freeing client[" << clientId << "]");
}
Expand Down
4 changes: 3 additions & 1 deletion cpp/OcvYoloDetection/triton/TritonInferencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 388b0d5

Please sign in to comment.