Skip to main content
Version: 3.19.2

Converting RawSample to Context

In this section you will learn how to convert RawSample (old API) to Context (Processing Block API) in C++ and Python. This can be useful if you want to use proven Capturer from old API and some new Processing Blocks like Age Estimator, QAA or Liveness.

pbio::Capturer::Ptr capturer = service->createCapturer("common_capturer_refa_fda_a.xml");

cv::Mat image = cv::imread(input_image_path);
auto ioData = service->createContext();

pbio::RawImage rawImage(image.cols, image.rows, pbio::RawImage::Format::FORMAT_BGR, image.data);

// make face detection
std::vector<pbio::RawSample::Ptr> samples = capturer->capture(rawImage);

auto objects = ioData["objects"];
auto imageContext = ioData["image"];

pbio::context_utils::putImage(imageContext, rawImage);

for (const auto& sample : samples)
{
// converting to Context
objects.push_back(sample->toContext());
}

auto configContext = service->createContext();

configContext["unit_type"] = "AGE_ESTIMATOR";
configContext["@sdk_path"] = sdk_dir;
configContext["ONNXRuntime"]["library_path"] = lib_dir;

pbio::ProcessingBlock block = service->createProcessingBlock(configContext);

block(ioData);

for (const auto& object : ioData["objects"])
{
auto bbox = object["bbox"];
cv::Point topLeft(bbox[0].getDouble() * image.cols, bbox[1].getDouble() * image.rows);
cv::Point bottomRight(bbox[2].getDouble() * image.cols, bbox[3].getDouble() * image.rows);

std::cout << "Bbox coordinates: (" << topLeft.x << ", " << topLeft.y << ") (" << bottomRight.x << ", " << bottomRight.y << ") " << "Age: " << object["age"].getLong() << std::endl;
}