[203] | 1 | // Copyright (C) 2009 foam |
---|
| 2 | // |
---|
| 3 | // This program is free software; you can redistribute it and/or modify |
---|
| 4 | // it under the terms of the GNU General Public License as published by |
---|
| 5 | // the Free Software Foundation; either version 2 of the License, or |
---|
| 6 | // (at your option) any later version. |
---|
| 7 | // |
---|
| 8 | // This program is distributed in the hope that it will be useful, |
---|
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 11 | // GNU General Public License for more details. |
---|
| 12 | // |
---|
| 13 | // You should have received a copy of the GNU General Public License |
---|
| 14 | // along with this program; if not, write to the Free Software |
---|
| 15 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
| 16 | |
---|
| 17 | #include <assert.h> |
---|
| 18 | #include "App.h" |
---|
| 19 | #include "FileTools.h" |
---|
| 20 | #include "PCA.h" |
---|
| 21 | |
---|
| 22 | #define SAVE_FRAMES |
---|
| 23 | |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
| 26 | //int w=50; |
---|
| 27 | //int h=80; |
---|
| 28 | |
---|
| 29 | int w=20; |
---|
| 30 | int h=30; |
---|
| 31 | |
---|
| 32 | App::App(const string &filename) : |
---|
| 33 | m_Capture(NULL), |
---|
| 34 | m_Frame(NULL), |
---|
| 35 | m_FrameCopy(NULL), |
---|
| 36 | m_ExtVec(NULL), |
---|
| 37 | m_FrameNum(0) |
---|
| 38 | { |
---|
| 39 | m_CtrlPort.open("/faceident-ctrl"); |
---|
| 40 | |
---|
| 41 | m_Extremes[0]=NULL; |
---|
| 42 | m_Extremes[1]=NULL; |
---|
| 43 | |
---|
| 44 | if (filename=="") |
---|
| 45 | { |
---|
| 46 | m_Capture = cvCaptureFromCAM(0); |
---|
| 47 | } |
---|
| 48 | else |
---|
| 49 | { |
---|
| 50 | m_Capture = cvCaptureFromAVI(filename.c_str()); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | assert(m_Capture); |
---|
| 54 | |
---|
| 55 | m_PCA = new PCA(w*h); |
---|
| 56 | //FILE *f=fopen("../no-redist/eigenspaces/spacek-50x80.pca", "rb"); |
---|
[207] | 57 | FILE *f=fopen("../../../libs/magicsquares/data/eigenspaces/spacek-20x30.pca", "rb"); |
---|
[203] | 58 | m_PCA->Load(f); |
---|
| 59 | fclose(f); |
---|
| 60 | m_PCA->Compress(30,70); |
---|
| 61 | |
---|
| 62 | cvInitFont( &m_Font, CV_FONT_HERSHEY_PLAIN, 0.5, 0.5, 0, 1, CV_AA ); |
---|
| 63 | cvInitFont( &m_LargeFont, CV_FONT_HERSHEY_PLAIN, 5, 5, 0, 10, CV_AA ); |
---|
| 64 | |
---|
| 65 | cvNamedWindow( "expression recog", 1 ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | App::~App() |
---|
| 69 | { |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | static CvScalar colors[] = |
---|
| 73 | { |
---|
| 74 | {{0,0,255}}, |
---|
| 75 | {{0,0,0}}, |
---|
| 76 | {{0,255,255}}, |
---|
| 77 | {{0,255,0}}, |
---|
| 78 | {{255,128,0}}, |
---|
| 79 | {{255,255,0}}, |
---|
| 80 | {{255,0,0}}, |
---|
| 81 | {{255,0,255}}, |
---|
| 82 | }; |
---|
| 83 | |
---|
| 84 | void App::Run() |
---|
| 85 | { |
---|
| 86 | m_Frame = cvQueryFrame( m_Capture ); |
---|
| 87 | if( !m_Frame ) |
---|
| 88 | { |
---|
| 89 | cerr<<"no frame captured"<<endl; |
---|
| 90 | return; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | if( !m_FrameCopy ) |
---|
| 94 | m_FrameCopy = cvCreateImage( cvSize(m_Frame->width,m_Frame->height), |
---|
| 95 | IPL_DEPTH_8U, m_Frame->nChannels ); |
---|
| 96 | if( m_Frame->origin == IPL_ORIGIN_TL ) |
---|
| 97 | cvCopy( m_Frame, m_FrameCopy, 0 ); |
---|
| 98 | else |
---|
| 99 | cvFlip( m_Frame, m_FrameCopy, 0 ); |
---|
| 100 | |
---|
| 101 | Image camera(m_FrameCopy); |
---|
| 102 | Update(camera); |
---|
| 103 | |
---|
| 104 | m_FrameNum++; |
---|
| 105 | #ifdef SAVE_FRAMES |
---|
| 106 | char name[256]; |
---|
| 107 | sprintf(name,"out-%0.4d.jpg",m_FrameNum); |
---|
| 108 | cerr<<"saving "<<name<<endl; |
---|
| 109 | cvSaveImage(name,camera.m_Image); |
---|
| 110 | #endif |
---|
| 111 | |
---|
| 112 | cvShowImage("expression recog", camera.m_Image); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void App::Update(Image &camera) |
---|
| 116 | { |
---|
| 117 | /////////////////////////////////// |
---|
| 118 | // dispatch from input |
---|
| 119 | |
---|
| 120 | int key=cvWaitKey(10); |
---|
| 121 | |
---|
| 122 | int learn=-1; |
---|
| 123 | |
---|
| 124 | switch (key) |
---|
| 125 | { |
---|
| 126 | case 'h': learn=0; break; |
---|
| 127 | case 's': learn=1; break; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | vector<Rect> rects = m_FaceFinder.Find(camera,true); |
---|
| 131 | for(vector<Rect>::iterator i = rects.begin(); i!=rects.end(); i++ ) |
---|
| 132 | { |
---|
| 133 | // get the face area as a sub image |
---|
| 134 | Image face = camera.SubImage(*i).Scale(w,h).RGB2GRAY(); |
---|
| 135 | face.SubMean(); |
---|
| 136 | Vector<float> params = m_PCA->Project(face.ToFloatVector()); |
---|
| 137 | |
---|
| 138 | if (learn!=-1) |
---|
| 139 | { |
---|
| 140 | m_Extremes[learn] = new Vector<float>(params); |
---|
| 141 | if (m_Extremes[0]!=NULL && m_Extremes[1]!=NULL) |
---|
| 142 | { |
---|
| 143 | Vector<float> v=(*m_Extremes[1])-(*m_Extremes[0]); |
---|
| 144 | m_ExtVec = new Vector<float>(v); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | if (m_ExtVec!=NULL) |
---|
| 149 | { |
---|
| 150 | float d = m_ExtVec->Dot(params-(*m_Extremes[0])); |
---|
| 151 | |
---|
| 152 | if (d>1.00) |
---|
| 153 | { |
---|
| 154 | cvPutText(camera.m_Image, "frown", cvPoint(15,200), &m_LargeFont, colors[1]); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | cerr<<d<<endl; |
---|
| 158 | |
---|
| 159 | if (d<1.00) |
---|
| 160 | { |
---|
| 161 | cvPutText(camera.m_Image, "smile", cvPoint(15,200), &m_LargeFont, colors[1]); |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /*for (int n=0; n<params.Size(); ++n) |
---|
| 166 | { |
---|
| 167 | cvRectangle(camera.m_Image, cvPoint(n*15,50+params[n]*50), |
---|
| 168 | cvPoint(n*15+15,50), colors[1]); |
---|
| 169 | char info[256]; |
---|
| 170 | sprintf(info,"%d",n); |
---|
| 171 | cvPutText(camera.m_Image, info, cvPoint(n*15,60), &m_Font, colors[0]); |
---|
| 172 | }*/ |
---|
| 173 | |
---|
| 174 | Image out(w,h,1,m_PCA->Synth(params*2)); |
---|
| 175 | camera.Blit(out.Scale(40,60),i->x-30,i->y); |
---|
| 176 | |
---|
| 177 | // pass it into the face bank |
---|
| 178 | |
---|
| 179 | //cvRectangle(camera.m_Image, cvPoint(i->x,i->y), cvPoint(i->x+i->w,i->y+i->h), colors[0]); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | //char info[256]; |
---|
| 183 | //cvPutText(camera.m_Image, info, cvPoint(10,10), &m_Font, colors[0]); |
---|
| 184 | |
---|
| 185 | } |
---|
| 186 | |
---|