Beispiel #1
0
int main()
{	
	srand(time(0));
	SVMClassifier svm;
	
	//Do a simple test of loading a pre-made model, testing 2 vectors, and saving it
	double testVectorPos[] = {.7,1,1,-.32,-.105,-1,1,-.4198,-1,-.2258,1,-1};
	double testVectorNeg[] = {.58,-1,.3333,-.6,1,-1,1,.358779,-1,-.484,-1,1};
	unsigned int testVectorSize = 12;
	svm.loadModel("svm.model");
	
	double probability;
	probability = svm.classifyVector(testVectorPos,testVectorSize);
	cout << "Test VectorPos Probability is " << probability << endl;
	
	probability = svm.classifyVector(testVectorNeg,testVectorSize);
	cout << "Test VectorNeg Probability is " << probability << endl;
	svm.saveModel("svmNew1.model");
	
	//Create a new model that exists of a simple cluster in the second quadrant of a 2d grid being positive, and the rest being negative (x,y)
	testVectorSize = 2;
	unsigned int numSamples = 1000;
	double **trainVec = new double*[numSamples];
	bool *trainPolarities = new bool[numSamples];
	for (unsigned int vecIter = 0; vecIter < numSamples; vecIter++)
	{
		trainVec[vecIter] = new double[2];
		//Pos
		if (vecIter <= (numSamples - 1)/2)
		{
			trainVec[vecIter][0] = -1*rand()/(double)RAND_MAX;
			trainVec[vecIter][1] = rand()/(double)RAND_MAX;
			trainPolarities[vecIter] = true;
		}
		//Neg
		else
		{
			trainVec[vecIter][0] = rand()/(double)RAND_MAX;
			trainVec[vecIter][1] = -1*rand()/(double)RAND_MAX;
			trainPolarities[vecIter] = false;
		}
	}
	
	svm.trainModel(trainVec, trainPolarities, numSamples, testVectorSize);
	svm.saveModel("svmNew2.model");
	
	for (unsigned int testVecIter = 0; testVecIter < numSamples; testVecIter++)
	{
		probability = svm.classifyVector(trainVec[testVecIter],testVectorSize);
		cout << "Test Probability is " << probability << endl;
	}
	
	delete [] trainPolarities;
	for (unsigned int vecIter = 0; vecIter < numSamples; vecIter++)
		delete [] trainVec[vecIter];
	delete [] trainVec;
	
	return 0;
}
Beispiel #2
0
// run from build folder of the project (see path below)
int main (int argc, char** argv) {
  
  Eigen::MatrixXf X;
  Eigen::MatrixXf y;
  eigen_extensions::loadASCII("../test/svmtestx.eig.txt", &X);
  eigen_extensions::loadASCII("../test/svmtesty.eig.txt", &y);
  
  // Classify
  cout << X.topRows(5) << endl;
  cout << y.topRows(5) << endl;
  vector<int> yhat;
  SVMClassifier svm;
  svm.train(X, y);
  svm.test(X, yhat);
  
  Eigen::MatrixXf w;
  float b;
  svm.getw(w, b);
  Eigen::MatrixXf margin= ((X * w).array() + b).matrix(); // ahh eigen...
  
  // Evaluate accuracy and print results
  int match=0;
  for(int i=0;i<yhat.size();i++) {
    if(yhat[i]==(int)y(i)) match++; else printf("WRONG! ");
    printf("y= %d, yhat= %d margin= %f\n", (int)y(i), yhat[i], margin(i));
  }
  printf("MATLAB cross-check: last margin should be around -3.107\n");
  printf("Accuracy= %f. From MATLAB cross-check, expect this to be around 0.945\n", 1.0*match/yhat.size());
  
  // Save the model
  svm.saveModel("temp.svmmodel");
  
  // Test loading model in new instance of SVMClassifier
  SVMClassifier svm2;
  svm2.loadModel("temp.svmmodel");
  yhat.clear();
  svm2.test(X, yhat);
  match=0; 
  for(int i=0;i<yhat.size();i++) if(yhat[i]==(int)y(i)) match++;
  printf("Accuracy= %f from loaded model. Should be 0.945 again.\n", 1.0*match/yhat.size());
  
  
  printf("you may want to rm the temporary file temp.svmmodel\n");
}
Beispiel #3
0
int main(int argc, const char *argv[]) {

   /* if(argc < 2) {
        std::cout << " Input Arguments required " << std::endl;
        return -1;
    }*/

    std::cout<< atoi(argv[1]) << std::endl;
    VideoProcessorClass* videoProcessor  =  VideoProcessorClass::getInstance(atoi(argv[1]));// new VideoProcessorClass(atoi(argv[1]));
    CamshiftProcessing* camshift = new CamshiftProcessing();
    HOGFeatureSelector* hogfeatureSelector = new HOGFeatureSelector();
    SVMClassifier* svmClassifier = new SVMClassifier();
    //HoughTransformClass* houghClass = new HoughTransformClass();


//    MarkerLessAR* markerlessAR = new MarkerLessAR();
//    markerlessAR->startDrawing(1);



//    cv::Mat inputSource = cv::imread("/home/pratheba/workspace/QTProject/HandDetectionWithCamshift/Images/Test/circle.jpg",CV_LOAD_IMAGE_COLOR);

//    houghClass->findCircles(inputSource);

    hogfeatureSelector->ComputeHOGFeatures("/home/pratheba/workspace/QTProject/HandDetectionWithCamshift/Images/Training/Triangle/", 25, IMG_TRIANGLE);
    hogfeatureSelector->ComputeHOGFeatures("/home/pratheba/workspace/QTProject/HandDetectionWithCamshift/Images/Training/Non/", 10, IMG_NONE);
    hogfeatureSelector->ComputeHOGFeatures("/home/pratheba/workspace/QTProject/HandDetectionWithCamshift/Images/Training/Circle/", 38, IMG_CIRCLE);


    /* Train the images to classify as triangle or circle using SVM classifier */
    svmClassifier->TrainData(hogfeatureSelector->hogDescriptorMat, hogfeatureSelector->TrainingClass);




    /* Compute features for SVM classifier using HOG feature selector */

    /* Get the VideoProcessing */
    videoProcessor->SetVideoCaptureInstance();
    //videoProcessor->ChooseProcessorClass(atoi(argv[1]));
    videoProcessor->SetframeToStopProcessing(30);
    videoProcessor->displayInput("Input Frame");
    videoProcessor->displayOutput("Output Frame");


////    // To get the time taken in terms of second



    double initialTickCount = (double)cv::getTickCount();
    videoProcessor->ProcessVideoFrame();

    double finalTickCount = (double)cv::getTickCount();

    double timeextended = (finalTickCount - initialTickCount)/cv::getTickFrequency();

    std::cout << timeextended << std::endl;

    /* Get the color Probability mask */
    camshift->TrackRegionOfInterest();

    std::string testImagefileName = "/home/pratheba/workspace/QTProject/HandDetectionWithCamshift/Images/Test/";

    hogfeatureSelector->ComputeHOGFeaturesForTestImage(testImagefileName);
    svmClassifier->ClassifyImage(hogfeatureSelector->TestClassMat);


    videoProcessor->release();
    delete camshift;
    delete hogfeatureSelector;
    delete svmClassifier;
   // delete houghClass;

    //delete markerlessAR;

    return 0;
}