You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.8 KiB
77 lines
1.8 KiB
10 years ago
|
#include <opencv2/core.hpp>
|
||
|
#include <opencv2/dnn.hpp>
|
||
|
#include <opencv2/imgproc.hpp>
|
||
|
#include <opencv2/highgui.hpp>
|
||
10 years ago
|
#include <algorithm>
|
||
|
#include <fstream>
|
||
10 years ago
|
using namespace cv;
|
||
|
using namespace cv::dnn;
|
||
|
|
||
10 years ago
|
typedef std::pair<int, double> ClassProb;
|
||
|
|
||
|
ClassProb getMaxClass(Blob &probBlob, int sampleNum = 0)
|
||
|
{
|
||
|
int numClasses = (int)probBlob.total(1);
|
||
|
Mat probMat(1, numClasses, CV_32F, probBlob.ptr<float>(sampleNum));
|
||
|
|
||
|
double prob;
|
||
|
Point probLoc;
|
||
|
minMaxLoc(probMat, NULL, &prob, NULL, &probLoc);
|
||
|
|
||
|
return std::make_pair(probLoc.x, prob);
|
||
|
}
|
||
|
|
||
|
std::vector<String> CLASES_NAMES;
|
||
|
|
||
|
void initClassesNames()
|
||
|
{
|
||
10 years ago
|
std::ifstream fp("synset_words.txt");
|
||
10 years ago
|
CV_Assert(fp.is_open());
|
||
|
|
||
|
std::string name;
|
||
|
while (!fp.eof())
|
||
|
{
|
||
|
std::getline(fp, name);
|
||
10 years ago
|
if (name.length())
|
||
|
CLASES_NAMES.push_back( name.substr(name.find(' ')+1) );
|
||
10 years ago
|
}
|
||
|
|
||
|
fp.close();
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
int main(int argc, char **argv)
|
||
10 years ago
|
{
|
||
|
Net net;
|
||
|
{
|
||
10 years ago
|
Ptr<Importer> importer = createCaffeImporter("bvlc_googlenet.prototxt", "bvlc_googlenet.caffemodel");
|
||
10 years ago
|
importer->populateNet(net);
|
||
|
}
|
||
|
|
||
10 years ago
|
String filename = (argc > 1) ? argv[1] : "space_shuttle.jpg";
|
||
|
|
||
|
Mat img = imread(filename);
|
||
10 years ago
|
CV_Assert(!img.empty());
|
||
10 years ago
|
cvtColor(img, img, COLOR_BGR2RGB);
|
||
10 years ago
|
resize(img, img, Size(227, 227));
|
||
10 years ago
|
Blob imgBlob(img);
|
||
|
|
||
10 years ago
|
net.setBlob(".data", imgBlob);
|
||
10 years ago
|
net.forward();
|
||
|
|
||
10 years ago
|
Blob prob = net.getBlob("prob");
|
||
|
ClassProb bc = getMaxClass(prob);
|
||
10 years ago
|
|
||
10 years ago
|
initClassesNames();
|
||
|
std::string className = (bc.first < (int)CLASES_NAMES.size()) ? CLASES_NAMES[bc.first] : "unnamed";
|
||
|
|
||
|
std::cout << "Best class:";
|
||
|
std::cout << " #" << bc.first;
|
||
10 years ago
|
std::cout << " (from " << prob.total(1) << ")";
|
||
10 years ago
|
std::cout << " \"" + className << "\"";
|
||
|
std::cout << std::endl;
|
||
|
std::cout << "Prob: " << bc.second * 100 << "%" << std::endl;
|
||
10 years ago
|
|
||
10 years ago
|
return 0;
|
||
10 years ago
|
}
|