int
main(int argc, char** argv)
{
	CvSeq*	comp = 0;
	CvRect	window, eye;
	int		key, nc, found; 
	int		text_delay, stage = STAGE_INIT;

	init();

	while (key != 'q')
	{
		frame = cvQueryFrame(capture);
		if (!frame)
			exit_nicely("cannot query frame!");
		frame->origin = 0;

		if (stage == STAGE_INIT)
			window = cvRect(0, 0, frame->width, frame->height);

		cvCvtColor(frame, gray, CV_BGR2GRAY);

		nc = get_connected_components(gray, prev, window, &comp);

		if (stage == STAGE_INIT && is_eye_pair(comp, nc, &eye))
		{
			delay_frames(5);

			cvSetImageROI(gray, eye);
			cvCopy(gray, tpl, NULL);
			cvResetImageROI(gray);

			stage = STAGE_TRACKING;
			text_delay = 10;
		}

		if (stage == STAGE_TRACKING)
		{
			found = locate_eye(gray, tpl, &window, &eye);

			if (!found || key == 'r')
				stage = STAGE_INIT;

			if (is_blink(comp, nc, window, eye)){
				text_delay = 10;
				system("/bin/bash ./blinked.sh");
			}
			DRAW_RECTS(frame, diff, window, eye);
			DRAW_TEXT(frame, "blink!", text_delay, 1);
		}

		cvShowImage(wnd_name, frame);
		cvShowImage(wnd_debug, diff);
		prev = (IplImage*)cvClone(gray);
		key  = cvWaitKey(15);
	}

	exit_nicely(NULL);
}
int
dllmain(int* EyeX,int* EyeY)
{
	//while (key != 'q')
	//{
		frame = cvQueryFrame(capture);
		if (!frame)
			exit_nicely("cannot query frame!");
		frame->origin = 0;
		if (stage == STAGE_INIT)
			window = cvRect(0, 0, frame->width, frame->height);
		cvCvtColor(frame, gray, CV_BGR2GRAY);
		nc = get_connected_components(gray, prev, window, &comp);
		if (stage == STAGE_INIT && is_eye_pair(comp, nc, &eye))
		{
			delay_frames(5);
			cvSetImageROI(gray, eye);
			cvCopy(gray, tpl, NULL);
			cvResetImageROI(gray);
			stage = STAGE_TRACKING;
			text_delay = 10;
		}
		if (stage == STAGE_TRACKING)
		{
			found = locate_eye(gray, tpl, &window, &eye);
			if (!found || key == 'r')
				stage = STAGE_INIT;
			if (is_blink(comp, nc, window, eye))
				text_delay = 10;
			DRAW_RECTS(frame, diff, window, eye);
			DRAW_TEXT(frame, "blink!", text_delay, 1);
		}
		cvShowImage(wnd_name, frame);
		cvShowImage(wnd_debug, diff);
		prev = cvCloneImage(gray);
		key = cvWaitKey(15);
		*EyeX = eye.x;
		*EyeY = eye.y;
		//printf("X = %d\nY = %d\nWidth = %d\nHeight = %d\n\n", eye.x, eye.y, eye.width, eye.height);
	//}
	//exit_nicely(NULL);
		return 0;
}
int main( int argc, char** argv )
{
	CvSeq* comp = 0;
	CvRect window, eye;
	int key, nc, found;
	int stage = STAGE_INIT;
		
	key=0;
	startpos_x = 0;
	startpos_y = 0;
	search_window_x=0,search_window_y=0;		
		
	/* Initialize Capture from webcam
	*	Here '0' in cvCaptureCAM indicates the Index of the camera to be used.		
	*/
	capture = cvCaptureFromCAM(0);
	if (!capture)
		exit_nicely("Webcam Not found!");
 
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,  300);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 250);
 
	// Grabs and returns a frame from the camera.
	frame = cvQueryFrame(capture);

	if (!frame)
		exit_nicely("Cannot query frame!");
 
	// Create a window named 'Video' with window size Normal.
	cvNamedWindow("video",CV_WINDOW_NORMAL);

	/*  * Creates Windows Handler for keeping the frame window
		* always on top of other windows.
	*/
	HWND win_handle = FindWindow(0, "video");
	if (!win_handle)
	{
		printf("Failed FindWindow\n");
	}
	
	SetWindowPos(win_handle, HWND_TOPMOST, 0, 0, 0, 0, 1);
	ShowWindow(win_handle, SW_SHOW);

	
	// Create a callback to mousehandler when mouse is clicked on frame.
	cvSetMouseCallback( "video", mouseHandler,NULL );

	// cvCreateImage is used to create & allocate image data
	nose_template = cvCreateImage( cvSize( NOSE_TPL_WIDTH, NOSE_TPL_HEIGHT ),frame->depth, frame->nChannels );
	nose_template_match = cvCreateImage( cvSize( BOUNDARY_WINDOW_WIDTH  - NOSE_TPL_WIDTH  + 1, BOUNDARY_WINDOW_HEIGHT - NOSE_TPL_HEIGHT + 1 ),IPL_DEPTH_32F, 1 );
	
	// Initialize Memory for storing the frames
	storage = cvCreateMemStorage(0);	
	if (!storage)
		exit_nicely("Cannot allocate memory storage!");
 
	/* Allocates and Fills the structure IplConvKernel ,
	which can be used as a structuring element in the morphological operations */
	kernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_CROSS, NULL);
	gray   = cvCreateImage(cvGetSize(frame), 8, 1);
	prev   = cvCreateImage(cvGetSize(frame), 8, 1);
	diff   = cvCreateImage(cvGetSize(frame), 8, 1);
	eye_template    = cvCreateImage(cvSize(EYE_TPL_WIDTH, EYE_TPL_HEIGHT), 8, 1);
 
	// Show if any error occurs during allocation
	if (!kernel || !gray || !prev || !diff || !eye_template)
		exit_nicely("System error.");
 
	gray->origin  = frame->origin;
	prev->origin  = frame->origin;
	diff->origin  = frame->origin;

	// Loop until ESC(27) key is pressed
	while( key != 27 ) 
	{
		// Get a frame from CAM
		frame = cvQueryFrame( capture );

		/* Always check if frame exists */
		if( !frame ) break;

		// Flip the frame vertically
		cvFlip( frame, frame, 1 );

		frame->origin = 0;

		// Eye blink detection & tracking
		if (stage == STAGE_INIT)
			window = cvRect(0, 0, frame->width, frame->height);

		// Convert original image to thresholded(grayscale) image for efficient detection*/
		cvCvtColor(frame, gray, CV_BGR2GRAY);

		// Find connected components in the image
		nc = get_connected_components(gray, prev, window, &comp);
			
		// Check if eyes are detected and start tracking by setting Region of Interest(ROI)
		if (stage == STAGE_INIT && is_eye_pair(comp, nc, &eye))
		{
			cvSetImageROI(gray, eye);
			cvCopy(gray, eye_template, NULL);
			cvResetImageROI(gray);
			// Start tracking eyes for blink
			stage = STAGE_TRACKING;				
		}
			
		// Here the tracking will start & will check for eye blink
		if (stage == STAGE_TRACKING)
		{
			// Try to locate the eye
			found = locate_eye(gray, eye_template, &window, &eye);
				
			// If eye is not found here or 'r' is pressed, restart the eye detection module
			if (!found || key == 'r')
				stage = STAGE_INIT;
				
			DRAW_RECTS(frame, diff, window, eye);
			// Check if there was an eye blink
			if (is_blink(comp, nc, window, eye))
			{
				//DRAW_RECTS(frame, diff, window, eye);
				printf("Eye Blinked!");

				// Perform mouse left button click on blink
				mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0,0,0,0);
			}				
		}
		
		prev = (IplImage*)cvClone(gray);

		/* Perform nose tracking if template is available 
			Here tracking will start & continues till
			selected templated is within specified 
			threshold limit */
		if( is_tracking ) trackObject();

		// Display the frame in window
		cvShowImage( "video", frame );

		// Check for a key press
		key = cvWaitKey( 10 );
	}

	// Exit without any error
	exit_nicely(NULL);
}
Example #4
0
int main(int argc, char const *argv[])
{
	char* s;
	std::srand(std::time(0)); //use current time as seed for random generator
	int r = rand() % 1000;
	for(int i = 0; i < r; i++)
	{
		rand();
	}
	if(argc < 3)
	{
		return 1;
	}
	
	int forestSize = strtol(argv[1], &s, 10);
	int iterations = strtol(argv[2], &s, 10);

	double SIDE = std::sqrt(forestSize);
	SIDE = fRand(std::sqrt(SIDE),std::sqrt(2)*SIDE);
	double R = 1;

	double begin, end;

	std::vector<int> empty;

	std::vector<Tree*> Forest;
	std::vector< std::vector<int> > neighbors(forestSize,empty);
	std::vector<double> metrics(forestSize,0.0);

	int num_threads;

	std::vector<int> systems_processed; // DEBUG
    std::vector<int> symbols_translated; // DEBUG

///// PARALLEL BLOCK
	begin = omp_get_wtime();
	#pragma omp parallel shared(Forest,neighbors,metrics,num_threads)
	{
		#pragma omp master
		{
			// INIT VARIABLES
			std::vector<Point> positions;

			num_threads = omp_get_num_threads();
			std::cout << "Running " << forestSize << " trees for " << iterations << " iterations on " << num_threads << " processors" << std::endl;
			
			for(int i = 0; i < forestSize; i++)
			{
				double x = fRand(0,SIDE);
				double y = fRand(0,SIDE);
				Point p = {x,y};
				Tree *T = new MonopodialTree();
				Forest.push_back(T);
				positions.push_back(p);
				for(int j = 0 ; j < i ; j++)
				{
					Point q = positions[j];
					if(pointDistance(p,q) < R)
					{
						neighbors[j].push_back(i);
						neighbors[i].push_back(j);
					}
				}
			}

			systems_processed = std::vector<int>(num_threads,0); //DEBUG
			symbols_translated = std::vector<int>(num_threads,0); //DEBUG
		}

		#pragma omp barrier

		int thread_num = omp_get_thread_num();
		// ITERATE
		for(int j = 0 ; j < iterations ; j++)
		{
			#pragma omp for schedule(dynamic)
			for(int i = 0; i < Forest.size() ; i++)
			{
				Forest[i]->next();
				double metric = Forest[i]->calculateMetric();
				metrics[i] = metric;
				systems_processed[thread_num]++; //DEBUG
				symbols_translated[thread_num] += Forest[i]->getState().size(); //DEBUG
			}

			#pragma omp for schedule(dynamic)
			for(int i = 0; i < Forest.size() ; i++)
			{
				Forest[i]->updateMetric(metrics,neighbors[i]);
			}
		}
	}
///// PARALLEL BLOCK
	end = omp_get_wtime();

	std::vector< std::vector<int> > connected_components = get_connected_components(neighbors);
	// print_forest(Forest, neighbors, metrics); // VERBOSE
	// print_connected_components( connected_components); // VERBOSE


	char buffer[80];

	FILE *f = fopen("Results_naive.txt", "a");
	if(f != NULL)
	{
	    fprintf(f, "%s\n", gettime(buffer));
	    fprintf(f,"%d threads\n",num_threads);
	    fprintf(f,"%d trees\n",forestSize);
	    fprintf(f,"%d iterations\n",iterations);
	    fprintf(f,"%lf %lf\n",SIDE,R);
	    for(int i = 0; i < connected_components.size(); i++)
	    {
	    	fprintf(f, "%d ", connected_components[i].size());
	    }
	    fprintf(f, "\n");
	    fprintf(f,"Proc   Systems   Symbols\n");//DEBUG
	    for(int i = 0; i < num_threads; i++)//DEBUG
	    {//DEBUG
	        fprintf(f,"  %02d  %03d  %03d\n",i,systems_processed[i],symbols_translated[i]);//DEBUG
	    }//DEBUG
	    fprintf(f,"Time : %f seconds\n", end-begin);
	    fprintf(f,"\n=====================\n");
	}

	for(int i = 0; i < Forest.size() ; i++)
	{
		delete Forest[i];
	}


	return 0;
}
Example #5
0
int main(int argc, char const *argv[])
{
	char* s;
	std::srand(std::time(0)); //use current time as seed for random generator
	int r = rand() % 1000;
	for(int i = 0; i < r; i++)
	{
		rand();
	}
	if(argc < 3)
	{
		int forestSize = strtol(argv[1], &s, 10);
		for(int i = 0 ; i < forestSize ; i++)
		{
			printf("%lf\n",fRand(1,std::sqrt(10)));
		}
		return 1;
	}
	
	
	int forestSize = strtol(argv[1], &s, 10);
	int iterations = strtol(argv[2], &s, 10);

	double SIDE = std::sqrt(forestSize);
	SIDE = fRand(std::sqrt(SIDE),std::sqrt(2)*SIDE);
	double R = 1;

	double begin, end;

	std::vector<int> empty;

	std::vector<Tree*> Forest;
	std::vector< std::vector<int> > neighbors(forestSize,empty);
	std::vector< std::vector<double> > metrics(iterations,std::vector<double>(forestSize,0.0));

	//Parallel variables
	int num_threads;
	std::vector<int> order;

	begin = omp_get_wtime();
	#pragma omp parallel shared(Forest,neighbors,metrics,forestSize,iterations,order)
	{
		
		#pragma omp master
		{
			// INIT VARIABLES
			num_threads = omp_get_num_threads();
			std::vector<Point> positions;
			std::cout << "Running " << forestSize << " trees for " << iterations << " iterations on " << num_threads << " processors" << std::endl;
			printf("SIDE = %lf, R = %lf\n",SIDE,R);
			
			for(int i = 0; i < forestSize; i++)
			{
				// double x = std::fabs((SIDE-1)*std::sin(i));
				// double y = std::fabs(SIDE*std::cos(i*i));
				double x = fRand(0,SIDE);
				double y = fRand(0,SIDE);
				Point p = {x,y};
				Tree *T = new MonopodialTree();
				Forest.push_back(T);
				positions.push_back(p);
				for(int j = 0 ; j < i ; j++)
				{
					Point q = positions[j];
					if(pointDistance(p,q) < R)
					{
						neighbors[j].push_back(i);
						neighbors[i].push_back(j);
					}
				}
			}

			order = get_order(neighbors);
			for(int i = 0; i < order.size(); i++)
				std::cout << order[i] << " ";
			std::cout << std::endl;
		}

		#pragma omp barrier

		int thread_num = omp_get_thread_num();
		// ITERATE
		

		int N = forestSize;
		int T = iterations;
		int P = omp_get_num_threads();
		int x = thread_num;
		int y = 0;

		while( x+N*y < N*T)
		{
			int i = order[x];
			// printf("%d (%d, %d)\n",thread_num,y,i );

			while(Forest[i]->iteration < y);
			bool ready = false;
			while(!ready)
			{
				ready = true;
				for(int k = 0; k < neighbors[i].size() ; k++)
				{
					if( Forest[ neighbors[i][k] ]->iteration < y)
					{
						ready = false;
						break;
					}
				}
			}

			if(y > 0)
			{
				Forest[i]->updateMetric(metrics[y],neighbors[i]);
			}
			Forest[i]->next();
			double metric = Forest[i]->calculateMetric();
			#pragma omp critical(metrics)
			{
				metrics[y][i] = metric;
			}

			x+=P;
			if(x >= N)
			{
				x -= N;
				y++;
			}
			
			
		}
		

	}

	end = omp_get_wtime();
	
	print_forest(Forest, neighbors, metrics[iterations-1]);

	std::vector< std::vector<int> > connected_components = get_connected_components(neighbors);
	print_connected_components( connected_components);

	char buffer[80];

	FILE *f = fopen("Results_lookahead.txt", "a");
	if(f != NULL)
	{
	    fprintf(f, "%s\n", gettime(buffer));
	    fprintf(f,"%d threads\n",num_threads);
	    fprintf(f,"%d trees\n",forestSize);
	    fprintf(f,"%d iterations\n",iterations);
	    for(int i = 0; i < connected_components.size(); i++)
	    {
	    	fprintf(f, "%d ", connected_components[i].size());
	    }
	    fprintf(f, "\n");
	    fprintf(f,"Time : %f seconds\n", end-begin);
	    fprintf(f,"\n=====================\n");
	}

	for(int i = 0; i < Forest.size() ; i++)
	{
		delete Forest[i];
	}

	return 0;
}