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