int _tmain(int argc, _TCHAR* argv[])
{
	 DTW dtw;

           cout << "Vector DTW Test" << endl;
           std::vector<LocalFeature> mainVec;
           std::vector<LocalFeature> testVec;
           LocalFeature p1(1, 2, 3);
           LocalFeature p2(2, 3, 4);

           LocalFeature p3(3, 2, 3);
           LocalFeature p4(2, 2, 3);

           mainVec.push_back(p1);
           mainVec.push_back(p2);
           mainVec.push_back(p4);

           testVec.push_back(p3);
           testVec.push_back(p4);

           GlobalFeature global(1,mainVec);
           dtw.addTrainingFeature(&global);

           GlobalFeature* result = dtw.getNextNeighbour(testVec);

           cout << "Distance: " << result->distance << endl;
           cout << "Label: " << result->classLabel << endl;

           system("PAUSE");
	return 0;
}
Example #2
0
DTW::DTW(const DTW& copy)
// Copy constructor
{
	this->myCompared = copy.myCompared;
	this->myComparison = copy.myComparison;
	this->setFirst(copy.getFirst());
	this->setSecond(copy.getSecond());
}
Example #3
0
//--------------------------------------------------------------
void testApp::setup(){

    ofSetFrameRate(30);

    //Initialize the training and info variables
    infoText = "";
    trainingClassLabel = 1;
    record = false;

    //Open the connection with Synapse
    synapseStreamer.openSynapseConnection();

    //Set which joints we want to track : we track only the hand joints
    synapseStreamer.trackAllJoints(false);
    synapseStreamer.trackLeftHand(true);
    synapseStreamer.trackRightHand(true);
    synapseStreamer.computeHandDistFeature(true);

    //The input to the training data will be the [x y z] of the two hands
    //so we set the number of dimensions to 6
    trainingData.setNumDimensions( 6 );
    trainingClassLabel = 1; // on ne s'occupe pas du trainingClassLabel pour la reconnaissance

    //Initialize the DTW classifier
    DTW dtw;

    //Turn on null rejection, this lets the classifier output the predicted class label of 0 when the likelihood of a gesture is low
    dtw.enableNullRejection( true );

    //Set the null rejection coefficient to 3, this controls the thresholds for the automatic null rejection
    //You can increase this value if you find that your real-time gestures are not being recognized
    //If you are getting too many false positives then you should decrease this value
    dtw.setNullRejectionCoeff( PRECISION_RECO );

    //Turn on the automatic data triming, this will remove any sections of none movement from the start and end of the training samples
    dtw.enableTrimTrainingData(true, 0.1, 90);

    //Offset the timeseries data by the first sample, this makes your gestures (more) invariant to the location the gesture is performed
    dtw.setOffsetTimeseriesUsingFirstSample(true);

    //Add the classifier to the pipeline (after we do this, we don't need the DTW classifier anymore)
    pipeline.setClassifier( dtw );

    //Load the data from TrainingData.txt, and train the pipeline
    if( trainingData.loadDatasetFromFile("TrainingData.txt") )
    {
        infoText = "Training data saved to file";
        if( pipeline.train( trainingData ) )
        {
            infoText = "Pipeline Trained";
        }
        else infoText = "WARNING: Failed to train pipeline";
    }
    else infoText = "WARNING: Failed to load training data from file";

}
bool GRT_Recognizer::initPipeline(string trainingdatafile, int dimension)
{
			    //Initialize the training and info variables
   // infoText = "";
   // trainingClassLabel = 1;
   // noOfHands = 2;
	//noOfTrackedHands = 0;
    
	
	//The input to the training data will be the R[x y z]L[x y z] from the left end right hand
	// so we set the number of dimensions to 6
	LabelledTimeSeriesClassificationData trainingData; 
    //trainingData.setNumDimensions(6);
	trainingData.loadDatasetFromFile(trainingdatafile);
    
    //Initialize the DTW classifier
    DTW dtw;
    
    //Turn on null rejection, this lets the classifier output the predicted class label of 0 when the likelihood of a gesture is low
    dtw.enableNullRejection( true);
    
    //Set the null rejection coefficient to 3, this controls the thresholds for the automatic null rejection
    //You can increase this value if you find that your real-time gestures are not being recognized
    //If you are getting too many false positives then you should decrease this value
    dtw.setNullRejectionCoeff(2);

    
    //Turn on the automatic data triming, this will remove any sections of none movement from the start and end of the training samples
    dtw.enableTrimTrainingData(true, 0.1, 90);
    
    //Offset the timeseries data by the first sample, this makes your gestures (more) invariant to the location the gesture is performed
    dtw.setOffsetTimeseriesUsingFirstSample(true);
  

    //Add the classifier to the pipeline (after we do this, we don't need the DTW classifier anymore)
    pipeline.setClassifier( dtw );
	//pipeline.addPreProcessingModule(MovingAverageFilter(5,dimension));
	//pipeline.addFeatureExtractionModule(FFT(16,1, dimension));
	/*ClassLabelFilter myFilter = ClassLabelFilter();
	myFilter.setBufferSize(6);
	myFilter.setBufferSize(2);*/

	pipeline.addPostProcessingModule(ClassLabelChangeFilter());
	pipeline.train(trainingData);

	return true;
}
int main (int argc, const char * argv[])
{
    TimeSeriesClassificationData trainingData;      //This will store our training data

    GestureRecognitionPipeline pipeline;               //This is a wrapper for our classifier and any pre/post processing modules

	string dirPath = "/home/vlad/AndroidStudioProjects/DataCapture/dataSetGenerator/build";

	if (!trainingData.loadDatasetFromFile(dirPath + "/acc-training-set-segmented.data")) {
		printf("Cannot open training segmented set\n");
		return 0;
	}

	printf("Successfully opened training data set ...\n");

    DTW dtw;
//    LowPassFilter lpf(0.1, 1, 1);
//    pipeline.setPreProcessingModule(lpf);

//    DoubleMovingAverageFilter filter( 1000, 3 );
//    pipeline.setPreProcessingModule(filter);

    //dtw.enableNullRejection( true );

    //Set the null rejection coefficient to 3, this controls the thresholds for the automatic null rejection
	//You can increase this value if you find that your real-time gestures are not being recognized
	//If you are getting too many false positives then you should decrease this value
    //dtw.setNullRejectionCoeff( 5 );
    dtw.enableTrimTrainingData(true, 0.1, 90);
//    dtw.setOffsetTimeseriesUsingFirstSample(true);

    pipeline.setClassifier( dtw );

    UINT KFolds = 5;

    /* Separate input dataset using KFold */
    KfoldTimeSeriesData* kFoldTS  = new KfoldTimeSeriesData(trainingData);
	if( !kFoldTS->spiltDataIntoKFolds(KFolds) ) {
		printf("BaseTGTestModel: Failed to spiltDataIntoKFolds!");
		return 0;
	}

	UINT maxTrainigSetSize = trainingData.getNumSamples() * (KFolds - 1) / (KFolds * trainingData.getNumClasses());


	// KFolds


	ofstream myfile;
	  myfile.open ("example.txt");
	Float acc = 0;
	for (GRT::UINT k = 1 ; k < KFolds; k++) {

		printf("Running tests for: %d fold", k);
		//  maxTrainigSetSize
//		for (UINT trainingSetSize = 1; trainingSetSize <= maxTrainigSetSize; trainingSetSize ++) {

			/* Set up training datasets for current fold */
			TimeSeriesClassificationData trainingDataset = kFoldTS->getTrainingFoldData(k, maxTrainigSetSize);

			/* Set up validation datasets for current fold */
			TimeSeriesClassificationDataStream testDataset = kFoldTS->getTestFoldData(k);

			/* Log test dataset size */
			//printf("Data set size: training %d; testing %d",
//					trainingDataset.getNumSamples(), testDataset.getNumSamples());

			/* Run test for current fold */
			pipeline.train(trainingDataset);
			pipeline.test(testDataset);
			myfile << pipeline.getTestAccuracy() << "\n";
//		}
	}

	  myfile.close();

	printf("Accuracy = %f ; %d\n", acc, maxTrainigSetSize);

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

    ofSetFrameRate(30);

    //Initialize the training and info variables
    infoText = "";
    trainingClassLabel = 1;
    record = false;
    noOfHands = 1;
    noOfTrackedHands = 0;
    //The input to the training data will be the [x y] from the mouse, so we set the number of dimensions to 2
    trainingData.setNumDimensions( noOfHands*3 );
    //trainingData.setNumDimensions( 3 );

    //Initialize the DTW classifier
    DTW dtw;

    //Turn on null rejection, this lets the classifier output the predicted class label of 0 when the likelihood of a gesture is low
    dtw.enableNullRejection( true );

    //Set the null rejection coefficient to 3, this controls the thresholds for the automatic null rejection
    //You can increase this value if you find that your real-time gestures are not being recognized
    //If you are getting too many false positives then you should decrease this value
    dtw.setNullRejectionCoeff( 3 );


    //Turn on the automatic data triming, this will remove any sections of none movement from the start and end of the training samples
    dtw.enableTrimTrainingData(true, 0.1, 90);

    //Offset the timeseries data by the first sample, this makes your gestures (more) invariant to the location the gesture is performed
    dtw.setOffsetTimeseriesUsingFirstSample(true);


    //Add the classifier to the pipeline (after we do this, we don't need the DTW classifier anymore)
    pipeline.setClassifier( dtw );

    //pipeline.inputVectorDimensions(3*noOfHands);

    ///setup nite


    niteRc = nite::NiTE::initialize();
    if (niteRc != nite::STATUS_OK)
    {
        printf("NiTE initialization failed\n");
        return;
    }

    niteRc = handTracker.create();
    if (niteRc != nite::STATUS_OK)
    {
        printf("Couldn't create user tracker\n");
        return;
    }

    handTracker.startGestureDetection(nite::GESTURE_CLICK);

    printf("\nPoint with your hand to start tracking it...\n");

    //put cursor in the middle of the screen: SPI_GETWORKAREA
    int screenX = GetSystemMetrics(SM_CXSCREEN);
    int screenY = GetSystemMetrics(SM_CYSCREEN);
    SetCursorPos(screenX / 2, screenY / 2);
    xcursorpos = 500;
    ycursorpos = 500;

}
//--------------------------------------------------------------
void testApp::draw() {

    ofBackground(0, 0, 0);

    string text;
    int textX = 20;
    int textY = 20;

    //Draw the training info
    ofSetColor(255, 255, 255);
    text = "------------------- TrainingInfo -------------------";
    ofDrawBitmapString(text, textX,textY);

    if( record ) ofSetColor(255, 0, 0);
    else ofSetColor(255, 255, 255);
    textY += 15;
    text = record ? "RECORDING" : "Not Recording";
    ofDrawBitmapString(text, textX,textY);

    ofSetColor(255, 255, 255);
    textY += 15;
    text = "TrainingClassLabel: " + ofToString(trainingClassLabel);
    ofDrawBitmapString(text, textX,textY);

    textY += 15;
    text = "NumTrainingSamples: " + ofToString(trainingData.getNumSamples());
    ofDrawBitmapString(text, textX,textY);


    //Draw the prediction info
    textY += 30;
    text = "------------------- Prediction Info -------------------";
    ofDrawBitmapString(text, textX,textY);

    textY += 15;
    text =  pipeline.getTrained() ? "Model Trained: YES" : "Model Trained: NO";
    ofDrawBitmapString(text, textX,textY);

    textY += 15;
    text = "PredictedClassLabel: " + ofToString(pipeline.getPredictedClassLabel());
    ofDrawBitmapString(text, textX,textY);

    textY += 15;
    text = "Likelihood: " + ofToString(pipeline.getMaximumLikelihood());
    ofDrawBitmapString(text, textX,textY);

    textY += 15;
    text = "SampleRate: " + ofToString(ofGetFrameRate(),2);
    ofDrawBitmapString(text, textX,textY);


    //Draw the info text
    textY += 30;
    text = "InfoText: " + infoText;
    ofDrawBitmapString(text, textX,textY);

    //Draw number of hands currently dragged
    textY += 15;
    text = "Number of tracked hands: "+ ofToString(noOfTrackedHands);
    ofSetColor(255, 0, 0);
    ofDrawBitmapString(text, textX,textY);
    ofSetColor(255, 255, 255);

    //Draw the timeseries data
    if( record  && noOfHands == noOfTrackedHands) {
        ofFill();
        for(UINT i=0; i<timeseries.getNumRows(); i++) {
            double x = timeseries[i][0];
            double y = timeseries[i][1];
            //double r = ofMap(i,0,timeseries.getNumRows(),0,255);
            //double g = 0;
            //double b = 255-r;
            //ofSetColor(r,g,b);
            ofSetColor(250,0,0);
            ofEllipse(300+x,500-y,5,5);

            if(noOfHands >= 2) {
                ofSetColor(0,80,255);
                x = timeseries[i][3];
                y = timeseries[i][4];
                ofEllipse(300+x,500-y,5,5);
            }
        }
    }

    if( pipeline.getTrained()  && noOfHands == noOfTrackedHands) {

        //Draw the data in the DTW input buffer
        DTW *dtw = pipeline.getClassifier< DTW >();

        if( dtw != NULL ) {
            vector< VectorDouble > inputData = dtw->getInputDataBuffer();
            for(UINT i=0; i<inputData.size(); i++) {
                double x = inputData[i][0];
                double y = inputData[i][1];
                //  double r = ofMap(i,0,inputData.size(),0,255);
                //  double g = 0;
                //   double b = 255-r;

                ofSetColor(250,0,0);
                ofEllipse(300+x,500-y,5,5);

                if(noOfHands >= 2) {
                    ofSetColor(0,80,250);
                    x = inputData[i][3];
                    y = inputData[i][4];
                    ofEllipse(300+x,500-y,5,5);
                }
            }
        }
    }

}
Example #8
0
int main (int argc, const char * argv[])
{
	//Create a new DTW instance, using the default parameters
	DTW dtw;
    
	//Load some training data to train the classifier - the DTW uses TimeSeriesClassificationData
	TimeSeriesClassificationData trainingData;
    
	if( !trainingData.load("DTWTrainingData.grt") ){
		cout << "Failed to load training data!\n";
		return EXIT_FAILURE;
	}
    
	//Use 20% of the training dataset to create a test dataset
	TimeSeriesClassificationData testData = trainingData.partition( 80 );

	//Trim the training data for any sections of non-movement at the start or end of the recordings
	dtw.enableTrimTrainingData(true,0.1,90);
    
	//Train the classifier
	if( !dtw.train( trainingData ) ){
		cout << "Failed to train classifier!\n";
		return EXIT_FAILURE;
	}	
    
	//Save the DTW model to a file
	if( !dtw.save("DTWModel.grt") ){
		cout << "Failed to save the classifier model!\n";
		return EXIT_FAILURE;
	}
    
	//Load the DTW model from a file
	if( !dtw.load("DTWModel.grt") ){
		cout << "Failed to load the classifier model!\n";
		return EXIT_FAILURE;
	}
    
	//Use the test dataset to test the DTW model
	double accuracy = 0;
	for(UINT i=0; i<testData.getNumSamples(); i++){
		//Get the i'th test sample - this is a timeseries
		UINT classLabel = testData[i].getClassLabel();
		MatrixDouble timeseries = testData[i].getData();
        
		//Perform a prediction using the classifier
		if( !dtw.predict( timeseries ) ){
			cout << "Failed to perform prediction for test sampel: " << i <<"\n";
			return EXIT_FAILURE;
		}
        
		//Get the predicted class label
		UINT predictedClassLabel = dtw.getPredictedClassLabel();
		double maximumLikelihood = dtw.getMaximumLikelihood();
		VectorDouble classLikelihoods = dtw.getClassLikelihoods();
		VectorDouble classDistances = dtw.getClassDistances();
        
		//Update the accuracy
		if( classLabel == predictedClassLabel ) accuracy++;
        
        cout << "TestSample: " << i <<  "\tClassLabel: " << classLabel << "\tPredictedClassLabel: " << predictedClassLabel << "\tMaximumLikelihood: " << maximumLikelihood << endl;
	}
    
	cout << "Test Accuracy: " << accuracy/double(testData.getNumSamples())*100.0 << "%" << endl;
    
	return EXIT_SUCCESS;
}
Example #9
0
int main() {
    vector<string> gestures(0,"");
    GetFilesInDirectory(gestures, "rawdata");
    CreateDirectory("processed", NULL);
    sort(gestures.begin(), gestures.end());
    data = vector<vector<vector<double > > >(gestures.size(), vector<vector<double > >(0,vector<double>(0,0)));
    for(size_t i = 0; i < gestures.size(); i++) {
        ifstream fin(gestures[i]);
        int n; fin >> n;
       // cerr << gestures[i] << endl;
       // cerr << n << endl;
        data[i] = vector<vector<double> >(n, vector<double>(NUMPARAM, 0));
        for(int j = 0; j < n; j++) {
            for(int k = 0; k < NUMPARAM; k++) {
                fin >> data[i][j][k];
            }
        }
        fin.close();
    }


    //Create a new instance of the TimeSeriesClassificationDataStream
    TimeSeriesClassificationData trainingData;

    // ax, ay, az
    trainingData.setNumDimensions(3);
    trainingData.setDatasetName("processed\\GestureTrainingData.txt");
    ofstream labelfile("processed\\GestureTrainingDataLabels.txt");
    UINT currLabel = 1;
    Random random;
    map<string, int> gesturenames;
    for(size_t overall = 0; overall < gestures.size(); overall++) {

        string nam = gestures[overall].substr(8,gestures[overall].find_first_of('_')-8);
        if(gesturenames.count(nam)) currLabel = gesturenames[nam];
        else {
            currLabel = gesturenames.size()+1;
            gesturenames[nam] = currLabel;
            labelfile << currLabel << " " << nam << endl;
        }
        MatrixDouble trainingSample;
        VectorDouble currVec( trainingData.getNumDimensions() );
        for(size_t k = 1; k < data[overall].size(); k++) {
            for(UINT j=0; j<currVec.size(); j++){
                currVec[j] = data[overall][k][j];
            }
            trainingSample.push_back(currVec);
        }
        trainingData.addSample(currLabel, trainingSample);

    }
    for(size_t i = 0; i < gestures.size(); i++) {
        MatrixDouble trainingSample;
        VectorDouble currVec(trainingData.getNumDimensions());
        for(UINT j = 0; j < currVec.size(); j++) {
            currVec[j] = random.getRandomNumberUniform(-1.0, 1.0);
        }
        for(size_t k = 0; k < 100; k++) {
            trainingSample.push_back(currVec);
        }
        trainingData.addSample(0, trainingSample);
    }

    //After recording your training data you can then save it to a file
    if( !trainingData.save( "processed\\TrainingData.grt" ) ){
        cout << "ERROR: Failed to save dataset to file!\n";
        return EXIT_FAILURE;
    }

    //This can then be loaded later
    if( !trainingData.load( "processed\\TrainingData.grt" ) ){
        cout << "ERROR: Failed to load dataset from file!\n";
        return EXIT_FAILURE;
    }

    //This is how you can get some stats from the training data
    string datasetName = trainingData.getDatasetName();
    string infoText = trainingData.getInfoText();
    UINT numSamples = trainingData.getNumSamples();
    UINT numDimensions = trainingData.getNumDimensions();
    UINT numClasses = trainingData.getNumClasses();

    cout << "Dataset Name: " << datasetName << endl;
    cout << "InfoText: " << infoText << endl;
    cout << "NumberOfSamples: " << numSamples << endl;
    cout << "NumberOfDimensions: " << numDimensions << endl;
    cout << "NumberOfClasses: " << numClasses << endl;

    //You can also get the minimum and maximum ranges of the data
    vector< MinMax > ranges = trainingData.getRanges();

    cout << "The ranges of the dataset are: \n";
    for(UINT j=0; j<ranges.size(); j++){
        cout << "Dimension: " << j << " Min: " << ranges[j].minValue << " Max: " << ranges[j].maxValue << endl;
    }

    DTW dtw;

    if( !dtw.train( trainingData ) ){
        cerr << "Failed to train classifier!\n";
        exit(EXIT_FAILURE);
    }
    dtw.enableNullRejection(true);
    dtw.setNullRejectionCoeff(4);
    dtw.enableTrimTrainingData(true, 0.1, 90);
    //Save the DTW model to a file
    if( !dtw.saveModelToFile("processed\\DTWModel.txt") ){
        cerr << "Failed to save the classifier model!\n";
        exit(EXIT_FAILURE);
    }

    trainingData.clear();

    return EXIT_SUCCESS;
}
Example #10
0
int* Querybh::MatchChroma(int* target, int targetsize)
{
    int p0[] = {2,2,0,0,2,9,9,9,9,9,11,1,1,11,11,0,1,1,9,9,9,10,1,1,1,0,11,10,10,9,9,9,9,11,1,0,0,11,11,1,1,9,9,9};
    int p1[] = {8,8,8,8,7,7,7,8,8,8,8,8,5,5,5,5,5,5,5,11,6,6,6,9,9,9,4,4,9,9,9,8,8,8,8,8,8,8,9,9,6,6,6,6,6,6,6,9,9,9};
    int p2[] = {1,3,3,6,11,10,10,10,11,2,2,3,3,6,11,10,10,10,10,1,1,1,11,11,11,11,11,11,11,5,5,7,7,7,7,4,1,1,2,2,2};
    int p3[] = {10,10,7,7,7,7,7,7,7,7,7,10,0,5,5,5,5,5,5,4,4,4,7,7,8,8,8,10,10,3,1,1,2,2,9,10,0,0,9,7,7,7,8,0,11,11,1,1,1,0,0,0,4,3,3,3,3,0,10,10,10,10,4,4,4,6,7,7,11,10,10,10,10,11,9,9,7,8,3,3,3};
    
    int** p;
    p = new int*[4];
    p[0] = new int[sizeof(p0)/sizeof(int)];
    p[1] = new int[sizeof(p1)/sizeof(int)];
    p[2] = new int[sizeof(p2)/sizeof(int)];
    p[3] = new int[sizeof(p3)/sizeof(int)];
    
    
    p[0] = p0;
    p[1] = p1;
    p[2] = p2;
    p[3] = p3;

    
    songlist.push_back("here comes the sun.wav");
    songlist.push_back("someone like you.wav");
    songlist.push_back("goodbye.wav");
    songlist.push_back("hey jude.wav");
    
    lengthlist.push_back(sizeof(p0)/sizeof(int));
    lengthlist.push_back(sizeof(p1)/sizeof(int));
    lengthlist.push_back(sizeof(p2)/sizeof(int));
    lengthlist.push_back(sizeof(p3)/sizeof(int));


    int* midp = new int[lengthlist.size()];
    for(int i=0; i<lengthlist.size(); i++)
        midp[i] = mid(p[i], lengthlist[i]);

    int midpt = mid(target, targetsize);
    
    for (int i=0; i<lengthlist.size(); i++){
        for(int j=0; j<lengthlist[i]; j++){
            p[i][j] = (p[i][j]-midp[i]+midpt+12)%12;
          //  cout<<p[i][j]<<",";
        }
     //   cout<<endl<<endl;
    }

    for (int i=0; i<lengthlist.size(); i++)
        ps.push_back(p[i]);
    
    vector<double> dist;
    dist.clear();
//    vector<double> dtemp;
//    for (int k=0; k<songlist.size(); k++){
//        dtemp.clear();
//        int pp=0;
//        if((lengthlist[k]-targetsize)>4){
//            while (1) {
//                DTW *mDTW = NULL;
//                mDTW = new DTW(targetsize, targetsize+5);
//                dtemp.push_back(mDTW->run(target, targetsize, &ps[k][pp], targetsize+5));
//                pp++;
//                if(pp+targetsize+5>lengthlist[k])
//                    break;
//                delete mDTW;
//            }
//        }
//        else if ((targetsize-lengthlist[k])>4){
//            while (1) {
//                DTW *mDTW = NULL;
//                mDTW = new DTW(lengthlist[k]+5, lengthlist[k]);
//                dtemp.push_back(mDTW->run(&target[pp], lengthlist[k]+5, &ps[k][0], lengthlist[k]));
//                pp++;
//                if(pp+lengthlist[k]+5>targetsize)
//                    break;
//                delete mDTW;
//            }
//        }
//        
//        else{
//            DTW *mDTW = NULL;
//            mDTW = new DTW(targetsize, targetsize);
//            dtemp.push_back(mDTW->run(target, targetsize, &ps[k][0], targetsize));
//            delete mDTW;
//        }
//        dist.push_back(MinVal(dtemp));
    for (int k=0; k<lengthlist.size(); k++){
        DTW *mDTW = NULL;
        mDTW = new DTW(targetsize, lengthlist[k]);
        dist.push_back(mDTW->run(target, targetsize, &ps[k][0], lengthlist[k]));
        delete mDTW;
    }
    return GetMinIndex(dist);
}