Android Open Source - wiigee_android_test Triggered Processing Unit






From Project

Back to project page wiigee_android_test.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project wiigee_android_test listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * wiigee - accelerometerbased gesture recognition
 * Copyright (C) 2007, 2008, 2009 Benjamin Poppinga
 * //from  w  w  w .j a v  a 2  s . c  o  m
 * Developed at University of Oldenburg
 * Contact: wiigee@benjaminpoppinga.de
 *
 * This file is part of wiigee.
 *
 * wiigee is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package org.wiigee.logic;

import java.util.Vector;
import org.wiigee.event.*;
import org.wiigee.util.Log;

/**
 * This class analyzes the AccelerationEvents emitted from a Wiimote
 * and further creates and manages the different models for each type
 * of gesture. 
 * 
 * @author Benjamin 'BePo' Poppinga
 */
public class TriggeredProcessingUnit extends ProcessingUnit {

  // gesturespecific values
  private Gesture current; // current gesture

  private Vector<Gesture> trainsequence;
  
  // State variables
  private boolean learning, analyzing;
  
  public TriggeredProcessingUnit() {
    super();
    this.learning=false;
    this.analyzing=false;
    this.current=new Gesture();
    this.trainsequence=new Vector<Gesture>();
  }

  /** 
   * Since this class implements the WiimoteListener this procedure is
   * necessary. It contains the filtering (directional equivalence filter)
   * and adds the incoming data to the current motion, we want to train
   * or recognize.
   * 
   * @param event The acceleration event which has to be processed by the
   * directional equivalence filter and which has to be added to the current
   * motion in recognition or training process.
   */
  public void accelerationReceived(AccelerationEvent event) {
    if(this.learning || this.analyzing) {
      this.current.add(event); // add event to gesture      
    }    
  }

  /** 
   * This method is from the WiimoteListener interface. A button press
   * is used to control the data flow inside the structures. 
   * 
   */
  public void buttonPressReceived(ButtonPressedEvent event) {
    this.handleStartEvent(event);
  }

  public void buttonReleaseReceived(ButtonReleasedEvent event) {
    this.handleStopEvent(event);
  }
  
  public void motionStartReceived(MotionStartEvent event) {
    // this.handleStartEvent(event);
  }
  
  public void motionStopReceived(MotionStopEvent event) {
    // this.handleStopEvent(event);
  }
  
  public void handleStartEvent(ActionStartEvent event) {
    
    // TrainButton = record a gesture for learning
    if((!this.analyzing && !this.learning) && 
      event.isTrainInitEvent()) {
      Log.write("Training started!");
      this.learning=true;
    }
    
    // RecognitionButton = record a gesture for recognition
    if((!this.analyzing && !this.learning) && 
      event.isRecognitionInitEvent()) {
      Log.write("Recognition started!");
      this.analyzing=true;
    }
      
    // CloseGestureButton = starts the training of the model with multiple
    // recognized gestures, contained in trainsequence
    if((!this.analyzing && !this.learning) && 
      event.isCloseGestureInitEvent()) {
    
      if(this.trainsequence.size()>0) {
        Log.write("Training the model with "+this.trainsequence.size()+" gestures...");
        this.learning=true;
        
        GestureModel m = new GestureModel();
        m.train(this.trainsequence);
        m.print();
        this.classifier.addGestureModel(m);
        
        this.trainsequence=new Vector<Gesture>();
        this.learning=false;
      } else {
        Log.write("There is nothing to do. Please record some gestures first.");
      }
    }
  }
  
  public void handleStopEvent(ActionStopEvent event) {
    if(this.learning) { // button release and state=learning, stops learning
      if(this.current.getCountOfData()>0) {
        Log.write("Finished recording (training)...");
        Log.write("Data: "+this.current.getCountOfData());
        Gesture gesture = new Gesture(this.current);
        this.trainsequence.add(gesture);
        this.current=new Gesture();
        this.learning=false;
      } else {
        Log.write("There is no data.");
        Log.write("Please train the gesture again.");
        this.learning=false; // ?
      }
    }
    
    else if(this.analyzing) { // button release and state=analyzing, stops analyzing
      if(this.current.getCountOfData()>0) {
        Log.write("Finished recording (recognition)...");
        Log.write("Compare gesture with "+this.classifier.getCountOfGestures()+" other gestures.");
        Gesture gesture = new Gesture(this.current);
        
        int recognized = this.classifier.classifyGesture(gesture);
        if(recognized!=-1) {
          double recogprob = this.classifier.getLastProbability();
          this.fireGestureEvent(true, recognized, recogprob);
          Log.write("######");
          Log.write("Gesture No. "+recognized+" recognized: "+recogprob);
          Log.write("######");
        } else {
          this.fireGestureEvent(false, 0, 0.0);
          Log.write("######");
          Log.write("No gesture recognized.");
          Log.write("######");
        }
        
        this.current=new Gesture();
        this.analyzing=false;
      } else {
        Log.write("There is no data.");
        Log.write("Please recognize the gesture again.");
        this.analyzing=false; // ?
      }
    }
  }

  @Override
  public void loadGesture(String filename) {
    GestureModel g = org.wiigee.util.FileIO.readFromFile(filename);
    this.classifier.addGestureModel(g);  
  }

  @Override
  public void saveGesture(int id, String filename) {
    org.wiigee.util.FileIO.writeToFile(this.classifier.getGestureModel(id), filename);
  }

}




Java Source Code List

asus4.wiigeeandroid.Logger.java
asus4.wiigeeandroid.MainActivity.java
org.wiigee.control.AndroidWiigee.java
org.wiigee.control.Wiigee.java
org.wiigee.device.AndroidDevice.java
org.wiigee.device.Device.java
org.wiigee.event.AccelerationEvent.java
org.wiigee.event.AccelerationListener.java
org.wiigee.event.ActionStartEvent.java
org.wiigee.event.ActionStopEvent.java
org.wiigee.event.ButtonListener.java
org.wiigee.event.ButtonPressedEvent.java
org.wiigee.event.ButtonReleasedEvent.java
org.wiigee.event.GestureEvent.java
org.wiigee.event.GestureListener.java
org.wiigee.event.MotionStartEvent.java
org.wiigee.event.MotionStopEvent.java
org.wiigee.filter.DirectionalEquivalenceFilter.java
org.wiigee.filter.Filter.java
org.wiigee.filter.HighPassFilter.java
org.wiigee.filter.IdleStateFilter.java
org.wiigee.filter.LowPassFilter.java
org.wiigee.filter.MotionDetectFilter.java
org.wiigee.logic.Classifier.java
org.wiigee.logic.GestureModel.java
org.wiigee.logic.Gesture.java
org.wiigee.logic.HMM.java
org.wiigee.logic.PreciseHMM.java
org.wiigee.logic.ProcessingUnit.java
org.wiigee.logic.Quantizer.java
org.wiigee.logic.TriggeredProcessingUnit.java
org.wiigee.util.FileIO.java
org.wiigee.util.Log.java