/* * FacePlugin * * Author: Andrew Salamon * * Copyright (c) 2008 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 "FacePlugin.h" #include "mp_CERT.h" namespace CERT { FacePlugin::FacePlugin( ConfigItem *_config ) : AfterFacePlugin(_config), valid(false) { labels.push_back( name() ); }; FacePlugin::~FacePlugin() { }; PluginBase * FacePlugin::copy() { return new FacePlugin(*this); } PluginStep FacePlugin::step() { return PluginSteps::unknown; } std::vector FacePlugin::getLabels() const { return labels; } std::string FacePlugin::invalidResultsString() const { return "\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN\tNaN"; } void FacePlugin::processImageUsingCert( RImage &image, MP_CERT &cert ) { valid = false; results.clear(); croppedFacePoints.clear(); faceSquares.clear(); if( cert.didFindFace() ) { valid = true; MP_CERT::certSquare face = cert.lastFoundFace(); results.push_back( face.x ); results.push_back( face.y ); results.push_back( face.width ); results.push_back( face.height ); results.push_back( face.scale ); // we might want a switch that would return all faces found, not just the largest if( allFaces() ) { mp_FDEyes *fd = cert.getFeatureDetector(); OWFList faces = fd->allFaces(); for( OWFList::iterator iter = faces.begin(), end = faces.end(); iter != end; ++iter ) { static const float delta = 0.001; // we've already got the largest face, so skip it here if( std::abs(iter->x - face.x) > delta || std::abs(iter->y - face.y) > delta || std::abs(iter->xSize - face.width) > delta || std::abs(iter->ySize - face.height) > delta || std::abs(iter->scale - face.scale) > delta ) { std::vector tmp; tmp.push_back( iter->x ); tmp.push_back( iter->y ); tmp.push_back( iter->xSize ); tmp.push_back( iter->ySize ); tmp.push_back( iter->scale ); faceSquares.push_back( tmp ); } } // The standard output plugin will need to walk through all face squares and print them out. } CFourCoordinates points = cert.getCroppedFaceBox(); croppedFacePoints.push_back( points.m_tx1 ); croppedFacePoints.push_back( points.m_ty1 ); croppedFacePoints.push_back( points.m_tx2 ); croppedFacePoints.push_back( points.m_ty2 ); croppedFacePoints.push_back( points.m_tx3 ); croppedFacePoints.push_back( points.m_ty3 ); croppedFacePoints.push_back( points.m_tx4 ); croppedFacePoints.push_back( points.m_ty4 ); } } void FacePlugin::processTask( Task &task ) { if( task.didFindFace ) { task.addResultsForPlugin( id(), results ); // we might want a switch that would return all faces found, not just the largest if( allFaces() ) { std::string label("face"); char num = 0; std::vector< std::vector > faces = getFaceSquares(); for( std::vector< std::vector >::iterator iter = faces.begin(), end = faces.end(); iter != end; ++iter ) { char chr = '0' + num; task.addResultsForPluginAndLabel( id(), (label+chr), *iter ); ++num; if( num > 9 ) { num = 0; label = label + '1'; } } } task.addResultsForPluginAndLabel( id(), "croppped face", croppedFacePoints ); } } std::string FacePlugin::getCroppedFaceName() { return "Cropped Face"; } std::vector FacePlugin::getCroppedFacePoints() { return croppedFacePoints; } bool FacePlugin::allFaces() const { std::vector< std::string > val = config->getValueForKey( "allfaces" ); if( 1 == val.size() ) { if( val[0] == "true" ) return true; } return false; } std::vector< std::vector > FacePlugin::getFaceSquares() { return faceSquares; } } // end namespace CERT