Я пытаюсь использовать DNN opencv для обнаружения лиц, используя файлы caffe, поставляемые с opencv. Модель работает для большинства изображений, однако с большими изображениями мой код не работает. Кажется, есть проблема с возвращаемыми значениями из матрицы обнаружения, причем в некоторых случаях они превышают 1. Если я изменю размер изображения до размера 300x300, как рекомендовано на некоторых сайтах, модель завершится сбоем и ничего не обнаружит. Код ниже, изображение не редактируется до этого момента.
std::vector<Rect> detectDNN(Mat image) {
const std::string caffeConfigFile = "deployFinal.prototxt";
const std::string caffeWeightFile = "res10_300x300_ssd_iter_140000.caffemodel";
std::vector<Rect> rects;
Net net = cv::dnn::readNetFromCaffe(caffeConfigFile, caffeWeightFile);
cv::Mat inputBlob = cv::dnn::blobFromImage(image, 1.0, cv::Size(image.cols, image.rows), Scalar(155,155,155), false, false);
net.setInput(inputBlob, "data");
cv::Mat detection = net.forward("detection_out");
cv::Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
for (int i = 0; i < detectionMat.rows; i++)
{
float confidence = detectionMat.at<float>(i, 2);
if (confidence >.45)
{
int x1 = static_cast<int>(detectionMat.at<float>(i, 3) * image.cols);
int y1 = static_cast<int>(detectionMat.at<float>(i, 4) * image.rows);
int x2 = static_cast<int>(detectionMat.at<float>(i, 5) * image.cols);
int y2 = static_cast<int>(detectionMat.at<float>(i, 6) * image.rows);
printf("FIRST: %f Second: %f Third: %f Fourth %f\n", detectionMat.at<float>(i, 3), detectionMat.at<float>(i, 4), detectionMat.at<float>(i, 5), detectionMat.at<float>(i, 6));
cv::rectangle(image, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 255, 0), 2, 4);
Rect found(Point(x1, y1), Point(x2, y2));
rects.push_back(found);
}
}
return rects;
}