⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 facedetect.cpp

📁 人脸检测领域经典的Haar人脸检测。使用OpenCV.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                       size.width, size.height, 3, step );
    }

    if( started )
    {
        double cur_frame_stamp = get_time_accurate();
        // update fps
        if( fps < 0 )
            fps = 1000/(cur_frame_stamp - prev_frame_stamp);
        else
            fps = (1-fps_alpha)*fps + fps_alpha*1000/(cur_frame_stamp - prev_frame_stamp);
        prev_frame_stamp = cur_frame_stamp;
        sprintf( fps_buffer, "FPS: %5.1f", fps );
        fps_box->label( fps_buffer );
        fps_box->redraw();
        if( total_frames > 0 )
        {
            if( --total_frames == 0 )
                if( !is_loopy )
                    cb_Exit(0,0);
                else
                {
                    total_frames = total_frames0;
                    cvSetCaptureProperty( capture, CV_CAP_PROP_POS_FRAMES, start_pos );
                }
        }
        Fl::add_timeout( timeout, get_next_frame, 0 );
    }
}

static void cb_PauseResume( Fl_Widget*, void* )
{
    video_button->deactivate();
    cam_button->deactivate();
    stop_button->activate();

    if( !started )
    {
        started = 1;
        play_button->label("@||");
        play_button->activate();
        play_button->redraw();
        stop_button->activate();
        stop_button->redraw();
        record_button->activate();

        video_window->redraw();
        prev_frame_stamp = get_time_accurate();
        Fl::add_timeout( timeout, get_next_frame, 0 );
    }
    else
    {
        started = 0;
        play_button->label("@>");
        play_button->redraw();
    }
}


static void cb_Stop( Fl_Widget*, void* )
{
    if( started )
        cb_PauseResume(0,0);
    if( is_recorded )
        cb_StartStopRecord(0,0);
    cvSetCaptureProperty( capture, CV_CAP_PROP_POS_AVI_RATIO, 0 );
    video_button->activate();
    cam_button->activate();
    stop_button->deactivate();
    record_button->deactivate();
}

static void cb_StartAVI( const char* newfile, int start_pos, int was_started )
{
    if( newfile != 0 )
    {
        end_capture();
        capture = cvCaptureFromAVI( newfile );
        if( capture )
        {
            was_started = 1;
            if( start_pos != 0 )
                cvSetCaptureProperty( capture, CV_CAP_PROP_POS_FRAMES, start_pos );
        }
        is_avi = 1;
    }

    if( was_started )
        cb_PauseResume(0,0);
}


static void cb_Open( Fl_Widget*, void* )
{
    char filename[1000];
    filename[0] = '\0';
    int was_started = 0;
    
    if( started )
    {
        was_started = 1;
        cb_PauseResume(0,0);
    }

    char *newfile = fl_file_chooser("Open File?", "*", filename);
    cb_StartAVI( newfile, 0, was_started );
}


static void cb_StartCam( Fl_Widget*, void* )
{
    if( started )
        cb_PauseResume(0,0);
    end_capture();
#ifdef WIN32    
    Sleep(10);
#endif    
    is_avi = 0;
    capture = cvCaptureFromCAM( -1 );
    cb_PauseResume(0,0);
}


static void cb_StartStopRecord( Fl_Widget*, void* )
{
    if( !is_recorded )
    {
        if( video_image )
        {
            writer = cvCreateAVIWriter( "c:\\test.avi", -1, 15, cvGetSize( video_image ));
            if( writer )
            {
                record_button->box( FL_DOWN_BOX );
                is_recorded = 1;
            }
        }
    }
    else
    {
        record_button->box( FL_UP_BOX );
        cvReleaseAVIWriter( &writer );
        is_recorded = 0;
    }
}

static void cb_AutoRun( void* _aviname )
{
    char* aviname = (char*)_aviname;
    char* ext_pos = strrchr( aviname, '.' );
    if( ext_pos )
    {
        char* colon_pos = strchr( ext_pos, ':' );
        if( colon_pos )
        {
            *colon_pos = '\0';
            start_pos = strtoul( colon_pos+1, &colon_pos, 10 );
            if( colon_pos && *colon_pos == ':' )
                total_frames0 = total_frames = strtoul( colon_pos+1, &colon_pos, 10 );
            if( colon_pos && *colon_pos == ':' )
                is_loopy = 1;
        }
    }

    cb_StartAVI( aviname, start_pos, 0 );
}


static void cb_Exit( Fl_Widget*, void* )
{
    cvReleaseCapture( &capture );
    cvReleaseAVIWriter( &writer );
    exit(0);
}

const int root_w = 400, root_h = 400;

