2011-10-03 18:53:30 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include "opencv2/objdetect/objdetect.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/contrib/contrib.hpp"
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2011-10-04 08:32:18 +02:00
|
|
|
#include <io.h>
|
2011-10-03 18:53:30 +02:00
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
#ifdef HAVE_CVCONFIG_H
|
|
|
|
#include <cvconfig.h>
|
2011-10-03 18:53:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_TBB
|
|
|
|
#include "tbb/task_scheduler_init.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void help()
|
2011-10-03 18:53:30 +02:00
|
|
|
{
|
|
|
|
cout << "This program demonstrated the use of the latentSVM detector." << endl <<
|
2011-10-05 15:37:24 +02:00
|
|
|
"It reads in a trained object models and then uses them to detect the objects in an images." << endl <<
|
2011-10-03 18:53:30 +02:00
|
|
|
endl <<
|
|
|
|
"Call:" << endl <<
|
2011-10-05 15:37:24 +02:00
|
|
|
"./latentsvm_multidetect <imagesFolder> <modelsFolder> [<overlapThreshold>][<threadsNumber>]" << endl <<
|
|
|
|
"<overlapThreshold> - threshold for the non-maximum suppression algorithm." << endl <<
|
|
|
|
"Example of <modelsFolder> is opencv_extra/testdata/cv/latentsvmdetector/models_VOC2007" << endl <<
|
2011-10-03 18:53:30 +02:00
|
|
|
endl <<
|
|
|
|
"Keys:" << endl <<
|
|
|
|
"'n' - to go to the next image;" << endl <<
|
|
|
|
"'esc' - to quit." << endl <<
|
|
|
|
endl;
|
|
|
|
}
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void detectAndDrawObjects( Mat& image, LatentSvmDetector& detector, const vector<Scalar>& colors, float overlapThreshold, int numThreads )
|
2011-10-03 18:53:30 +02:00
|
|
|
{
|
|
|
|
vector<LatentSvmDetector::ObjectDetection> detections;
|
|
|
|
|
|
|
|
TickMeter tm;
|
|
|
|
tm.start();
|
2011-10-05 15:37:24 +02:00
|
|
|
detector.detect( image, detections, overlapThreshold, numThreads);
|
2011-10-03 18:53:30 +02:00
|
|
|
tm.stop();
|
|
|
|
|
|
|
|
cout << "Detection time = " << tm.getTimeSec() << " sec" << endl;
|
|
|
|
|
|
|
|
const vector<string> classNames = detector.getClassNames();
|
|
|
|
CV_Assert( colors.size() == classNames.size() );
|
|
|
|
|
|
|
|
for( size_t i = 0; i < detections.size(); i++ )
|
|
|
|
{
|
|
|
|
const LatentSvmDetector::ObjectDetection& od = detections[i];
|
2011-10-06 11:43:43 +02:00
|
|
|
rectangle( image, od.rect, colors[od.classID], 3 );
|
2011-10-05 15:37:24 +02:00
|
|
|
}
|
|
|
|
// put text over the all rectangles
|
|
|
|
for( size_t i = 0; i < detections.size(); i++ )
|
|
|
|
{
|
|
|
|
const LatentSvmDetector::ObjectDetection& od = detections[i];
|
|
|
|
putText( image, classNames[od.classID], Point(od.rect.x+4,od.rect.y+13), FONT_HERSHEY_SIMPLEX, 0.55, colors[od.classID], 2 );
|
2011-10-03 18:53:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void readDirectory( const string& directoryName, vector<string>& filenames, bool addDirectoryName=true )
|
2011-10-03 18:53:30 +02:00
|
|
|
{
|
|
|
|
filenames.clear();
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2011-10-04 08:32:18 +02:00
|
|
|
struct _finddata_t s_file;
|
|
|
|
string str = directoryName + "\\*.*";
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
intptr_t h_file = _findfirst( str.c_str(), &s_file );
|
|
|
|
if( h_file != static_cast<intptr_t>(-1.0) )
|
2011-10-04 08:32:18 +02:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( addDirectoryName )
|
|
|
|
filenames.push_back(directoryName + "\\" + s_file.name);
|
|
|
|
else
|
|
|
|
filenames.push_back((string)s_file.name);
|
|
|
|
}
|
|
|
|
while( _findnext( h_file, &s_file ) == 0 );
|
|
|
|
}
|
|
|
|
_findclose( h_file );
|
2011-10-03 18:53:30 +02:00
|
|
|
#else
|
|
|
|
DIR* dir = opendir( directoryName.c_str() );
|
|
|
|
if( dir != NULL )
|
|
|
|
{
|
|
|
|
struct dirent* dent;
|
|
|
|
while( (dent = readdir(dir)) != NULL )
|
|
|
|
{
|
|
|
|
if( addDirectoryName )
|
|
|
|
filenames.push_back( directoryName + "/" + string(dent->d_name) );
|
|
|
|
else
|
|
|
|
filenames.push_back( string(dent->d_name) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sort( filenames.begin(), filenames.end() );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2012-06-07 19:21:29 +02:00
|
|
|
help();
|
2011-10-03 18:53:30 +02:00
|
|
|
|
|
|
|
string images_folder, models_folder;
|
2012-03-16 22:21:04 +01:00
|
|
|
float overlapThreshold = 0.2f;
|
2011-10-03 18:53:30 +02:00
|
|
|
int numThreads = -1;
|
|
|
|
if( argc > 2 )
|
2012-06-07 19:21:29 +02:00
|
|
|
{
|
2011-10-03 18:53:30 +02:00
|
|
|
images_folder = argv[1];
|
|
|
|
models_folder = argv[2];
|
2012-03-16 22:21:04 +01:00
|
|
|
if( argc > 3 ) overlapThreshold = (float)atof(argv[3]);
|
2011-10-05 15:37:24 +02:00
|
|
|
if( overlapThreshold < 0 || overlapThreshold > 1)
|
|
|
|
{
|
|
|
|
cout << "overlapThreshold must be in interval (0,1)." << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( argc > 4 ) numThreads = atoi(argv[4]);
|
2012-06-07 19:21:29 +02:00
|
|
|
}
|
2011-10-03 18:53:30 +02:00
|
|
|
|
|
|
|
vector<string> images_filenames, models_filenames;
|
|
|
|
readDirectory( images_folder, images_filenames );
|
|
|
|
readDirectory( models_folder, models_filenames );
|
|
|
|
|
|
|
|
LatentSvmDetector detector( models_filenames );
|
|
|
|
if( detector.empty() )
|
|
|
|
{
|
|
|
|
cout << "Models cann't be loaded" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2011-10-05 15:37:24 +02:00
|
|
|
const vector<string>& classNames = detector.getClassNames();
|
|
|
|
cout << "Loaded " << classNames.size() << " models:" << endl;
|
|
|
|
for( size_t i = 0; i < classNames.size(); i++ )
|
|
|
|
{
|
|
|
|
cout << i << ") " << classNames[i] << "; ";
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
cout << "overlapThreshold = " << overlapThreshold << endl;
|
|
|
|
|
2011-10-06 11:43:43 +02:00
|
|
|
vector<Scalar> colors;
|
|
|
|
generateColors( colors, detector.getClassNames().size() );
|
2011-10-03 18:53:30 +02:00
|
|
|
|
|
|
|
for( size_t i = 0; i < images_filenames.size(); i++ )
|
|
|
|
{
|
|
|
|
Mat image = imread( images_filenames[i] );
|
|
|
|
if( image.empty() ) continue;
|
|
|
|
|
|
|
|
cout << "Process image " << images_filenames[i] << endl;
|
2011-10-05 15:37:24 +02:00
|
|
|
detectAndDrawObjects( image, detector, colors, overlapThreshold, numThreads );
|
2011-10-03 18:53:30 +02:00
|
|
|
|
|
|
|
imshow( "result", image );
|
|
|
|
|
2012-03-16 22:21:04 +01:00
|
|
|
for(;;)
|
2011-10-03 18:53:30 +02:00
|
|
|
{
|
|
|
|
int c = waitKey();
|
|
|
|
if( (char)c == 'n')
|
|
|
|
break;
|
|
|
|
else if( (char)c == '\x1b' )
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2012-06-07 19:21:29 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-10-03 18:53:30 +02:00
|
|
|
}
|