Computer Graphics and Multimedia Software - Knowledge Sharing

Monday, November 20, 2017

openframeworks basic tutorial LeapMotion gesture recognition

The following repository version could be built without problems or you could also receive directly from openframeworks addon.
First of all, as a Hello World-like sample, try to recognize and display the hand. Only the fingertip could be detected independently! Easy!
There are two ways available for ofxLeapMotion. The first method is to use a simple hand model called ofxLeapMotionSimpleHand defined in ofxLeapMotion. The second method is to access and display functions provided by LeapMotion's SDK.
First of all, I try to draw by using a simple ofxLeapMotionSimpleHand. The way of doing it is very simple, once you have recognized ofxLeapMotionSimpleHand, calling a method called debugDraw () on that instance will display a simple 3D model of the hand.

testApp.cpp

#include "testApp.h"

void testApp::setup(){
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(31);

ofEnableLighting();
light.setPosition(200, 300, 50);
light.enable();
cam.setOrientation(ofPoint(-20, 0, 0));

glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
leap.open();
}

void testApp::update(){

simpleHands = leap.getSimpleHands();

if( leap.isFrameNew() && simpleHands.size() ){
fingerPos.clear();

leap.setMappingX(-230, 230, -ofGetWidth()/2, ofGetWidth()/2);
leap.setMappingY(90, 490, -ofGetHeight()/2, ofGetHeight()/2);
leap.setMappingZ(-150, 150, -200, 200);

for(int i = 0; i < simpleHands.size(); i++){
for(int j = 0; j < simpleHands[i].fingers.size(); j++){
ofVec3f pos = simpleHands[i].fingers[j].pos;
fingerPos.push_back(pos);
}
}
}
leap.markFrameAsOld();
}

void testApp::draw(){
cam.begin();
for(int i = 0; i < fingerPos.size(); i++){
ofBoxPrimitive box;
box.setPosition(fingerPos[i].x, fingerPos[i].y, fingerPos[i].z);
box.set(20);
box.draw();
}
cam.end();
}

testApp.h

#include "ofMain.h"
#include "ofxLeapMotion.h"

class testApp : public ofBaseApp{

public:
void setup();
void update();
void draw();

void keyPressed (int key);
void keyReleased(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);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void exit();

ofxLeapMotion leap;
vector simpleHands;
ofEasyCam cam;
ofLight light;
vector fingerPos;
};

The output : 


Share:

openFrameworks with Leap Motion example

ofxLeapMotion is an addon to work with openFrameworks and makes it very easy to get hand coordinates by using the prepared ofxLeapMotionSimpleHand class. Nonetheless, it seems that it is not fulfilling enough to utilize the full function provided by Leap Motion's SDK.
However, ofxLeapMotion also provides a way to directly obtain information on the Leap Motion SDK. Using this makes it possible to acquire more varied information.For details, refer to Documentation provided by Leap Motion .
Here I am providing the sample below, we first acquire the hand of Leap, obtain the position of each finger, then obtain and display information on the virtual sphere wrapped around the whole finger.

testApp.h

#include "ofMain.h"
#include "ofxLeapMotion.h"

class testApp: public ofBaseApp {

public:
void setup();
void update();
void draw();

void keyPressed (int key);
void keyReleased(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);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
void exit();

// the main class of Leap Motion
// vector simpleHands;

// vector array of simple hand model
ofEasyCam cam; // Camera
ofLight light; // Write
vector fingerPos; / / Array of position of fingers
vector spherePos; // array of positions of spheres surrounded by hands
vector sphereSize; // Array of sphere sizes surrounded by hands
};


testApp.cpp

#include "testApp.h"

void testApp::setup(){

ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(31);
ofEnableLighting();
light.setPosition(200, 300, 50);
light.enable();
cam.setOrientation(ofPoint(-20, 0, 0));
glEnable(GL_DEPTH_TEST);

// Leap Motion
leap.open();
}

void testApp::update(){
vector hands = leap.getLeapHands();
if( leap.isFrameNew() && hands.size() ){
fingerPos.clear();
spherePos.clear();
sphereSize.clear();

leap.setMappingX(-230, 230, -ofGetWidth()/2, ofGetWidth()/2);
leap.setMappingY(90, 490, -ofGetHeight()/2, ofGetHeight()/2);
leap.setMappingZ(-150, 150, -200, 200);

for(int i = 0; i < hands.size(); i++){
for(int j = 0; j < hands[i].fingers().count(); j++){
ofVec3f pt;
const Finger & finger = hands[i].fingers()[j];
pt = leap.getMappedofPoint( finger.tipPosition() );
fingerPos.push_back(pt);
}

ofVec3f sp = leap.getMappedofPoint(hands[i].sphereCenter());
float r = hands[i].sphereRadius();
spherePos.push_back(sp);
sphereSize.push_back(r);
}
}
leap.markFrameAsOld();
}

void testApp::draw(){
cam.begin();
for(int i = 0; i < fingerPos.size(); i++){
ofSpherePrimitive sphere;
sphere.setPosition(fingerPos[i].x, fingerPos[i].y, fingerPos[i].z);
sphere.draw();
}

for(int i = 0; i < spherePos.size(); i++){
ofSpherePrimitive sphere;
sphere.setPosition(spherePos[i].x, spherePos[i].y, spherePos[i].z);
sphere.setRadius(sphereSize[i]*1.5);
sphere.draw();
}
cam.end();
}

The output : 



Share:

Malaysia Augmented Reality

Its incredibly difficult to discover the undiscovered. Here will see how I can help you. Cheers - Ajune (comp.utm.my/ajune)
ajune@utm.my. Powered by Blogger.