Skip to content

Commit

Permalink
Broken Armadillo version of tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
peko committed May 17, 2012
1 parent f6f095c commit ba290b1
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 100 deletions.
2 changes: 2 additions & 0 deletions FaceTrackerTest.vcxproj
Expand Up @@ -122,11 +122,13 @@ if not exist "$(ProjectDir)\bin\data" mkdir "$(ProjectDir)\bin\data"
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\FaceTracker.cpp" />
<ClCompile Include="tests\FaceTrackerTest\Face.cpp" />
<ClCompile Include="tests\FaceTrackerTest\main.cpp" />
<ClCompile Include="tests\FaceTrackerTest\testApp.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\FaceTracker.h" />
<ClInclude Include="tests\FaceTrackerTest\Face.h" />
<ClInclude Include="tests\FaceTrackerTest\testApp.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
4 changes: 4 additions & 0 deletions FaceTrackerTest.vcxproj.user
Expand Up @@ -4,4 +4,8 @@
<LocalDebuggerWorkingDirectory>$(ProjectDir)\bin</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>$(ProjectDir)\bin</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
76 changes: 17 additions & 59 deletions src/FaceTracker.cpp
@@ -1,10 +1,11 @@
#include "FaceTracker.h"
//#include <armadillo>

#include <armadillo>

#define LOST_TICKS 300

//using namespace std;
//using namespace arma;
using namespace std;
using namespace arma;


