Esempio n. 1
0
void hup_handler(int sig)
{
	double accuracy = calculate_accuracy(total_count, total_time);
	printf("\n--- Results after %d passes ---\n", pass);
	printf("Best: %.3f%% -- Worst: %.3f%% -- Average: %f%%\n",
			best, worst, pass ? total/pass : 100.00);
	printf("Cumulative Accuracy (not per pass): %0.3f\n",
	       pass ? accuracy : 0.0);
	exit(0);
}
int main(int argc, char **argv[])
{
	string name;
	vector<Mat>Images(100), TestImages(50);
	vector<Mat> Descriptor(100), TestDescriptor(50), TestPcafeature(50);
	vector<vector<KeyPoint>>Keypoints(100), TestKeypoint(50);
	Mat histogram = Mat::zeros(100, Cluster, CV_32F);
	Mat Testhistogram = Mat::zeros(50, Cluster, CV_32F);
	Mat Keyword = Mat::zeros(Cluster, 20, CV_32F);
	Mat full_Descriptor, Pcafeature, Pcaduplicate, clusteridx, trainlabels(100, 1, CV_32F);
	vector<vector<DMatch>> matches(50);
	Mat predicted(Testhistogram.rows, 1, CV_32F);

	// Read Training Images.
	read_train(Images, name);

	//Calculate SIFT features for the Training Images.
	calculate_SIFT(Images,Keypoints,Descriptor);
	merge_descriptor(full_Descriptor,Descriptor);

	//Compute PCA for all the features across all Images.
	PCA pca;
	perform_PCA(full_Descriptor, Pcafeature, pca);
	
	//Perform K-Means on all the PCA reduced features.
	Pcafeature.convertTo(Pcaduplicate, CV_32F);
	calculate_Kmeans(Pcaduplicate, clusteridx);

	//Calculate the Keywords in the Feature Space.
	make_dictionary(clusteridx, Pcaduplicate, Keyword);

	//Get the Histogram for each Training Image.
	hist(Descriptor, clusteridx, histogram);

	//Read Test Image
	read_test(TestImages, name);

	//Calculate the SIFT feature for all the test Images.
	calculate_SIFT(TestImages, TestKeypoint, TestDescriptor);

	//Project the SIFT feature of each feature on the lower dimensional PCA plane calculated above. 
	pca_testProject(TestDescriptor, TestPcafeature, pca);

	//Find the Label by searching for keywords closest to current feature.
	get_matches(TestPcafeature,Keyword,matches);

	//Calculate Histogram for each test Image.
	hist_test(TestDescriptor, matches, Testhistogram);
	
	//Perform classification through Knn Classifier. 
	train_labels(trainlabels);
	KNearest knn;
	train_classifier(histogram, trainlabels, knn);
	test_classify(Testhistogram,predicted,knn);

	//Calculate Accuracy for each class.
	calculate_accuracy(predicted);
	
	getchar();
	return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
	int fd;
	int res;
	int c;
	int count = 0;
	int seconds = 0;
	int curarg = 1;
	char buf[8192];
	float ms;
	struct timeval start, now;
	fd = open("/dev/dahdi/pseudo", O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "Unable to open dahdi interface: %s\n", strerror(errno));
		exit(1);
	}
	
	while ((c = getopt(argc, argv, "c:hv")) != -1) {
		switch(c) {
		case 'c':
			seconds = atoi(optarg);
			break;
		case 'h':
			usage(argv[0]);
			exit(0);
			break;
		case '?':
			usage(argv[0]);
			exit(1);
			break;
		case 'v':
			verbose++;
			break;
		}
	}
	while (curarg < argc) {
		if (!strcasecmp(argv[curarg], "-v"))
			verbose++;
		if (!strcasecmp(argv[curarg], "-c") && argc > curarg)
			seconds = atoi(argv[curarg + 1]);
		curarg++;
	}
	printf("Opened pseudo dahdi interface, measuring accuracy...\n");
	signal(SIGHUP, hup_handler);
	signal(SIGINT, hup_handler);
	signal(SIGALRM, hup_handler);
	/* Flush input buffer */
	for (count = 0; count < 4; count++)
		res = read(fd, buf, sizeof(buf));
	count = 0;
	ms = 0; /* Makes the compiler happy */
	if (seconds > 0)
		alarm(seconds + 1); /* This will give 'seconds' cycles */
	for (;;) {
		if (count == 0)
			ms = 0;
		gettimeofday(&start, NULL);
		res = read(fd, buf, sizeof(buf));
		if (res < 0) {
			fprintf(stderr, "Failed to read from pseudo interface: %s\n", strerror(errno));
			exit(1);
		}
		count += res;
		gettimeofday(&now, NULL);
		ms += (now.tv_sec - start.tv_sec) * 8000;
		ms += (now.tv_usec - start.tv_usec) / 125.0;
		if (count >= SIZE) {
			const double percent = calculate_accuracy(count, ms);
			if (verbose) {
				printf("\n%d samples in %0.3f system clock sample intervals (%.3f%%)", 
						count, ms, percent);
			} else if (pass > 0 && (pass % 8) == 0) {
				printf("\n");
			}
			if (percent > best)
				best = percent;
			if (percent < worst)
				worst = percent;
			if (!verbose)
				printf("%.3f%% ", percent);
			total += percent;
			fflush(stdout);
			total_count += count;
			total_time += ms;
			count = 0;
			pass++;
		}
	}
}