UINT GesturesAnalyzer::PredictGesture(const vector<vector<cv::Point3d>>& pSampleVec)
{
	GRT::MatrixDouble sample = GRT::MatrixDouble( );
	vector<double> vecTemp;
	UINT predictedClassLabel = 0;
	for(int iterator = 0; iterator < pSampleVec.size(); iterator++)
	{
		int x0 = pSampleVec[iterator][nite::JOINT_TORSO].x;
		int y0 = pSampleVec[iterator][nite::JOINT_TORSO].y;
		int z0 = pSampleVec[iterator][nite::JOINT_TORSO].z;

		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_RIGHT_HAND].x - x0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_RIGHT_HAND].y - y0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_RIGHT_HAND].z - z0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_LEFT_HAND].x - x0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_LEFT_HAND].y - y0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_LEFT_HAND].z - z0);

		sample.push_back(vecTemp);
		vecTemp.clear();
	}	

	if( !m_dtw.predict(sample))
	{
		printf("Failed prediction !\n");
	}
	else
	{
		predictedClassLabel = m_dtw.getPredictedClassLabel();
	}
	return predictedClassLabel;
}
예제 #2
0
void HiddenMarkovModels::setUpTrainingDataset() {
	/* Setup Quantizer */
	if (quantizer != NULL) {
		delete(quantizer);
	}
	quantizer = new GRT::KMeansQuantizer( trainingDataset.getNumDimensions(), NUM_SYMBOLS );

	//Train the quantizer using the training data
	if(!quantizer->train( trainingDataset ) ){
		throw TestModelException("HiddenMarkovModels: ERROR: Failed to train quantizer!");
	}

	//Quantize the training data
	GRT::LabelledTimeSeriesClassificationData quantizedTrainingData( 1 );

	for( GRT::UINT i=0; i<trainingDataset.getNumSamples(); i++){

		GRT::UINT classLabel = trainingDataset[i].getClassLabel();
		GRT::MatrixDouble quantizedSample;

		for(GRT::UINT j=0; j<trainingDataset[i].getLength(); j++) {
			quantizer->quantize( trainingDataset[i].getData().getRowVector(j) );
			quantizedSample.push_back( quantizer->getFeatureVector() );
		}

		if( !quantizedTrainingData.addSample(classLabel, quantizedSample) ){
			throw TestModelException("HiddenMarkovModels:  ERROR: Failed to quantize training data!");
		}

	}
	this->trainingDataset = quantizedTrainingData;
}
예제 #3
0
파일: iostream.cpp 프로젝트: tomfisher/ESP
void ASCIISerialStream::readSerial() {
    // TODO(benzh) This readSerial is running in a different thread
    // and performing a busy polling (100% CPU usage). Should be
    // optimized.
    int sleep_time = 10;
    ofLog() << "Serial port will be read every " << sleep_time << " ms";
    while (has_started_) {
        string s;

        do {
            while (serial_->available() < 1) {
                std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
            }
            s += serial_->readByte();
        } while(s[s.length() - 1] != '\n');

        //ofLog() << "read: '" << s << "'" << endl;

        if (data_ready_callback_ != nullptr) {
            istringstream iss(s);
            vector<double> data;
            double d;

            while (iss >> d) data.push_back(d);

            if (data.size() > 0) {
                data = normalize(data);

                GRT::MatrixDouble matrix;
                matrix.push_back(data);

                data_ready_callback_(matrix);
            }
        }
    }
HRESULT GesturesAnalyzer::DTWSaveSampleAndTrain(UINT pClasse, const vector<vector<cv::Point3d>>& pSampleVec)
{
	printf("Start saving DTW sample\n");
	GRT::MatrixDouble sample = GRT::MatrixDouble( );
	vector<double> vecTemp;
	for(int iterator = 0; iterator < pSampleVec.size(); iterator++)
	{
		int x0 = pSampleVec[iterator][nite::JOINT_TORSO].x;
		int y0 = pSampleVec[iterator][nite::JOINT_TORSO].y;
		int z0 = pSampleVec[iterator][nite::JOINT_TORSO].z;

		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_RIGHT_HAND].x - x0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_RIGHT_HAND].y - y0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_RIGHT_HAND].z - z0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_LEFT_HAND].x - x0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_LEFT_HAND].y - y0);
		vecTemp.push_back(pSampleVec[iterator][nite::JOINT_LEFT_HAND].z - z0);

		sample.push_back(vecTemp);
		vecTemp.clear();
	}	

	printf("Adding sample to DTW data\n");
	if( !m_DTWTrainingData.addSample(pClasse, sample))
	{
		printf("Failed to add DTW sample\n");
		return E_FAIL;
	}

	if( !m_DTWTrainingData.saveDatasetToFile("TrainingData\\DTWTrainingData.txt"))
	{
		printf("Failed to save DTW data\n");
		return E_FAIL;
	}

	printf("Training DTW classifier\n");
	if( !m_dtw.train( m_DTWTrainingData ) ){
		cout << "Failed to train DTW classifier!\n";
		return E_FAIL;
	} 
	else
	{
		printf("Succesfully trained DTW Classifier\n");
	}
	return S_OK;
}
예제 #5
0
void HiddenMarkovModels::setUpTestingDataset() {
	//Quantize the test data
	GRT::LabelledTimeSeriesClassificationData quantizedTestData( 1 );

	for(GRT::UINT i=0; i < testDataset.getNumSamples(); i++){

		GRT::UINT classLabel = testDataset[i].getClassLabel();
		GRT::MatrixDouble quantizedSample;

		for(GRT::UINT j=0; j<testDataset[i].getLength(); j++){
			quantizer->quantize( testDataset[i].getData().getRowVector(j) );

			quantizedSample.push_back( quantizer->getFeatureVector() );
		}

		if( !quantizedTestData.addSample(classLabel, quantizedSample) ){
			throw TestModelException("HiddenMarkovModels:  ERROR: Failed to quantize training data!");
		}
	}

	this->testDataset = quantizedTestData;
}
예제 #6
0
파일: iostream.cpp 프로젝트: damellis/ESP
void ASCIISerialStream::parseSerial(vector<unsigned char> &buffer) {
    auto newline = find(buffer.begin(), buffer.end(), '\n');
    if (newline != buffer.end()) {
        string s(buffer.begin(), newline);

        buffer.erase(buffer.begin(), ++newline);

        if (data_ready_callback_ != nullptr) {
            istringstream iss(s);
            vector<double> data;
            double d;

            while (iss >> d) data.push_back(d);

            if (data.size() > 0) {
                data = normalize(data);

                GRT::MatrixDouble matrix;
                matrix.push_back(data);

                data_ready_callback_(matrix);
            }
        }