FaceTracker::FaceTracker(void) {
Expand All @@ -16,74 +17,37 @@ FaceTracker::~FaceTracker(void) {

}

// TRACK METHOD
//
// New face appears One face disappear
//
// finder j - [1] [2] [3] : [1] [2] - ah height of matrix
// | X | / : | X | \
// faces i - (1) (2) : (1) (2) (3) - aw width of matrix
//
// Building of distance (dst) matrix, # - shortest
// dst (*)>[*] dst [*]>(*)
//
// (0) (1) i : (0) (1) (2) i
// [0]| # | | : [0]| # | | |
// [1]| | # | : [1]| | # | |
// [2]| | | : j
// j
// nw = true nw = false
// sid = [0, 1] sid = [0, 1]
// lid = [0, 1,-1] lid = [0, 1,-1]
// lost face or blob = -1

void FaceTracker::trackFaces(vector<ofRectangle>& blobs) {

string dbg = debug = "";
//cout << ("\n\n---------------------------------\n");

void FaceTracker::trackFaces(vector<Face>& blobs) {

int aw = blobs.size();
int ah = trackedFaces.size();

dbg = "dst["+ofToString(aw)+"x"+ofToString(ah)+"]\n";
//cout << dbg;
debug += dbg;

mat D(aw, ah);

// if aw > ah
int sc = min(aw, ah); // short side counter
int lc = max(aw, ah); // long side counter
bool nw = aw > ah; // true - number of detected faces greater than tracked faces

// Allocate memory
float** dst; dst = new float* [ah]; for (int i = 0; i < ah; ++i) dst[i] = new float[aw];
float* min = new float[ah]; for (int i = 0; i < ah; ++i) min[i] = 1e8;
// if nw lid - mean blob to face link else face to blob
int* sid = new int [sc]; for (int i = 0; i < sc; ++i) sid[i] = -1; // link id
int* lid = new int [lc]; for (int i = 0; i < lc; ++i) lid[i] = -1; // back reference id, has holes

for(int j = 0; j < ah; j++) {
dbg ="[";
for(int i = 0; i < aw; i++) {
ofRectangle& r1 = trackedFaces[j];
ofRectangle& r2 = blobs[i];
float dcx = r2.x+r2.width /2.0 - r1.x-r1.width /2.0;
float dcy = r2.y+r2.height/2.0 - r1.y-r1.height/2.0;
dst[j][i] = sqrt(dcx*dcx + dcy*dcy);
int c = nw ? j : i; // shortest side counter
int r = nw ? i : j; // reference to longer side counter
if(dst[j][i] < min[c]) {
min[c] = dst[j][i];
sid[c] = r;
}
dbg += ofToString(dst[j][i])+string((i<aw-1)?", ":"");
//float dcx = r2.x+r2.width /2.0 - r1.x-r1.width /2.0;
//float dcy = r2.y+r2.height/2.0 - r1.y-r1.height/2.0;
//dst[j][i] = sqrt(dcx*dcx + dcy*dcy);
//int c = nw ? j : i; // shortest side counter
//int r = nw ? i : j; // reference to longer side counter
//if(dst[j][i] < min[c]) {
// min[c] = dst[j][i];
// sid[c] = r;
//}
}
dbg +="]\n";
debug+=dbg;
}

dbg= "sid["; for (int i=0; i<sc; i++) dbg+=ofToString(sid[i])+string((i<sc-1)?", ":""); dbg+= "]\n";
//cout << dbg;
debug += dbg;

// tracking
for (int i=0; i<sc; i++) {
Expand All @@ -103,9 +67,6 @@ void FaceTracker::trackFaces(vector<ofRectangle>& blobs) {
//trackedFaces[sid[i]].setNewRect(blobs[i]);
}
}
dbg="lid["; for (int i=0; i<lc; i++) dbg+=ofToString(lid[i])+string((i<lc-1)?", ":""); dbg+= "]\n";
// cout << dbg;
debug += dbg;

// Process new blobs and lost tracks
for (int i=0; i<lc; i++) {
Expand Down Expand Up @@ -133,8 +94,5 @@ void FaceTracker::trackFaces(vector<ofRectangle>& blobs) {
}

// De-Allocate memory to prevent memory leak
delete[] lid;
delete[] sid;
delete[] min;
for (int i = 0; i < ah; ++i) delete [] dst[i]; delete[] dst;
};
7 changes: 4 additions & 3 deletions src/FaceTracker.h
@@ -1,16 +1,17 @@
#pragma once

#include "ofMain.h"
#include "Face.h"

class FaceTracker
{
public:
FaceTracker(void);
~FaceTracker(void);

vector<ofRectangle> trackedFaces;
int trackedFacesCounter[100];
void trackFaces(vector<ofRectangle>& blobs);
vector<Face> faces;

void trackFaces(vector<Face>& blobs);

string debug;

Expand Down
2 changes: 1 addition & 1 deletion tests/FaceTrackerTest/main.cpp
Expand Up @@ -6,7 +6,7 @@
int main( ){

ofAppGlutWindow window;
ofSetupOpenGL(&window, 800,600, OF_WINDOW); // <-------- setup the GL context
ofSetupOpenGL(&window, 1920,1080, OF_WINDOW); // <-------- setup the GL context

// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
Expand Down
67 changes: 31 additions & 36 deletions tests/FaceTrackerTest/testApp.cpp
Expand Up @@ -13,21 +13,20 @@ unsigned char testApp::colors[] = {

//--------------------------------------------------------------
void testApp::setup(){

oclx1 = ofGetWidth()* 4/16;
oclx2 = ofGetWidth()*11/16;
oclw = ofGetWidth() /16;

ofBackground(0);
ofEnableAlphaBlending();
ofSetCircleResolution(4);
ofSetCircleResolution(36);

bg.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR);
unsigned char* pixels = bg.getPixels();
memset(pixels, 0x0, bg.width*bg.height*3);
//ofSetFrameRate(300);
}

//--------------------------------------------------------------
void testApp::update(){

unsigned char* pixels = bg.getPixels();


if(!B.size() || (B.size() < 10 && ofRandom(0.0, 100.0) > 99.0)) {

Expand All @@ -46,6 +45,7 @@ void testApp::update(){
C.push_back(ofColor(colors[c*3], colors[c*3+1], colors[c*3+2]));
}

// random kill first
if(B.size() >= 5 && ofRandom(0.0, 100.0) > 99.0) {
B.erase(B.begin());
T.erase(T.begin());
Expand All @@ -58,39 +58,31 @@ void testApp::update(){
float dy = B[i].y + B[i].height/2 - T[i].y;
float ln = sqrt(dx*dx + dy*dy);

float v = 1.0;
float v = 20.0;

V[i].x += (-dx/ln*v- V[i].x)/25;
V[i].y += (-dy/ln*v- V[i].y)/25;
V[i].x += (-dx/ln*v- V[i].x)/50;
V[i].y += (-dy/ln*v- V[i].y)/50;

B[i] = B[i]+V[i];

// T[i].x +=dy/ln/ln*1e2;
// T[i].y -=dx/ln/ln*1e2;
T[i].x +=dy/ln*v/2.0;
T[i].y -=dx/ln*v/2.0;
T[i].x +=dy/ln/ln*1e2*v;
T[i].y -=dx/ln/ln*1e2*v;
//T[i].x +=dy/ln*v;
//T[i].y -=dx/ln*v;

// ofPoint c = B[i].getCenter();
// for (int j=0; j<ln/2; j++) {
// static float R = 25.0;
// static float l = 30.0;
// float x = c.x + ofRandom(-(ln-l)/R, (ln-l)/R)+ofRandom(-(ln-l)/R, (ln-l)/R);
// float y = c.y + ofRandom(-(ln-l)/R, (ln-l)/R)+ofRandom(-(ln-l)/R, (ln-l)/R);
// if(x>0 && x<bg.width-1 && y>0 && y<bg.height-1) {
// int pa = ((int)x + (int)y*bg.width)*3;
// float r = ofRandom(0.8, 1.0);
// pixels[pa+0] = r*pixels[pa+0] + (1.0-r)*colors[((int)ln/20) % NUM_COL*3+0];
// pixels[pa+1] = r*pixels[pa+1] + (1.0-r)*colors[((int)ln/20) % NUM_COL*3+1];
// pixels[pa+2] = r*pixels[pa+2] + (1.0-r)*colors[((int)ln/20) % NUM_COL*3+2];
// }
// }

if(ofRandom(0.0, 100.0) > 99.5)
T[i] = ofPoint( ofRandom(ofGetWidth()), ofRandom(ofGetHeight()));
}
bg.update();

vector<ofRectangle> b = B;
vector<ofRectangle> b;
// occlude blobs
for (int i=0; i< B.size(); i++) {
float x = B[i].getCenter().x;
if((x<oclx1 || x>oclx1+oclw) && (x<oclx2 || x>oclx2+oclw)) {
b.push_back(B[i]);
}
}

random_shuffle(b.begin(), b.end());
tracker.trackFaces(b);

Expand All @@ -99,10 +91,7 @@ void testApp::update(){
//--------------------------------------------------------------
void testApp::draw() {

ofSetColor(255);
bg.draw(0,0);
//fl.draw(0,0);


ofNoFill();

for (int i=0; i<B.size(); i++) {
Expand All @@ -115,7 +104,7 @@ void testApp::draw() {
ofSetColor(C[i], 80);
ofLine(B[i].getCenter(), T[i]);
}

ofFill();
for (int i=0; i<tracker.trackedFaces.size(); i++) {
ofRectangle& f = tracker.trackedFaces[i];
Expand All @@ -126,6 +115,12 @@ void testApp::draw() {
ofSetColor(colors[i%21*3], colors[i%21*3+1], colors[i%21*3+2]);
ofDrawBitmapString(ofToString(i)+"-"+ofToString(tracker.trackedFacesCounter[i]/10), f.x + 10 , f.y-2);
}

// occlusions
ofSetColor(40, 200);
ofRect(oclx1, 0, oclw, ofGetHeight());
ofRect(oclx2, 0, oclw, ofGetHeight());

ofSetColor(255);
ofDrawBitmapString(tracker.debug, 10 , 20);

Expand Down
5 changes: 4 additions & 1 deletion tests/FaceTrackerTest/testApp.h
Expand Up @@ -29,7 +29,10 @@ class testApp : public ofBaseApp{

FaceTracker tracker;

ofImage bg;
float oclx1;
float oclx2;
float oclw ;

static unsigned char testApp::colors[];


Expand Down

0 comments on commit ba290b1

Please sign in to comment.