void SmilesApp::detectSmiles(const RImage<float> &pixels)
{
    mFaces.clear();
	// Find Smiles
	if( mMPSmile->findSmiles( pixels, mFaces, 1.00, wt_avg ) ){
        //console() << " findSmiles error " << endl;
        return;
	}
    mSmileResponse = 0;
    // The result is in faces(VisualObject).
    if(!mFaces.empty()) {
        for(list<VisualObject *>::iterator it = mFaces.begin(); it != mFaces.end(); ++it) {
            mFace = static_cast<FaceObject*>(*it);
            mSmileResponse = mFace->activation;
            mSmileResponse = max( 0.0f, min( mSmileResponse, mSmileLimit ) ) / mSmileLimit;
        }
        //console() << mFaces.size() << " faces detected!" << endl;
    }else{
        //console() << " no faces detected " << endl;
    }
    
	mSmileAverage[mSmileAverageIndex] = mSmileResponse;
	mSmileAverageIndex++;
	if(mSmileAverageIndex>=mSmileAverageNumOfFrames)mSmileAverageIndex=0;
	
	float smileSum = 0;
	for(int i=0;i<mSmileAverageNumOfFrames;i++){
		smileSum += mSmileAverage[i];
	}
	mSmileThreshold = smileSum/mSmileAverageNumOfFrames;
}
void SmilesApp::draw()
{
	gl::enableAlphaBlending();
	gl::clear( Color::black() );
    gl::color(1.0f, 1.0f, 1.0f);
    
    // draw webcam capture
	if( !mCapture || !mSurface )
		return;
    gl::draw( gl::Texture(mSurface) );
    
    if(mGreyChannel){
    gl::pushMatrices();{
        gl::translate(mSmileRect.getUpperLeft());
        gl::draw( gl::Texture(mGreyChannel) );
    }gl::popMatrices();
    }
    
    // draw rect that actually gets analysed:
    gl::color(ColorA(0.0f, 1.0f, 0.0f, 1.0f));
    gl::drawStrokedRect(mSmileRect);
    gl::drawStrokedCircle(mSmileRect.getUpperLeft(), 10);
    gl::drawStrokedCircle(mSmileRect.getUpperRight(), 10);
    gl::drawStrokedCircle(mSmileRect.getLowerLeft(), 10);
    gl::drawStrokedCircle(mSmileRect.getLowerRight(), 10);
    
    
    //draw mSmileThreshold
    gl::pushMatrices();{
        gl::color(ColorA(1.0f, 0.0f, 0.0f, 0.3f));
        gl::drawSolidRect(Rectf( 0, (1-mSmileThreshold)*getWindowHeight(), getWindowWidth(), getWindowHeight() ) );
        gl::drawStrokedRect( Rectf(0, 0, getWindowWidth(), getWindowHeight() ) );
    }gl::popMatrices();
    
    //draw faces
    if(!mFaces.empty()){
        gl::pushMatrices();{
            gl::translate(mSmileRect.getUpperLeft());
            gl::color(1.0f, 1.0f, 1.0f);
            for(list<VisualObject *>::iterator it = mFaces.begin(); it != mFaces.end(); ++it) {
                mFace = static_cast<FaceObject*>(*it);
                gl::drawStrokedRect(Rectf(mFace->x, mFace->y, mFace->x+mFace->xSize, mFace->y+mFace->ySize ));
            }
        }gl::popMatrices();
    }
    
    mParams.draw();
}