Код: Выделить всё
// Prepare containers for detected boxes, confidences, and class IDs
std::vector boxes;
std::vector confidences;
std::vector class_ids;
// Pre-allocate vectors for efficiency
boxes.reserve(cols);
confidences.reserve(cols);
class_ids.reserve(cols);
clock_t start_time = clock();
// Iterate through the rows of the output array
for (int i = 0; i < cols; i++) {
cv::Mat classes_scores = output.col(i).rowRange(4, output.rows);
// Find the class with the maximum score
double maxScore;
cv::Point maxClassLoc;
if (cv::countNonZero(classes_scores) > 0) {
cv::minMaxLoc(classes_scores, nullptr, &maxScore, nullptr, &maxClassLoc);
int maxClassIndex = maxClassLoc.y; // Note: use maxClassLoc.y for row index
// Apply confidence threshold
if (maxScore >= 0.25) {
// Extract bounding box coordinates relative to the image size
float x = output.at(0, i);
float y = output.at(1, i);
float w = output.at(2, i);
float h = output.at(3, i);
// Calculate absolute coordinates
int x_scaled = static_cast((x - 0.5f * w) * scale);
int y_scaled = static_cast((y - 0.5f * h) * scale);
int w_scaled = static_cast(w * scale);
int h_scaled = static_cast(h * scale);
cv::Rect box(x_scaled, y_scaled, w_scaled, h_scaled);
boxes.push_back(box);
confidences.push_back(static_cast(maxScore));
class_ids.push_back(maxClassIndex);
}
}
}
Моя целевая платформа — ограниченное устройство, поэтому я ограничен использованием только стандартных функций C++ и не могу использовать сторонние библиотеки. Для компиляции я просто использую g++ draw_detections.cpp.
Вот приведен полный код C++ и входные данные, необходимые для воспроизведения и запуска.
Подробнее здесь: https://stackoverflow.com/questions/786 ... run-faster