I recently discovered the
openframeworks toolkit. It is an open source C++ toolkit that wraps together many commonly used libraries for simple and fast experimentation. I absolutely love it!
When I shared code in the past, it took quite a bit of time to document what libraries, which version, and sometimes (even with C++) which operating system the new user must install for the code to compile properly. With
openframeworks, that annoyance is a thing of the past.
For this post I decided to make a quick and fun example adopted from one of openframeworks' provided examples on detecting faces in pictures. The example can be found under the folder: examples/addons/opencvHaarFinderExample.
I decided to extend their example to detect faces from a live webcam or a quicktime movie. I was amazed at how few lines of code it took. To get started,
download the appropriate toolkit for your IDE, here. Then copy the folder opencvHaarFinderExample in examples/addons/ to apps/myApps/. Then replace the code in testApp.cpp with the following:
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
camWidth = 480;
camHeight = 360;
//uncomment and comment lines below to test different detectors
finder.setup("haarcascade_frontalface_default.xml");
//finder.setup("haarcascade_frontalface_alt.xml");
//finder.setup("haarcascade_frontalface_alt2.xml");
//finder.setup("haarcascade_eyeglasses.xml");
//finder.setup("haarcascade_eye.xml");
#ifdef _USE_LIVE_VIDEO
VideoGrabber.setVerbose(true);
VideoGrabber.initGrabber(camWidth,camHeight,OF_IMAGE_COLOR);
colorImg.allocate(camWidth,camHeight,OF_IMAGE_COLOR);
#else
VideoPlayer.loadMovie("ThemeFromShaft.mov");
VideoPlayer.play();
#endif
}
//--------------------------------------------------------------
void testApp::update(){
ofBackground(100,100,100);
bool newFrame = false;
#ifdef _USE_LIVE_VIDEO
VideoGrabber.grabFrame();
newFrame = VideoGrabber.isFrameNew();
#else
VideoPlayer.idleMovie();
newFrame = VideoPlayer.isFrameNew();
#endif
if (newFrame){
#ifdef _USE_LIVE_VIDEO
colorImg.setFromPixels(VideoGrabber.getPixels(), camWidth, camHeight, OF_IMAGE_COLOR);
#else
colorImg.setFromPixels(VideoPlayer.getPixels(), camWidth, camHeight, OF_IMAGE_COLOR);
#endif
finder.findHaarObjects(colorImg);
}
}
//--------------------------------------------------------------
void testApp::draw(){
ofSetHexColor(0xffffff);
colorImg.draw(0,0);
ofNoFill();
for(int i = 0; i < finder.blobs.size(); i++) {
ofRectangle dim = finder.blobs[i].boundingRect;
ofRect(dim.x, dim.y, dim.width, dim.height);
}
ofSetHexColor(0xffffff);
char reportStr[1024];
sprintf(reportStr, "Number Detected: %lu fps: %f", finder.blobs.size(), ofGetFrameRate());
ofDrawBitmapString(reportStr, 20, 20+camHeight);
}
//--------------------------------------------------------------
void testApp::keyPressed (int key){
switch (key){
case ' ':
break;
}
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
Then replace the code in testApp.h with the following:
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
#include "ofxCvHaarFinder.h"
#define _USE_LIVE_VIDEO //comment to use video, uncomment to use webcam
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
int camWidth, camHeight;
#ifdef _USE_LIVE_VIDEO
ofVideoGrabber VideoGrabber;
#else
ofVideoPlayer VideoPlayer;
#endif
ofxCvHaarFinder finder;
ofImage colorImg;
};
#endif
For those mac users out there, just download
this, open /apps/myApps/HeadTracking/TrackingExample.xcodeproj in Xcode (3.2 and above) and hit run!
No comments:
Post a Comment