I am creating a Java program to recognize some features from LeapMotion.
I am following this tutorial.
Code
import com.leapmotion.leap.*;
import com.leapmotion.leap.Gesture.State;
import java.io.IOException;
//Listener
class LeapListener extends Listener {
public void onInit(Controller controller) {
System.out.println("Initialized");
}
public void onConnect(Controller controller) {
System.out.println("Connected to Motion Sensor");
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
}
public void onDisconnect(Controller controller) {
System.out.println("Motion Sensor disconnected");
}
public void onExit(Controller controller) {
System.out.println("Exited");
}
//Method for detection
public void onFrame(Controller controller) {
Frame frame = controller.frame();
//FRAME DATA always
/*System.out.println("Frame id: "+ frame.id()
+ ", Timestamp: " + frame.timestamp()
+ ", Hands: " + frame.hands().count()
+ ", Fingers: " + frame.fingers().count()
+ ", Tools: " + frame.tools().count()
+ ", Gestures: " + frame.gestures().count());*/
//HAND DATE shown when hand is detected
/*for (Hand hand: frame.hands()) {
String handType = hand.isLeft() ? "Left Hand" : "Right hand";
System.out.println(handType + " " + ", id: " + hand.id()
+ ", Palm position: " + hand.palmPosition());
Vector normal = hand.palmNormal();
Vector direction = hand.direction();
System.out.println("Pitch: " + Math.toDegrees(direction.pitch())
+ "Roll: " + Math.toDegrees(normal.roll())
+ "Yaw: " + Math.toDegrees(direction.yaw()));
}*/
//FINGER DATA shown when hand is detected
/*for (Finger finger: frame.fingers()) {
System.out.println("Finger Type: " + finger.type()
+ " ID: " + finger.id()
+ " Finger Length (mm): " + finger.length()
+ " Finger Width (mm): " + finger.width());
for (Bone.Type boneType : Bone.Type.values()){
Bone bone = finger.bone(boneType);
System.out.println("Bone Type: " + bone.type()
+ " Start: " + bone.prevJoint()
+ " End : " + bone.nextJoint()
+ " Direction: " + bone.direction());
}
}*/
//TOOL DATA
/*for (Tool tool: frame.tools()) {
System.out.println("Tool ID: " + tool.id()
+ " Tip Position: " + tool.tipPosition()
+ " Direction: " + tool.direction()
+ " Width (mm): " + tool.width()
+ " Touch Distance (mm): " + tool.touchDistance());
}*/
//GESTURE
GestureList gestures = frame.gestures();
for (int i = 0; i < gestures.count(); i++) {
Gesture gesture = gestures.get(i);
switch (gesture.type()) {
case TYPE_CIRCLE:
CircleGesture circle = new CircleGesture(gesture);
String clockWiseness;
if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI/4 ) {
clockWiseness = "clockwise";
} else {
clockWiseness = "counter-clockwise";
}
double sweptAngle = 0;
if (circle.state() != State.STATE_START) {
CircleGesture previous = new CircleGesture(controller.frame(1).gesture(circle.id()));
sweptAngle = (circle.progress() - previous.progress()) * 2 * Math.PI;
}
System.out.println("Circle ID: " + circle.id()
+ " State: " + circle.state()
// Progress = how many times did you make the circle
+ " Progress " + circle.progress()
+ " Radius: " + circle.radius()
+ " Angle: " + Math.toDegrees(sweptAngle)
+ " Direction: " + clockWiseness);
break;
case TYPE_SWIPE:
SwipeGesture swipe = new SwipeGesture(gesture);
System.out.println("Swipe ID: " + swipe.id()
+ " State: " + swipe.state()
+ " Swipe Position (mm): " + swipe.position()
+ " Direction: " + swipe.direction()
+ " Speed (mm/s): " + swipe.speed());
break;
case TYPE_SCREEN_TAP:
ScreenTapGesture screenTap = new ScreenTapGesture();
System.out.println("Tap ID: " + screenTap.id()
+ " State: " + screenTap.state()
+ " Position: " + screenTap.position()
+ " Direction: " + screenTap.direction());
break;
case TYPE_KEY_TAP:
KeyTapGesture keyTap = new KeyTapGesture();
System.out.println("Key ID: " + keyTap.id()
+ " State: " + keyTap.state()
+ " Position: " + keyTap.position()
+ " Direction: " + keyTap.direction());
break;
default:
System.out.println("Unkown gesture");
break;
}
}
}
}
public class LeapController {
public static void main(String[] args) {
//Initialize the listener
LeapListener listener = new LeapListener();
Controller controller = new Controller();
controller.addListener(listener);
System.out.println("Press enter to quit");
try {
System.in.read();
} catch (IOException e){
e.printStackTrace();
}
controller.removeListener(listener);
}
}
The code perfectly works while I need to show the data of my hands (commented in this code), but It never gets into the for
created to show the gestures. Do you know why?
These are the gestures that I am trying to recognize:
-