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;
}