inline cv::Mat PlotBbox()

in awstreamer/gst_plugins/mxnet/src/common.hpp [166:227]


inline cv::Mat PlotBbox(cv::Mat img, NDArray bboxes, NDArray scores, NDArray labels,
               float thresh, std::vector<std::string> class_names,
               std::map<int, cv::Scalar> colors, bool verbose) {
    int num = bboxes.GetShape()[1];
    std::mt19937 eng;
    std::uniform_real_distribution<float> rng(0, 1);
    float hue = rng(eng);
    bboxes.WaitToRead();
    scores.WaitToRead();
    labels.WaitToRead();
    if (verbose) {
        LOG(INFO) << "Start Ploting with visualize score threshold: " << thresh;
    }
    for (int i = 0; i < num; ++i) {
        float score = scores.At(0, 0, i);
        float label = labels.At(0, 0, i);
        if (score < thresh) continue;
        if (label < 0) continue;

        int cls_id = static_cast<int>(label);
        if (colors.find(cls_id) == colors.end()) {
            // create a new color
            int csize = static_cast<int>(class_names.size());
            if (class_names.size() > 0) {
                float hue = label / csize;
                colors[cls_id] = HSV2BGR(cv::Scalar(hue * 255, 0.75, 0.95));
            } else {
                // generate color for this id
                hue += 0.618033988749895;  // golden ratio
                hue = fmod(hue, 1.0);
                colors[cls_id] = HSV2BGR(cv::Scalar(hue * 255, 0.75, 0.95));
            }
        }

        // draw bounding box
        auto color = colors[cls_id];
        cv::Point pt1(bboxes.At(0, i, 0), bboxes.At(0, i, 1));
        cv::Point pt2(bboxes.At(0, i, 2), bboxes.At(0, i, 3));
        cv::rectangle(img, pt1, pt2, color, 2);

        if (verbose) {
            if (cls_id >= (int)(class_names.size())) {
                LOG(INFO) << "id: " << cls_id << ", scores: " << score;
            } else {
                LOG(INFO) << "id: " << class_names[cls_id] << ", scores: " << score;
            }

        }

        // put text
        std::string txt;
        if ((int)(class_names.size()) > cls_id) {
            txt += class_names[cls_id];
        }
        std::stringstream ss;
        ss << std::fixed << std::setprecision(3) << score;
        txt += " " + ss.str();
        // cv::putText(img, txt, cv::Point(pt1.x, pt1.y - 5), , 0.6, color, 1);
        PutLabel(img, txt, pt1, color);
    }
    return img;
}