1 /*****************************************************************************
2 * opencv_example.cpp : Example OpenCV internal video filter
3 * (performs face identification). Mostly taken from the facedetect.c
5 *****************************************************************************
6 * Copyright (C) 2006 the VideoLAN team
8 * Authors: Dugal Harris <dugalh@protoclea.co.za>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include <vlc_plugin.h>
39 #include <vlc_decoder.h>
40 #include <vlc_filter.h>
41 #include "filter_common.h"
42 #include <vlc_image.h>
43 #include "filter_event_info.h"
45 /*****************************************************************************
46 * filter_sys_t : filter descriptor
47 *****************************************************************************/
50 CvMemStorage* p_storage;
51 CvHaarClassifierCascade* p_cascade;
52 video_filter_event_info_t event_info;
56 /****************************************************************************
58 ****************************************************************************/
59 static int OpenFilter ( vlc_object_t * );
60 static void CloseFilter( vlc_object_t * );
62 static picture_t *Filter( filter_t *, picture_t * );
64 /*****************************************************************************
66 *****************************************************************************/
68 set_description( _("OpenCV face detection example filter") );
69 set_shortname( _( "OpenCV example" ));
70 set_capability( "opencv example", 1 );
71 add_shortcut( "opencv_example" );
73 set_category( CAT_VIDEO );
74 set_subcategory( SUBCAT_VIDEO_VFILTER2 );
75 set_callbacks( OpenFilter, CloseFilter );
77 add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml", NULL,
78 N_("Haar cascade filename"),
79 N_("Name of XML file containing Haar cascade description"), false);
82 /*****************************************************************************
83 * OpenFilter: probe the filter and return score
84 *****************************************************************************/
85 static int OpenFilter( vlc_object_t *p_this )
87 filter_t *p_filter = (filter_t*)p_this;
90 /* Allocate the memory needed to store the decoder's structure */
91 if( ( p_filter->p_sys = p_sys =
92 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
94 msg_Err( p_filter, "out of memory" );
98 //init the video_filter_event_info_t struct
99 p_sys->event_info.i_region_size = 0;
100 p_sys->event_info.p_region = NULL;
103 p_filter->pf_video_filter = Filter;
105 //create the VIDEO_FILTER_EVENT_VARIABLE
107 if (var_Create( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_ADDRESS | VLC_VAR_DOINHERIT ) != VLC_SUCCESS)
108 msg_Err( p_filter, "Could not create %s\n", VIDEO_FILTER_EVENT_VARIABLE);
110 val.p_address = &(p_sys->event_info);
111 if (var_Set( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, val )!=VLC_SUCCESS)
112 msg_Err( p_filter, "Could not set %s\n", VIDEO_FILTER_EVENT_VARIABLE);
114 //OpenCV init specific to this example
115 char* filename = config_GetPsz( p_filter, "opencv-haarcascade-file" );
116 p_filter->p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 );
117 p_filter->p_sys->p_storage = cvCreateMemStorage(0);
122 /*****************************************************************************
123 * CloseFilter: clean up the filter
124 *****************************************************************************/
125 static void CloseFilter( vlc_object_t *p_this )
127 filter_t *p_filter = (filter_t*)p_this;
128 filter_sys_t *p_sys = p_filter->p_sys;
130 if( p_filter->p_sys->p_cascade )
131 cvReleaseHaarClassifierCascade( &p_filter->p_sys->p_cascade );
133 if( p_filter->p_sys->p_storage )
134 cvReleaseMemStorage( &p_filter->p_sys->p_storage );
136 if (NULL != p_filter->p_sys->event_info.p_region)
137 free(p_filter->p_sys->event_info.p_region);
141 var_Destroy( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE);
144 /****************************************************************************
145 * Filter: Check for faces and raises an event when one is found.
146 ****************************************************************************
147 * p_pic: A picture_t with its p_data_orig member set to an array of
148 * IplImages (one image for each picture_t plane).
149 ****************************************************************************/
150 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
152 IplImage** p_img = NULL;
159 msg_Err( p_filter, "no image array" );
162 if (!(p_pic->p_data_orig))
164 msg_Err( p_filter, "no image array" );
167 //(hack) cast the picture_t to array of IplImage*
168 p_img = (IplImage**) p_pic->p_data_orig;
169 i_planes = p_pic->i_planes;
171 //check the image array for validity
172 if ((!p_img[0])) //1st plane is 'I' i.e. greyscale
174 msg_Err( p_filter, "no image" );
177 if ((p_pic->format.i_chroma != VLC_FOURCC('I','4','2','0')))
179 msg_Err( p_filter, "wrong chroma - use I420" );
184 msg_Err( p_filter, "no image planes" );
188 //perform face detection
189 cvClearMemStorage(p_filter->p_sys->p_storage);
191 if( p_filter->p_sys->p_cascade )
193 //we should make some of these params config variables
194 faces = cvHaarDetectObjects( p_img[0], p_filter->p_sys->p_cascade,
195 p_filter->p_sys->p_storage, 1.15, 5, CV_HAAR_DO_CANNY_PRUNING,
197 //create the video_filter_region_info_t struct
199 if (faces && (faces->total > 0))
201 //msg_Dbg( p_filter, "Found %d face(s)\n", faces->total );
202 if (NULL != p_filter->p_sys->event_info.p_region)
204 free(p_filter->p_sys->event_info.p_region);
205 p_filter->p_sys->event_info.p_region = NULL;
207 if( NULL == ( p_filter->p_sys->event_info.p_region =
208 (video_filter_region_info_t *)malloc(faces->total*sizeof(video_filter_region_info_t))))
210 msg_Err( p_filter, "out of memory" );
213 memset(p_filter->p_sys->event_info.p_region, 0, faces->total*sizeof(video_filter_region_info_t));
214 p_filter->p_sys->event_info.i_region_size = faces->total;
217 //populate the video_filter_region_info_t struct
218 for( i = 0; i < (faces ? faces->total : 0); i++ )
220 r = (CvRect*)cvGetSeqElem( faces, i );
222 pt2.x = (r->x+r->width)*scale;
224 pt2.y = (r->y+r->height)*scale;
225 cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 );
227 *(CvRect*)(&(p_filter->p_sys->event_info.p_region[i])) = *r;
228 p_filter->p_sys->event_info.p_region[i].i_id = p_filter->p_sys->i_id++;
229 p_filter->p_sys->event_info.p_region[i].p_description = "Face Detected";
232 if (faces && (faces->total > 0)) //raise the video filter event
233 var_Change( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL );
236 msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );