@ -1,4 +1,4 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
/** M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
@ -38,14 +38,12 @@
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
# include <opencv2/dnn.hpp>
# include <opencv2/imgproc.hpp>
# include <opencv2/highgui.hpp>
using namespace cv ;
using namespace cv : : dnn ;
# include <algorithm>
# include <fstream>
# include <iostream>
# include <cstdlib>
@ -68,8 +66,7 @@ std::vector<String> readClassNames(const char *filename = "synset_words.txt")
std : : ifstream fp ( filename ) ;
if ( ! fp . is_open ( ) )
{
std : : cerr < < " File with classes labels not found " < < std : : endl ;
std : : cerr < < " Check it: " < < filename < < std : : endl ;
std : : cerr < < " File with classes labels not found: " < < filename < < std : : endl ;
exit ( - 1 ) ;
}
@ -89,19 +86,19 @@ int main(int argc, char **argv)
{
String modelTxt = " bvlc_googlenet.prototxt " ;
String modelBin = " bvlc_googlenet.caffemodel " ;
String imageFile = ( argc > 1 ) ? argv [ 1 ] : " space_shuttle.jpg " ;
//! [importer_creation ]
//! [Create the importer of Caffe model ]
Ptr < dnn : : Importer > importer ;
try //Try to import Caffe GoogleNet model
try //Try to import Caffe GoogleNet model
{
importer = dnn : : createCaffeImporter ( modelTxt , modelBin ) ;
}
catch ( const cv : : Exception & err ) //i mporter can throw errors, we will catch them
catch ( const cv : : Exception & err ) //I mporter can throw errors, we will catch them
{
std : : cerr < < err . msg < < std : : endl ;
importer = Ptr < Importer > ( ) ; //NULL
}
//! [importer_creation ]
//! [Create the importer of Caffe model ]
if ( ! importer )
{
@ -113,52 +110,45 @@ int main(int argc, char **argv)
exit ( - 1 ) ;
}
//! [network_initialization ]
//! [Initialize network ]
dnn : : Net net ;
importer - > populateNet ( net ) ;
importer . release ( ) ; //We don't need importer anymore
//! [Initialize network]
delete importer ;
//! [network_initialization]
String imagefile = ( argc > 1 ) ? argv [ 1 ] : " space_shuttle.jpg " ;
//! [input_blob_preparation]
Mat img = imread ( imagefile ) ;
//! [Prepare blob]
Mat img = imread ( imageFile ) ;
if ( img . empty ( ) )
{
std : : cerr < < " Can't read image from the file: " < < imagef ile < < std : : endl ;
std : : cerr < < " Can't read image from the file: " < < imageFile < < std : : endl ;
exit ( - 1 ) ;
}
//GoogLeNet accepts only 224x224 RGB-images
cvtColor ( img , img , COLOR_BGR2RGB ) ;
resize ( img , img , Size ( 224 , 224 ) ) ;
dnn : : Blob inputBlob = dnn : : Blob ( img ) ;
//! [input_blob_preparation]
resize ( img , img , Size ( 224 , 224 ) ) ; //GoogLeNet accepts only 224x224 RGB-images
dnn : : Blob inputBlob = dnn : : Blob ( img ) ; //Convert Mat to dnn::Blob image batch
//! [Prepare blob]
//! [setup_ blob]
net . setBlob ( " .data " , inputBlob ) ; //set the network input
//! [setup_ blob]
//! [Set input blob]
net . setBlob ( " .data " , inputBlob ) ; //set the network input
//! [Set input blob]
//! [make_forward ]
net . forward ( ) ; //compute output
//! [make_forward ]
//! [Make forward pass ]
net . forward ( ) ; //compute output
//! [Make forward pass ]
//! [get_ output]
dnn : : Blob prob = net . getBlob ( " prob " ) ; //gather output of "prob" layer
//! [Gather output]
dnn : : Blob prob = net . getBlob ( " prob " ) ; //gather output of "prob" layer
int classId ;
double classProb ;
getMaxClass ( prob , & classId , & classProb ) ; //find the best class
//! [get_ output]
getMaxClass ( prob , & classId , & classProb ) ; //find the best class
//! [Gather output]
//! [print_info ]
//! [Print results ]
std : : vector < String > classNames = readClassNames ( ) ;
std : : cout < < " Best class: # " < < classId < < " ' " < < classNames . at ( classId ) < < " ' " < < std : : endl ;
std : : cout < < " Probability: " < < classProb * 100 < < " % " < < std : : endl ;
//! [print_info ]
//! [Print results ]
return 0 ;
}
} //main