X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fvideo_filter%2Fopencv_example.cpp;h=bfa0dc861a2cd0d8e91398f843eb4fb2f747b5e2;hb=3feac244a9ea77e39d80a7bb2deb3d41253842fe;hp=457fad6d037b5ba8c7022004fab60609ce84f257;hpb=05492281965ed211badf7e1f4c2220be720d3356;p=vlc diff --git a/modules/video_filter/opencv_example.cpp b/modules/video_filter/opencv_example.cpp index 457fad6d03..bfa0dc861a 100644 --- a/modules/video_filter/opencv_example.cpp +++ b/modules/video_filter/opencv_example.cpp @@ -3,9 +3,11 @@ * (performs face identification). Mostly taken from the facedetect.c * OpenCV sample. ***************************************************************************** - * Copyright (C) 2006 the VideoLAN team + * Copyright (C) 2006-2012 the VideoLAN team + * Copyright (C) 2012 Edward Wang * * Authors: Dugal Harris + * Edward Wang * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,10 +27,6 @@ /***************************************************************************** * Preamble *****************************************************************************/ -#include -#include -#include - #ifdef HAVE_CONFIG_H # include "config.h" @@ -37,10 +35,14 @@ #include #include #include -#include "filter_common.h" +#include #include #include "filter_event_info.h" +#include +#include +#include + /***************************************************************************** * filter_sys_t : filter descriptor *****************************************************************************/ @@ -66,14 +68,14 @@ static picture_t *Filter( filter_t *, picture_t * ); vlc_module_begin () set_description( N_("OpenCV face detection example filter") ) set_shortname( N_( "OpenCV example" )) - set_capability( "opencv example", 1 ) + set_capability( "opencv internal filter", 1 ) add_shortcut( "opencv_example" ) set_category( CAT_VIDEO ) - set_subcategory( SUBCAT_VIDEO_VFILTER2 ) + set_subcategory( SUBCAT_VIDEO_VFILTER ) set_callbacks( OpenFilter, CloseFilter ) - add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml", NULL, + add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml", N_("Haar cascade filename"), N_("Name of XML file containing Haar cascade description"), false); vlc_module_end () @@ -103,16 +105,16 @@ static int OpenFilter( vlc_object_t *p_this ) //create the VIDEO_FILTER_EVENT_VARIABLE vlc_value_t val; if (var_Create( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_ADDRESS | VLC_VAR_DOINHERIT ) != VLC_SUCCESS) - msg_Err( p_filter, "Could not create %s\n", VIDEO_FILTER_EVENT_VARIABLE); + msg_Err( p_filter, "Could not create %s", VIDEO_FILTER_EVENT_VARIABLE); val.p_address = &(p_sys->event_info); if (var_Set( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, val )!=VLC_SUCCESS) - msg_Err( p_filter, "Could not set %s\n", VIDEO_FILTER_EVENT_VARIABLE); + msg_Err( p_filter, "Could not set %s", VIDEO_FILTER_EVENT_VARIABLE); //OpenCV init specific to this example - char* filename = config_GetPsz( p_filter, "opencv-haarcascade-file" ); - p_filter->p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 ); - p_filter->p_sys->p_storage = cvCreateMemStorage(0); + char* filename = var_InheritString( p_filter, "opencv-haarcascade-file" ); + p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 ); + p_sys->p_storage = cvCreateMemStorage(0); free( filename ); return VLC_SUCCESS; @@ -126,15 +128,13 @@ static void CloseFilter( vlc_object_t *p_this ) filter_t *p_filter = (filter_t*)p_this; filter_sys_t *p_sys = p_filter->p_sys; - if( p_filter->p_sys->p_cascade ) - cvReleaseHaarClassifierCascade( &p_filter->p_sys->p_cascade ); + if( p_sys->p_cascade ) + cvReleaseHaarClassifierCascade( &p_sys->p_cascade ); - if( p_filter->p_sys->p_storage ) - cvReleaseMemStorage( &p_filter->p_sys->p_storage ); - - if (NULL != p_filter->p_sys->event_info.p_region) - free(p_filter->p_sys->event_info.p_region); + if( p_sys->p_storage ) + cvReleaseMemStorage( &p_sys->p_storage ); + free( p_sys->event_info.p_region ); free( p_sys ); var_Destroy( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE); @@ -142,30 +142,21 @@ static void CloseFilter( vlc_object_t *p_this ) /**************************************************************************** * Filter: Check for faces and raises an event when one is found. - **************************************************************************** - * p_pic: A picture_t with its p_data_orig member set to an array of - * IplImages (one image for each picture_t plane). ****************************************************************************/ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic ) { IplImage** p_img = NULL; - int i_planes = 0; CvPoint pt1, pt2; int i, scale = 1; + filter_sys_t *p_sys = p_filter->p_sys; if ((!p_pic) ) { msg_Err( p_filter, "no image array" ); return NULL; } - if (!(p_pic->p_data_orig)) - { - msg_Err( p_filter, "no image array" ); - return NULL; - } //(hack) cast the picture_t to array of IplImage* - p_img = (IplImage**) p_pic->p_data_orig; - i_planes = p_pic->i_planes; + p_img = (IplImage**) p_pic; //check the image array for validity if ((!p_img[0])) //1st plane is 'I' i.e. greyscale @@ -173,62 +164,45 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic ) msg_Err( p_filter, "no image" ); return NULL; } - if ((p_pic->format.i_chroma != VLC_FOURCC('I','4','2','0'))) - { - msg_Err( p_filter, "wrong chroma - use I420" ); - return NULL; - } - if (i_planes<1) - { - msg_Err( p_filter, "no image planes" ); - return NULL; - } //perform face detection - cvClearMemStorage(p_filter->p_sys->p_storage); - CvSeq* faces = NULL; - if( p_filter->p_sys->p_cascade ) + cvClearMemStorage(p_sys->p_storage); + if( p_sys->p_cascade ) { //we should make some of these params config variables - faces = cvHaarDetectObjects( p_img[0], p_filter->p_sys->p_cascade, - p_filter->p_sys->p_storage, 1.15, 5, CV_HAAR_DO_CANNY_PRUNING, + CvSeq *faces = cvHaarDetectObjects( p_img[0], p_sys->p_cascade, + p_sys->p_storage, 1.15, 5, + CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20) ); //create the video_filter_region_info_t struct - CvRect* r; if (faces && (faces->total > 0)) { - //msg_Dbg( p_filter, "Found %d face(s)\n", faces->total ); - if (NULL != p_filter->p_sys->event_info.p_region) - { - free(p_filter->p_sys->event_info.p_region); - p_filter->p_sys->event_info.p_region = NULL; - } - if( NULL == ( p_filter->p_sys->event_info.p_region = - (video_filter_region_info_t *)malloc(faces->total*sizeof(video_filter_region_info_t)))) - { + //msg_Dbg( p_filter, "Found %d face(s)", faces->total ); + free( p_sys->event_info.p_region ); + p_sys->event_info.p_region = (video_filter_region_info_t*) + calloc( faces->total, sizeof(video_filter_region_info_t)); + if( !p_sys->event_info.p_region ) return NULL; - } - memset(p_filter->p_sys->event_info.p_region, 0, faces->total*sizeof(video_filter_region_info_t)); - p_filter->p_sys->event_info.i_region_size = faces->total; + p_sys->event_info.i_region_size = faces->total; } //populate the video_filter_region_info_t struct for( i = 0; i < (faces ? faces->total : 0); i++ ) { - r = (CvRect*)cvGetSeqElem( faces, i ); + CvRect *r = (CvRect*)cvGetSeqElem( faces, i ); pt1.x = r->x*scale; pt2.x = (r->x+r->width)*scale; pt1.y = r->y*scale; pt2.y = (r->y+r->height)*scale; cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 ); - *(CvRect*)(&(p_filter->p_sys->event_info.p_region[i])) = *r; - p_filter->p_sys->event_info.p_region[i].i_id = p_filter->p_sys->i_id++; - p_filter->p_sys->event_info.p_region[i].p_description = "Face Detected"; + *(CvRect*)(&(p_sys->event_info.p_region[i])) = *r; + p_sys->event_info.p_region[i].i_id = p_sys->i_id++; + p_sys->event_info.p_region[i].p_description = "Face Detected"; } if (faces && (faces->total > 0)) //raise the video filter event - var_Change( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL ); + var_TriggerCallback( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE ); } else msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );