c++ - Tensorflow DrawBoundingBoxes C++

标签 c++ tensorflow bounding-box

我正在使用 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/

相关文章:

python - pip 错误 "No matching distribution found for perceptilabs"

ios - 如果一个节点被缩放,包含的 scntext 边界框不是太正常,这是否正常?

c++ - boost::optional<T&> vs T*

c++ - 在 GCC/C++ 下访问 Windows 注册表

tensorflow - Keras 使用预训练嵌入初始化大型嵌入层

python - 随后运行多个 tensorflow session

c++ - 使用 strlen() 在堆栈中分配缓冲区

c++ - 在 C++ 和 Armadillo 中将 sqrt(-1) 乘以矩阵

ruby - Prawn :有没有办法让一个盒子里的所有内容垂直对齐?

algorithm - 3D 中的最小边界框算法