]> git.sesse.net Git - vlc/blob - modules/video_filter/opencv_example.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / video_filter / opencv_example.c
1 /*****************************************************************************
2  * opencv_example.cpp : Example OpenCV internal video filter
3  * (performs face identification).  Mostly taken from the facedetect.c
4  * OpenCV sample.
5  *****************************************************************************
6  * Copyright (C) 2006 the VideoLAN team
7  *
8  * Authors: Dugal Harris <dugalh@protoclea.co.za>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
36 #include <vlc_vout.h>
37 #include <vlc_image.h>
38 #include "filter_event_info.h"
39
40 #include <cxcore.h>
41 #include <cv.h>
42
43 /*****************************************************************************
44  * filter_sys_t : filter descriptor
45  *****************************************************************************/
46 struct filter_sys_t
47 {
48     CvMemStorage* p_storage;
49     CvHaarClassifierCascade* p_cascade;
50     video_filter_event_info_t event_info;
51     int i_id;
52 };
53
54 /****************************************************************************
55  * Local prototypes
56  ****************************************************************************/
57 static int  OpenFilter ( vlc_object_t * );
58 static void CloseFilter( vlc_object_t * );
59
60 static picture_t *Filter( filter_t *, picture_t * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin ()
66     set_description( N_("OpenCV face detection example filter") )
67     set_shortname( N_( "OpenCV example" ))
68     set_capability( "opencv example", 1 )
69     add_shortcut( "opencv_example" )
70
71     set_category( CAT_VIDEO )
72     set_subcategory( SUBCAT_VIDEO_VFILTER2 )
73     set_callbacks( OpenFilter, CloseFilter )
74
75     add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml", NULL,
76                           N_("Haar cascade filename"),
77                           N_("Name of XML file containing Haar cascade description"), false);
78 vlc_module_end ()
79
80 /*****************************************************************************
81  * OpenFilter: probe the filter and return score
82  *****************************************************************************/
83 static int OpenFilter( vlc_object_t *p_this )
84 {
85     filter_t *p_filter = (filter_t*)p_this;
86     filter_sys_t *p_sys;
87
88     /* Allocate the memory needed to store the decoder's structure */
89     if( ( p_filter->p_sys = p_sys =
90           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
91     {
92         return VLC_ENOMEM;
93     }
94
95     //init the video_filter_event_info_t struct
96     p_sys->event_info.i_region_size = 0;
97     p_sys->event_info.p_region = NULL;
98     p_sys->i_id = 0;
99
100     p_filter->pf_video_filter = Filter;
101
102     //create the VIDEO_FILTER_EVENT_VARIABLE
103     vlc_value_t val;
104     if (var_Create( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_ADDRESS | VLC_VAR_DOINHERIT ) != VLC_SUCCESS)
105         msg_Err( p_filter, "Could not create %s", VIDEO_FILTER_EVENT_VARIABLE);
106
107     val.p_address = &(p_sys->event_info);
108     if (var_Set( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, val )!=VLC_SUCCESS)
109         msg_Err( p_filter, "Could not set %s", VIDEO_FILTER_EVENT_VARIABLE);
110
111     //OpenCV init specific to this example
112     char* filename = var_InheritString( p_filter, "opencv-haarcascade-file" );
113     p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 );
114     p_sys->p_storage = cvCreateMemStorage(0);
115     free( filename );
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * CloseFilter: clean up the filter
122  *****************************************************************************/
123 static void CloseFilter( vlc_object_t *p_this )
124 {
125     filter_t *p_filter = (filter_t*)p_this;
126     filter_sys_t *p_sys = p_filter->p_sys;
127
128     if( p_sys->p_cascade )
129         cvReleaseHaarClassifierCascade( &p_sys->p_cascade );
130
131     if( p_sys->p_storage )
132         cvReleaseMemStorage( &p_sys->p_storage );
133
134     free( p_sys->event_info.p_region );
135     free( p_sys );
136
137     var_Destroy( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE);
138 }
139
140 /****************************************************************************
141  * Filter: Check for faces and raises an event when one is found.
142  ****************************************************************************
143  * p_pic: A picture_t with its p_data_orig member set to an array of
144  * IplImages (one image for each picture_t plane).
145  ****************************************************************************/
146 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
147 {
148     IplImage** p_img = NULL;
149     int i_planes = 0;
150     CvPoint pt1, pt2;
151     int i, scale = 1;
152     filter_sys_t *p_sys = p_filter->p_sys;
153  
154     if ((!p_pic) )
155     {
156         msg_Err( p_filter, "no image array" );
157         return NULL;
158     }
159     if (!(p_pic->p_data_orig))
160     {
161         msg_Err( p_filter, "no image array" );
162         return NULL;
163     }
164     //(hack) cast the picture_t to array of IplImage*
165     p_img = (IplImage**) p_pic->p_data_orig;
166     i_planes = p_pic->i_planes;
167
168     //check the image array for validity
169     if ((!p_img[0]))    //1st plane is 'I' i.e. greyscale
170     {
171         msg_Err( p_filter, "no image" );
172         return NULL;
173     }
174     if ((p_pic->format.i_chroma != VLC_CODEC_I420))
175     {
176         msg_Err( p_filter, "wrong chroma - use I420" );
177         return NULL;
178     }
179     if (i_planes<1)
180     {
181         msg_Err( p_filter, "no image planes" );
182         return NULL;
183     }
184
185     //perform face detection
186     cvClearMemStorage(p_sys->p_storage);
187     if( p_sys->p_cascade )
188     {
189         //we should make some of these params config variables
190         CvSeq *faces = cvHaarDetectObjects( p_img[0], p_sys->p_cascade,
191                                             p_sys->p_storage, 1.15, 5,
192                                             CV_HAAR_DO_CANNY_PRUNING,
193                                             cvSize(20, 20) );
194         //create the video_filter_region_info_t struct
195         if (faces && (faces->total > 0))
196         {
197             //msg_Dbg( p_filter, "Found %d face(s)", faces->total );
198             free( p_sys->event_info.p_region );
199             p_sys->event_info.p_region = (video_filter_region_info_t*)
200                     calloc( faces->total, sizeof(video_filter_region_info_t));
201             if( !p_sys->event_info.p_region )
202                 return NULL;
203             p_sys->event_info.i_region_size = faces->total;
204         }
205
206         //populate the video_filter_region_info_t struct
207         for( i = 0; i < (faces ? faces->total : 0); i++ )
208         {
209             CvRect *r = (CvRect*)cvGetSeqElem( faces, i );
210             pt1.x = r->x*scale;
211             pt2.x = (r->x+r->width)*scale;
212             pt1.y = r->y*scale;
213             pt2.y = (r->y+r->height)*scale;
214             cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 );
215
216             *(CvRect*)(&(p_sys->event_info.p_region[i])) = *r;
217             p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
218             p_sys->event_info.p_region[i].p_description = "Face Detected";
219         }
220
221         if (faces && (faces->total > 0))    //raise the video filter event
222             var_TriggerCallback( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE );
223     }
224     else
225         msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
226
227     return p_pic;
228 }
229