/* * Linear Emotion Emotions1 * * Author: Andrew Salamon * * Copyright (c) 2010 Machine Perception Laboratory * University of California San Diego. * * 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. * */ #include "Emotions1.h" #include #include #include #include #include #include #ifdef WIN32 #define _USE_MATH_DEFINES #include #else #include #endif #include #include #include #include #include #include // For basename and extension namespace bf = boost::filesystem; namespace mpt { Emotions1::Emotions1() { } void Emotions1::clear() { weights.resize( boost::extents[0][0] ); } bool Emotions1::readFromFile( const std::string &file ) { bf::path weightPath( file, bf::native ); std::string ext = bf::extension(weightPath); if( (".txt" == ext) || (".TXT" == ext) || (".text" == ext) || (".TEXT" == ext) ) { // read a plain text file, probably exported from Matlab with something like: // save emotions1.xml Wemo -ASCII std::ifstream weightsStream( file.c_str() ); std::string line; int lineCnt = 0; weights.resize( boost::extents[auCount][emotionCount] ); while( std::getline( weightsStream, line ) ) { std::istringstream iss( line, std::istringstream::in ); float val; for( int cnt = 0; cnt < emotionCount; ++cnt ) { if( iss >> val ) { weights[lineCnt][cnt] = val; } else { std::cerr << "Couldn't read emotion weight value: " << lineCnt << " x " << cnt << std::endl; std::cerr << "Line was: " << line << std::endl; } } ++lineCnt; } if( auCount != lineCnt ) { std::cerr << "Wrong number of AUs in weights file." << std::endl; return false; } } else { throw std::runtime_error( "Unsupported file type in Emotions1." ); } printMatrix( weights, "Weights" ); return true; } std::vector Emotions1::calc( const std::vector &aus ) { // Multiply the vector times the weights and return the results. Matrix2d inputs; inputs.resize( boost::extents[1][auCount] ); for( int cnt = 0; cnt < auCount; ++cnt ) inputs[0][cnt] = aus[cnt]; printMatrix( inputs, "Inputs" ); Matrix2d outputs; SimpleMatrixOps::multiply( inputs, weights, outputs ); std::vector res( emotionCount, 0.0 ); for( int idx = 0; idx < emotionCount; ++idx ) res[idx] = outputs[0][idx]; printMatrix( outputs, "Outputs" ); return res; } void printMatrix( const Matrix2d &matrix, const std::string &title ) { if( title.length() > 0 ) std::cout << title << ":" << std::endl; for( unsigned int row = 0; row < matrix.shape()[0]; ++row ) { for( unsigned int col = 0; col < matrix.shape()[1]; ++col ) { if( title.length() > 0 ) std::cout << " "; std::cout << matrix[row][col]; if( col < (matrix.shape()[1] - 1) ) std::cout << " "; } std::cout << std::endl; } } } // end namespace mpt BOOST_CLASS_VERSION( mpt::Emotions1, 1 );