|
| 1 | +#include "DNNDetector.h" |
| 2 | + |
| 3 | +/// |
| 4 | +/// \brief DNNDetector::DNNDetector |
| 5 | +/// \param collectPoints |
| 6 | +/// \param gray |
| 7 | +/// |
| 8 | +DNNDetector::DNNDetector( |
| 9 | + bool collectPoints, |
| 10 | + cv::UMat& colorFrame |
| 11 | + ) |
| 12 | + : |
| 13 | + BaseDetector(collectPoints, colorFrame) |
| 14 | +{ |
| 15 | +} |
| 16 | + |
| 17 | +/// |
| 18 | +/// \brief DNNDetector::~DNNDetector |
| 19 | +/// |
| 20 | +DNNDetector::~DNNDetector(void) |
| 21 | +{ |
| 22 | +} |
| 23 | + |
| 24 | +/// |
| 25 | +/// \brief DNNDetector::Init |
| 26 | +/// \return |
| 27 | +/// |
| 28 | +bool DNNDetector::Init(std::string modelConfiguration, std::string modelBinary) |
| 29 | +{ |
| 30 | + m_net = cv::dnn::readNetFromCaffe(modelConfiguration, modelBinary); |
| 31 | + |
| 32 | + return !m_net.empty(); |
| 33 | +} |
| 34 | + |
| 35 | +/// |
| 36 | +/// \brief DNNDetector::Detect |
| 37 | +/// \param gray |
| 38 | +/// |
| 39 | +void DNNDetector::Detect(cv::UMat& colorFrame) |
| 40 | +{ |
| 41 | + const int inWidth = 300; |
| 42 | + const int inHeight = 300; |
| 43 | + const float WHRatio = inWidth / (float)inHeight; |
| 44 | + const float inScaleFactor = 0.007843f; |
| 45 | + const float meanVal = 127.5; |
| 46 | + const float confidenceThreshold = 0.2f; |
| 47 | + std::string classNames[] = {"background", |
| 48 | + "aeroplane", "bicycle", "bird", "boat", |
| 49 | + "bottle", "bus", "car", "cat", "chair", |
| 50 | + "cow", "diningtable", "dog", "horse", |
| 51 | + "motorbike", "person", "pottedplant", |
| 52 | + "sheep", "sofa", "train", "tvmonitor"}; |
| 53 | + |
| 54 | + cv::Size cropSize; |
| 55 | + if (colorFrame.cols / (float)colorFrame.rows > WHRatio) |
| 56 | + { |
| 57 | + cropSize = cv::Size(cvRound(colorFrame.rows * WHRatio), colorFrame.rows); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + cropSize = cv::Size(colorFrame.cols, cvRound(colorFrame.cols / WHRatio)); |
| 62 | + } |
| 63 | + |
| 64 | + cv::Rect crop(cv::Point((colorFrame.cols - cropSize.width) / 2, (colorFrame.rows - cropSize.height) / 2), cropSize); |
| 65 | + |
| 66 | + cv::Mat inputBlob = cv::dnn::blobFromImage(colorFrame.getMat(cv::ACCESS_READ), inScaleFactor, cv::Size(inWidth, inHeight), meanVal, false); //Convert Mat to batch of images |
| 67 | + |
| 68 | + m_net.setInput(inputBlob, "data"); //set the network input |
| 69 | + |
| 70 | + cv::Mat detection = m_net.forward("detection_out"); //compute output |
| 71 | + |
| 72 | + std::vector<double> layersTimings; |
| 73 | + double freq = cv::getTickFrequency() / 1000; |
| 74 | + double time = m_net.getPerfProfile(layersTimings) / freq; |
| 75 | + |
| 76 | + cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>()); |
| 77 | + |
| 78 | + //cv::Mat frame = colorFrame(crop); |
| 79 | + |
| 80 | + //ss << "FPS: " << 1000/time << " ; time: " << time << " ms"; |
| 81 | + //putText(frame, ss.str(), Point(20,20), 0, 0.5, Scalar(0,0,255)); |
| 82 | + //std::cout << "Inference time, ms: " << time << endl; |
| 83 | + |
| 84 | + m_regions.clear(); |
| 85 | + |
| 86 | + for (int i = 0; i < detectionMat.rows; ++i) |
| 87 | + { |
| 88 | + float confidence = detectionMat.at<float>(i, 2); |
| 89 | + |
| 90 | + if (confidence > confidenceThreshold) |
| 91 | + { |
| 92 | + size_t objectClass = (size_t)(detectionMat.at<float>(i, 1)); |
| 93 | + |
| 94 | + int xLeftBottom = cvRound(detectionMat.at<float>(i, 3) * colorFrame.cols); |
| 95 | + int yLeftBottom = cvRound(detectionMat.at<float>(i, 4) * colorFrame.rows); |
| 96 | + int xRightTop = cvRound(detectionMat.at<float>(i, 5) * colorFrame.cols); |
| 97 | + int yRightTop = cvRound(detectionMat.at<float>(i, 6) * colorFrame.rows); |
| 98 | + |
| 99 | + cv::Rect object((int)xLeftBottom, (int)yLeftBottom, (int)(xRightTop - xLeftBottom), (int)(yRightTop - yLeftBottom)); |
| 100 | + |
| 101 | + m_regions.push_back(object); |
| 102 | + |
| 103 | + //cv::rectangle(frame, object, Scalar(0, 255, 0)); |
| 104 | + std::string label = classNames[objectClass] + ": " + std::to_string(confidence); |
| 105 | + int baseLine = 0; |
| 106 | + cv::Size labelSize = cv::getTextSize(label, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); |
| 107 | + //cv::rectangle(frame, cv::Rect(cv::Point(xLeftBottom, yLeftBottom - labelSize.height), cv::Size(labelSize.width, labelSize.height + baseLine)), cv::Scalar(255, 255, 255), CV_FILLED); |
| 108 | + //cv::putText(frame, label, Point(xLeftBottom, yLeftBottom), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0,0,0)); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments