From 9618584c806c8f2ad972d7634f9587dc2ce5091b Mon Sep 17 00:00:00 2001 From: daniel watson Date: Sun, 10 Jul 2011 17:58:50 -0700 Subject: [PATCH] more fault tolerant of single frames and/or capture dying --- oble.cpp | 70 ++++++++++++++++++++++++++++++++++++-------------------- oble.h | 2 +- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/oble.cpp b/oble.cpp index 13a1021..ab5ca39 100644 --- a/oble.cpp +++ b/oble.cpp @@ -186,64 +186,84 @@ int parse_opts(int argc, char **argv) return 0; } -void get_frames(CvCapture* capture, CvHaarClassifierCascade* cascade) +int get_frames(CvCapture* capture, CvHaarClassifierCascade* cascade) { + int got_null_frame = 0; IplImage* frame = NULL; get_one_frame(capture, frame); IplImage* mirrored = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels); // Show the image captured from the camera in the window and repeat - while(1) { - if(get_one_frame(capture, frame)) break; - - //flip the image so displayed right/left corresponds to physical right/left - cvFlip(frame, mirrored, MIRROR); + while(!(got_null_frame = get_one_frame(capture, frame))) + { + //so displayed right/left corresponds to physical right/left: + cvFlip(frame, mirrored, MIRROR); - detect_and_draw_objects(mirrored, cascade, 1); - cvShowImage("mywindow", mirrored); - // Do not release the frame! - //screensave(t_current); + detect_and_draw_objects(mirrored, cascade, 1); + cvShowImage("mywindow", mirrored); + // Do not release the frame! + //screensave(t_current); - //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), - //remove higher bits using AND operator - if((cvWaitKey(100) & 255) == 27) break; - } + //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), + //remove higher bits using AND operator + if((cvWaitKey(100) & 255) == 27) break; + } cvReleaseImage(&mirrored); + return got_null_frame; } int do_capture() { + int got_null_capture = 0; CvHaarClassifierCascade* cascade = load_object_detector(cascade_filename.c_str()); CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); if(!capture) { cerr << "ERROR: capture is NULL " << endl; - getchar(); - return -1; + return 1; } // Create a window in which the captured images will be presented cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); t_current = time(NULL); - get_frames(capture, cascade); + while(get_frames(capture, cascade)) + { + cerr << "resetting capture." << endl; + cvReleaseCapture( &capture ); + capture = cvCaptureFromCAM( CV_CAP_ANY ); + if(!capture) + { + got_null_capture = 1; + break; + } + } // Release the capture device housekeeping - cvReleaseCapture(&capture); + if(!got_null_capture) + { + cvReleaseCapture(&capture); + } cvDestroyWindow("mywindow"); - return 0; + return got_null_capture; } int get_one_frame(CvCapture* capture, IplImage* &frame) { + int got_null_frame = 0; frame = cvQueryFrame(capture); - if(!frame) { - cerr << "ERROR: frame is null..." << endl; - getchar(); - return 1; - } - return 0; + if(!frame) + { + cerr << "ERROR: frame is null..., trying again" << endl; + frame = cvQueryFrame(capture); + if(!frame) + { + cerr << "ERROR: frame is still null... maybe reset capture?" << endl; + got_null_frame = 1; + } + } + return got_null_frame; } // A Simple Camera Capture Framework. diff --git a/oble.h b/oble.h index cb4cd24..7fd6b6d 100644 --- a/oble.h +++ b/oble.h @@ -54,6 +54,6 @@ int do_capture(); int get_one_frame(CvCapture* capture, IplImage* &frame); -void get_frames(CvCapture* capture, CvHaarClassifierCascade* cascade); +int get_frames(CvCapture* capture, CvHaarClassifierCascade* cascade); void update_idle(int faces_total); -- 2.30.2