int ImageProcessor::runNeuralNetworkInference()

in awstreamer/gst_plugins/cvmlfilter/ImageProcessor.cpp [122:159]


int ImageProcessor::runNeuralNetworkInference(const cv::Mat &src, cv::Mat &dst) {
    if (cvNet.empty()) {
        GST_WARNING("Neural network not initialized. Forgot to setup configuraton file?");
        return 0;
    }

    double t = (double)cv::getTickCount();
    float w = src.cols;
    float h = src.rows;
    bool swapRB = true;
    bool crop = false;

    // Run inference
    cvNet.setInput(cv::dnn::blobFromImage(src, 1.0, cv::Size(300, 300), cv::Scalar(104, 177, 123), swapRB, crop));
    cv::Mat detection = cvNet.forward();

    // Draw bounding boxes
    cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
    std::vector<std::vector<int>> bboxes;
    for (int i = 0; i < detectionMat.rows; i++) {
        float confidence = detectionMat.at<float>(i, 2);
        if (confidence > 0.5) {
            int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * w);
            int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * h);
            int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * w);
            int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * h);
            std::vector<int> box = {x1, y1, x2, y2};
            bboxes.push_back(box);
            cv::rectangle(dst, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0),2, 4);
        }
    }

    t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
    GST_INFO("Inference took %.3f seconds", t);
    GST_INFO("Detected %lu bounding boxes", bboxes.size());

    return bboxes.size();
}