\brief OCVDNNDetector::ParseYOLOv5 \param crop \param detections \param tmpRegions
| 513 | /// \param tmpRegions |
| 514 | /// |
| 515 | void OCVDNNDetector::ParseYOLOv5(const cv::Rect& crop, std::vector<cv::Mat>& detections, regions_t& tmpRegions) |
| 516 | { |
| 517 | int rows = detections[0].size[1]; |
| 518 | int dimensions = detections[0].size[2]; |
| 519 | |
| 520 | // yolov5 has an output of shape (batchSize, 25200, 85) (Num classes + box[x,y,w,h] + confidence[c]) |
| 521 | // yolov8 has an output of shape (batchSize, 84, 8400) (Num classes + box[x,y,w,h]) |
| 522 | if (dimensions > rows) // Check if the shape[2] is more than shape[1] (yolov8) |
| 523 | { |
| 524 | rows = detections[0].size[2]; |
| 525 | dimensions = detections[0].size[1]; |
| 526 | |
| 527 | detections[0] = detections[0].reshape(1, dimensions); |
| 528 | cv::transpose(detections[0], detections[0]); |
| 529 | } |
| 530 | float* data = (float*)detections[0].data; |
| 531 | |
| 532 | float x_factor = crop.width / static_cast<float>(m_inWidth); |
| 533 | float y_factor = crop.height / static_cast<float>(m_inHeight); |
| 534 | |
| 535 | for (int i = 0; i < rows; ++i) |
| 536 | { |
| 537 | float confidence = data[4]; |
| 538 | |
| 539 | if (confidence >= m_confidenceThreshold) |
| 540 | { |
| 541 | float* classes_scores = data + 5; |
| 542 | |
| 543 | cv::Mat scores(1, static_cast<int>(m_classNames.size()), CV_32FC1, classes_scores); |
| 544 | cv::Point class_id; |
| 545 | double maxClassScore = 0; |
| 546 | cv::minMaxLoc(scores, 0, &maxClassScore, 0, &class_id); |
| 547 | |
| 548 | if (maxClassScore > m_confidenceThreshold) |
| 549 | { |
| 550 | float x = data[0]; |
| 551 | float y = data[1]; |
| 552 | float w = data[2]; |
| 553 | float h = data[3]; |
| 554 | |
| 555 | int left = cvRound((x - 0.5f * w) * x_factor); |
| 556 | int top = cvRound((y - 0.5f * h) * y_factor); |
| 557 | |
| 558 | int width = cvRound(w * x_factor); |
| 559 | int height = cvRound(h * y_factor); |
| 560 | |
| 561 | if (m_classesWhiteList.empty() || m_classesWhiteList.find(T2T(class_id.x)) != std::end(m_classesWhiteList)) |
| 562 | tmpRegions.emplace_back(cv::Rect(left + crop.x, top + crop.y, width, height), T2T(class_id.x), static_cast<float>(maxClassScore)); |
| 563 | } |
| 564 | } |
| 565 | data += dimensions; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /// |
| 570 | /// \brief OCVDNNDetector::ParseYOLOv8 |