int main( int argc, char **argv )
{
	//allow command line processing
    const char* facebaseopt="--facebase=";
    char* classifierbase = 0;
    char* aviname = 0;
    int auto_run = 0;
	char inputAviName[5000];

	outf = NULL;
	interactiveRun = true;

	if ( argc >= 4 ) {
	
		if ( stricmp(argv[1],"nogui") == 0 ) {
			interactiveRun = false;
			strcpy(inputAviName,argv[2]);
			strcpy(outputFileName,argv[3]);
			if ( argc == 5 ) {
				numScales = atoi(argv[4]);
			}
			argc--;argc--;argc--;argc--;
			argv++;argv++;argv++;argv++;
		}
	}


    if( argc > 1 && argv[argc-1][0] != '-' )
    {
        aviname = argv[argc-1];
        auto_run = 1;
        argc--;
    }
    
    if( argc > 1 && strncmp(argv[argc-1],facebaseopt,strlen(facebaseopt))==0 )
    {
        classifierbase=argv[argc-1] + strlen(facebaseopt);
        argc--;
    }
    
    if( !InitFaceDetect(classifierbase))
    {
        fprintf( stderr, "Could not locate face classifier base at %s\n"
                         "Use --facebase=<classifier base path> option to specify the base\n",
                 classifierbase );
        return -1;
    }
	
    cpu_freq = cvGetTickFrequency(); 
    printf("Tick frequency (*10^-6): %g\n", cpu_freq );
 
	if ( interactiveRun ) {
		Fl_Window* w;
		{
			Fl_Window* o = root_window = new Fl_Window( root_w, root_h );
			w = o;
			{
				Fl_Tabs* o = new Fl_Tabs( 10, 10, root_w - 20, root_h - 100 );
				// camera tab
				{
					Fl_Group* o = new Fl_Group( 10, 30, root_w - 20, root_h - 110, "Face Detection" );
					{
						VideoWindow* o = new VideoWindow( 15, 35, root_w - 30, root_h - 120 );
						video_window = o;
						o->box( FL_BORDER_BOX );
						o->color(0);
					}
					o->end();
				}
				o->end();
				Fl_Group::current()->resizable(o);
			}
			{
				const int bwidth = 30, bheight = 30;
				play_button = new Fl_Button( 10, root_h - 35, bwidth, bheight, "@>" );
				play_button->callback((Fl_Callback*)cb_PauseResume);
				play_button->deactivate();
				stop_button = new Fl_Button( 10 + bwidth, root_h - 35, bwidth, bheight, "@square" );
				stop_button->callback((Fl_Callback*)cb_Stop);
				stop_button->deactivate();
				video_button = new Fl_Button( 10 + bwidth*2, root_h - 35, bwidth, bheight, "..." );
				video_button->callback((Fl_Callback*)cb_Open);
				cam_button = new Fl_Button( 10 + bwidth*3, root_h - 35, bwidth, bheight, "[o]" );
				cam_button->callback((Fl_Callback*)cb_StartCam);
				video_pos = new Fl_Value_Slider( 10 + bwidth*4 + 10, root_h - 35, 200, 20, "Position" );
				video_pos->type( FL_HOR_NICE_SLIDER );
				record_button = new Fl_Button( 10 + bwidth*4 + 230, root_h - 35, bwidth, bheight, "@circle" );
				record_button->labelcolor(FL_RED);
				record_button->callback((Fl_Callback*)cb_StartStopRecord );
				record_button->deactivate();
				fps_box = new Fl_Box( 10, root_h - 75, bwidth*4, bheight, "<No data>" );
				fps_box->box( FL_DOWN_BOX );
			}
			o->end();
		}
		Fl::visual(FL_RGB);
		w->show(argc, argv);
		if( auto_run )
			Fl::add_timeout( 0.1, cb_AutoRun, aviname );
		Fl::run();
		cb_Exit(0,0);
	}
	else {
		//get the avi file and open and run it!
		IplImage* frame;
		outf = fopen(outputFileName,"w");
		capture = cvCaptureFromAVI( inputAviName);
		frame = (IplImage*)1;
		int iteration = 1;

		

		frame = cvQueryFrame( capture );
		while (frame != 0 ) {
			
			video_image = cvCloneImage(frame);
			//now find faces
			if( frame->origin == 1 )
				cvFlip( frame, video_image, 0 );
			else
				cvCopy( frame, video_image, 0 );


			DetectAndDrawFaces(video_image);
			frame = cvQueryFrame( capture );			

			cvReleaseImage(&video_image);
		}
		

		cvReleaseCapture( &capture );
		fclose(outf);

	}
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -