Merge pull request #10529 from LaurentBerger:ExampleGoogleNet

* Add a parameter labels to command line

* default value

* samples: caffe_googlenet.cpp minor refactoring
pull/10534/head
LaurentBerger 7 years ago committed by Alexander Alekhin
parent d0b2e60edc
commit 004a1cd64a
  1. 8
      samples/dnn/caffe_googlenet.cpp

@ -60,7 +60,7 @@ static void getMaxClass(const Mat &probBlob, int *classId, double *classProb)
*classId = classNumber.x; *classId = classNumber.x;
} }
static std::vector<String> readClassNames(const char *filename = "synset_words.txt") static std::vector<String> readClassNames(const char *filename )
{ {
std::vector<String> classNames; std::vector<String> classNames;
@ -87,6 +87,7 @@ const char* params
= "{ help | false | Sample app for loading googlenet model }" = "{ help | false | Sample app for loading googlenet model }"
"{ proto | bvlc_googlenet.prototxt | model configuration }" "{ proto | bvlc_googlenet.prototxt | model configuration }"
"{ model | bvlc_googlenet.caffemodel | model weights }" "{ model | bvlc_googlenet.caffemodel | model weights }"
"{ label | synset_words.txt | names of ILSVRC2012 classes }"
"{ image | space_shuttle.jpg | path to image file }" "{ image | space_shuttle.jpg | path to image file }"
"{ opencl | false | enable OpenCL }" "{ opencl | false | enable OpenCL }"
; ;
@ -106,6 +107,7 @@ int main(int argc, char **argv)
String modelTxt = parser.get<string>("proto"); String modelTxt = parser.get<string>("proto");
String modelBin = parser.get<string>("model"); String modelBin = parser.get<string>("model");
String imageFile = parser.get<String>("image"); String imageFile = parser.get<String>("image");
String classNameFile = parser.get<String>("label");
Net net; Net net;
try { try {
@ -113,7 +115,7 @@ int main(int argc, char **argv)
net = dnn::readNetFromCaffe(modelTxt, modelBin); net = dnn::readNetFromCaffe(modelTxt, modelBin);
//! [Read and initialize network] //! [Read and initialize network]
} }
catch (cv::Exception& e) { catch (const cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl; std::cerr << "Exception: " << e.what() << std::endl;
//! [Check that network was read successfully] //! [Check that network was read successfully]
if (net.empty()) if (net.empty())
@ -169,7 +171,7 @@ int main(int argc, char **argv)
//! [Gather output] //! [Gather output]
//! [Print results] //! [Print results]
std::vector<String> classNames = readClassNames(); std::vector<String> classNames = readClassNames(classNameFile.c_str());
std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl; std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
std::cout << "Probability: " << classProb * 100 << "%" << std::endl; std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
//! [Print results] //! [Print results]

Loading…
Cancel
Save