我正嘗試在OpenCV中使用MobileNet SSD +深度神經網絡(dnn)模塊進行對象檢測。我成功加載並使用了模型。作爲net.forward的輸出,我獲得了包含關於檢測到的對象的信息的Mat對象。不幸的是,我在與「易於工作的一部分」鬥爭,閱讀究竟是什麼被發現。如何在高維Mat:Class中使用C++讀取特定座標處的數據?
這是我知道的輸出墊目標信息:
- 它有4種尺寸。
- 的大小是1×1×number_of_objects_detected X 7.
- 七條關於每個對象的信息的有:第一是類ID,所述第二是信心,所述第三-第七是邊界框的值。
我找不到任何C++的例子,但我發現了很多python的例子。他們讀取這樣的數據:
for i in np.arange(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
如何在C++中做到這一點最簡單的方法是什麼?即我需要讀取高維Mat:class中特定座標的數據。
謝謝你的幫助。我在C++中很新,有時會發現它壓倒性的...
我正在使用OpenCV 3.3.0。我正在使用MobileNet SSD的GitHub:https://github.com/chuanqi305/MobileNet-SSD。
我的程序的代碼:
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <fstream>
#include <iostream>
using namespace cv;
using namespace cv::dnn;
using namespace std;
// function to create vector of class names
std::vector<String> createClaseNames() {
std::vector<String> classNames;
classNames.push_back("background");
classNames.push_back("aeroplane");
classNames.push_back("bicycle");
classNames.push_back("bird");
classNames.push_back("boat");
classNames.push_back("bottle");
classNames.push_back("bus");
classNames.push_back("car");
classNames.push_back("cat");
classNames.push_back("chair");
classNames.push_back("cow");
classNames.push_back("diningtable");
classNames.push_back("dog");
classNames.push_back("horse");
classNames.push_back("motorbike");
classNames.push_back("person");
classNames.push_back("pottedplant");
classNames.push_back("sheep");
classNames.push_back("sofa");
classNames.push_back("train");
classNames.push_back("tvmonitor");
return classNames;
}
// main function
int main(int argc, char **argv)
{
// set inputs
String modelTxt = "C:/Users/acer/Desktop/kurz_OCV/cv4faces/project/python/object-detection-deep-learning/MobileNetSSD_deploy.prototxt";
String modelBin = "C:/Users/acer/Desktop/kurz_OCV/cv4faces/project/python/object-detection-deep-learning/MobileNetSSD_deploy.caffemodel";
String imageFile = "C:/Users/acer/Desktop/kurz_OCV/cv4faces/project/puppies.jpg";
std::vector<String> classNames = createClaseNames();
//read caffe model
Net net;
try {
net = dnn::readNetFromCaffe(modelTxt, modelBin);
}
catch (cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
if (net.empty())
{
std::cerr << "Can't load network." << std::endl;
exit(-1);
}
}
// read image
Mat img = imread(imageFile);
// create input blob
resize(img, img, Size(300, 300));
Mat inputBlob = blobFromImage(img, 0.007843, Size(300, 300), Scalar(127.5)); //Convert Mat to dnn::Blob image batch
// apply the blob on the input layer
net.setInput(inputBlob); //set the network input
// classify the image by applying the blob on the net
Mat detections = net.forward("detection_out"); //compute output
// print some information about detections
std::cout << "dims: " << detections.dims << endl;
std::cout << "size: " << detections.size << endl;
//show image
String winName("image");
imshow(winName, img);
// Wait for keypress
waitKey();
}
有一個C++樣品與MobileNet-SSD從來自Caffe:https://github.com/opencv/opencv/blob/master/samples/dnn/ssd_mobilenet_object_detection.cpp –