opencv + Caffe model zoo的 GoogLeNet训练网络将opencv_dnn模块用于图像分类。

准备

1、synset_words.txt

2、 bvlc_googlenet.prototxt

3、bvlc_googlenet.caffemodel

环境 opencv3.4.3+ vs2015



源码


#include

#include

#include

#include

using namespace cv;

using namespace cv::dnn;

#include

#include

#include

using namespace std;

/* Find best class for the blob (i. e. class with maximal probability) */

static void getMaxClass(const Mat &probBlob, int *classId, double *classProb)

{

Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 1x1000 matrix

Point classNumber;

minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);

*classId = classNumber.x;

}

static std::vector readClassNames(const char *filename = "C:\\Users\\Administrator\\Desktop\\opencv_extra-master\\testdata\\dnn\\synset_words.txt")

{

std::vector classNames;

std::ifstream fp(filename);

if (!fp.is_open())

{

std::cerr << "File with classes labels not found: " << filename << std::endl;

exit(-1);

}

std::string name;

while (!fp.eof())

{

std::getline(fp, name);

if (name.length())

classNames.push_back(name.substr(name.find(' ') + 1));

}

fp.close();

return classNames;

}

int main(int argc, char **argv)

{

CV_TRACE_FUNCTION();

Mat nn = imread("C:\\Users\\Administrator\\Desktop\\bin\\chicky_512.png");

imshow("cafee model", nn);

String modelTxt = "C:\\Users\\Administrator\\Desktop\\opencv_extra-master\\testdata\\dnn\\bvlc_googlenet.prototxt";

String modelBin = "C:\\Users\\Administrator\\Desktop\\opencv_extra-master\\testdata\\dnn\\bvlc_googlenet.caffemodel";

String imageFile = "C:\\Users\\Administrator\\Desktop\\bin\\chicky_512.png";

Net net = dnn::readNetFromCaffe(modelTxt, modelBin);

if (net.empty())

{

std::cerr << "Can't load network by using the following files: " << std::endl;

std::cerr << "prototxt:  " << modelTxt << std::endl;

std::cerr << "caffemodel: " << modelBin << std::endl;

std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;

std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;

exit(-1);

}

Mat img = imread(imageFile);

if (img.empty())

{

std::cerr << "Can't read image from the file: " << imageFile << std::endl;

exit(-1);

}

//GoogLeNet accepts only 224x224 RGB-images

Mat inputBlob = blobFromImage(img, 1, Size(224, 224),

Scalar(104, 117, 123));  //Convert Mat to batch of images

Mat prob;

cv::TickMeter t;

for (int i = 0; i < 10; i++)

{

CV_TRACE_REGION("forward");

net.setInput(inputBlob, "data");        //set the network input

t.start();

prob = net.forward("prob");                          //compute output

t.stop();

}

int classId;

double classProb;

getMaxClass(prob, &classId, &classProb);//find the best class

std::vector classNames = readClassNames();

std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;

std::cout << "Probability: " << classProb * 100 << "%" << std::endl;

std::cout << "Time: " << (double)t.getTimeMilli() / t.getCounter() << " ms (average from " << t.getCounter() << " iterations)" << std::endl;

waitKey(1000000);

return 0;

} //main

你可能感兴趣的:(opencv + Caffe model zoo的 GoogLeNet训练网络将opencv_dnn模块用于图像分类。)