/* * main.cpp * * Copyright (c) 2006 Machine Perception Laboratory * University of California San Diego. * * Authors: Andrew Salamon, Josh Susskind * * Please read the disclaimer and notes about redistribution * at the end of this file. * */ #include // for cout and cerr #include // for cout and cerr #include #include #include #include #include #include // For system independent path support #include #include #include // For basename and extension namespace bf = boost::filesystem; #include "rimage.h" #include "VideoIterator.h" #include "ImageDirIterator.h" #include "ImageLoader.h" #include "Deception.h" #include "NRL_Deception.h" #include "svm_rbf.h" #include "CERT_Arguments.h" void test( const std::string &file ); void testRandomGabor(); void saveRimageCopy( CERT::ImageLoader &loader, const std::string &path, RImage &image ); int main (int argc, char *argv[]) { bool dotest = false; bool useStartEnd = false; int startFrame = 0; int endFrame = 10000000; std::string configPath = "CERT.cfg"; // leave it blank to use the built-in (hard-coded) config #ifdef WIN32 #ifdef DEBUG configPath = "CERT_Win_debug.cfg"; { bf::path dpath( configPath, bf::native ); if( !bf::exists( dpath ) ) { configPath = "CERT_Win.cfg"; std::cout << "CERT_Win_debug.cfg doesn't exist. Trying CERT_Win.cfg" << std::endl; } } #else configPath = "CERT_Win.cfg"; #endif #endif CERT::Arguments::processArgs( argc, argv, configPath ); CERT::Arguments::overrideTabDelim( true ); #ifdef DEBUG CERT::Arguments::overrideOutput( "aus.txt" ); #endif if( dotest ) { test( argv[1] ); // mpt::Deception dec( "ay.txt", "vs.txt" ); // std::vector histo = dec.loadHistogram( argv[1] ); // double val = dec.decision( histo, 50 ); // std::cout << "Deception from loaded histogram (" << argv[1] << "): " << val << std::endl; return 0; } // if( 4 == argc ) // { // std::stringstream startStr( argv[2] ); // std::stringstream endStr( argv[3] ); // if( (startStr >> startFrame) && (endStr >> endFrame) ) // { // if( (startFrame >=0) && (endFrame > startFrame) ) // useStartEnd = true; // } // } const std::vector< std::string > &paths( CERT::Arguments::paths() ); if( 1 != paths.size() ) { std::cerr << "Usage: DDP " << std::endl; return -1; } std::string videoPathString( paths[0] ); bf::path bfPath( videoPathString, bf::native ); if( !bf::exists( bfPath ) ) { std::cerr << "Error. File does not exist at: " << videoPathString << std::endl; return -2; } std::vector allImages; if( bf::is_directory( bfPath ) ) { // std::cerr << "Error. Path is a directory: " << videoPathString << std::endl; ImageDirIterator iter( videoPathString ); while( ++iter ) { allImages.push_back( *iter ); } // std::ofstream unsorted("unsorted.txt"); // std::ofstream sorted("sorted.txt"); // mpt::printVec( allImages, unsorted ); std::sort( allImages.begin(), allImages.end() ); // mpt::printVec(allImages, sorted); // return -3; } std::cout << "Initializing" << std::endl; // nrl::Deception nrldec( "w2.txt", "vs.txt", configPath ); RImage image; // if( useStartEnd ) // nrldec.start( startFrame, endFrame ); // else // nrldec.start(); std::cout << "Processing" << std::endl; if( allImages.size() > 0 ) { CERT::ImageLoader loader; for( unsigned int i = 0; i < allImages.size(); ++i ) { std::string &path = allImages[i]; RImage image; if( loader.loadRImage( image, path ) ) { // nrldec.runOneImage( image, path ); saveRimageCopy( loader, path, image ); } } } // else if( !nrldec.runOneVideo( videoPathString ) ) // { // std::cerr << "Error. Unable to read video file at path: " << videoPathString << std::endl; // return -4; // } std::cout << "Final Deception Value: " << nrldec.currentValue() << std::endl; return 0; } bool containsNaN( const std::string &str ) { size_t found; std::string lowerCopy; std::transform( str.begin(), str.end(), lowerCopy.begin(), ::tolower ); found = lowerCopy.find( "na" ); return (found != std::string::npos); } void loadAUs( const std::string &file, std::vector< std::vector > &aus ) { std::ifstream ifs( file.c_str() ); if( ifs.good() ) { do { std::string line; std::getline( ifs, line ); if( line.size() > 0 ) { if( containsNaN( line ) ) { aus.push_back( std::vector() ); } else { std::stringstream framedata( line ); std::vector frame; double val; while( framedata >> val ) frame.push_back(val); aus.push_back( frame ); } } } while( !ifs.eof() ); } } void test( const std::string &file ) { std::cout << "Begin Initializing..." << std::endl; mpt::Deception dec( "ay.txt", "vs.txt" ); std::vector< std::vector > aus; loadAUs( file, aus ); for( unsigned int i = 0; i < aus.size(); ++i ) { dec.interpolate( aus[i], (aus[i].size() > 0) ); } std::cout << "Begin Calculating..." << std::endl; double val = dec.currentValue(); std::cout << "Deception Value: " << val << std::endl; } void testRandomGabor() { mpt::vectorD3D gabor(12); double val=-1.623; double step=4.6510e-04; for( unsigned int i = 0; i < 12; ++i ) { gabor[i].resize(23); for( unsigned int j = 0; j < 23; ++j ) { gabor[i][j].resize(32); for( unsigned int k = 0; k < 32; ++k ) { gabor[i][j][k] = val; val += step; } } } mpt::Deception dec( "ay.txt", "vs.txt" ); const double d = 1.26; const int b = 4; mpt::vectorD1D hist; hist = dec.newhist( gabor, d, b ); mpt::printVec(hist); } void saveRimageCopy( CERT::ImageLoader &loader, const std::string &path, RImage &image ) { // remove the extension, add .rimage, save // change_extension fails on some images possibly because they have a space in their name. bf::path fname( path, bf::native ); fname = bf::change_extension( fname, ".rimage" ); std::cerr << "New name would be: " << fname.native_file_string() << std::endl; bf::path bname( (bf::basename(fname) + ".rimage"), bf::native); if( !bf::exists(fname) ) { if( !loader.saveRImage( image, fname.native_file_string(), CERT::Images::rimage ) ) { std::cerr << "Unable to write rimage to file: " << fname.native_file_string() << std::endl; } } else { std::cerr << "Rimage already exists: " << fname.native_file_string() << std::endl; } } /* * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */