gpt4 book ai didi

c++ - Tensorflow DrawBoundingBoxes C++

转载 作者:行者123 更新时间:2023-11-30 03:26:53 24 4
gpt4 key购买 nike

我正在使用 C++ 中的 Tensorflow 检测对象。它运行良好,我想绘制方框以获得一些视觉反馈。
有操作 tensorflow::ops::DrawBoundingBoxes 可以让我这样做,但问题是:

  • 我不明白这些框的输入值应该是多少。这是什么意思:

    boxes: 3-D with shape [batch, num_bounding_boxes, 4] containing bounding boxes.
  • 我在任何地方都找不到在 C++ 中使用此操作的示例,就好像此操作不存在一样。

C++ 中是否有使用此操作的示例?这听起来是教程或调试的基本操作。

最佳答案

如果您仍然在这个问题上,我已经使用 OpenCV 基本方法编写了我自己的此操作的实现。它还支持使用相应的类标签为框添加标题。

/** Draw bounding box and add caption to the image.
* Boolean flag _scaled_ shows if the passed coordinates are in relative units (true by default in tensorflow detection)
*/
void drawBoundingBoxOnImage(Mat &image, double yMin, double xMin, double yMax, double xMax, double score, string label, bool scaled=true) {
cv::Point tl, br;
if (scaled) {
tl = cv::Point((int) (xMin * image.cols), (int) (yMin * image.rows));
br = cv::Point((int) (xMax * image.cols), (int) (yMax * image.rows));
} else {
tl = cv::Point((int) xMin, (int) yMin);
br = cv::Point((int) xMax, (int) yMax);
}
cv::rectangle(image, tl, br, cv::Scalar(0, 255, 255), 1);

// Ceiling the score down to 3 decimals (weird!)
float scoreRounded = floorf(score * 1000) / 1000;
string scoreString = to_string(scoreRounded).substr(0, 5);
string caption = label + " (" + scoreString + ")";

// Adding caption of type "LABEL (X.XXX)" to the top-left corner of the bounding box
int fontCoeff = 12;
cv::Point brRect = cv::Point(tl.x + caption.length() * fontCoeff / 1.6, tl.y + fontCoeff);
cv::rectangle(image, tl, brRect, cv::Scalar(0, 255, 255), -1);
cv::Point textCorner = cv::Point(tl.x, tl.y + fontCoeff * 0.9);
cv::putText(image, caption, textCorner, FONT_HERSHEY_SIMPLEX, 0.4, cv::Scalar(255, 0, 0));
}

/** Draw bounding boxes and add captions to the image.
* Box is drawn only if corresponding score is higher than the _threshold_.
*/
void drawBoundingBoxesOnImage(Mat &image,
tensorflow::TTypes<float>::Flat scores,
tensorflow::TTypes<float>::Flat classes,
tensorflow::TTypes<float,3>::Tensor boxes,
map<int, string> labelsMap, double threshold=0.5) {
for (int j = 0; j < scores.size(); j++)
if (scores(j) > threshold)
drawBoundingBoxOnImage(image, boxes(0,j,0), boxes(0,j,1), boxes(0,j,2), boxes(0,j,3), scores(j), labelsMap[classes(j)]);
}

完整的例子是here .

关于c++ - Tensorflow DrawBoundingBoxes C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com