OpenCV example: move to C++ and opencv2 APIs
[vlc.git] / modules / video_filter / opencv_example.cpp
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-2012 the VideoLAN team
7  * Copyright (C) 2012 Edward Wang
8  *
9  * Authors: Dugal Harris <dugalh@protoclea.co.za>
10  *          Edward Wang <edward.c.wang@compdigitec.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include <vlc_vout.h>
39 #include <vlc_image.h>
40 #include "filter_event_info.h"
41
42 #include <opencv2/core/core_c.h>
43 #include <opencv2/core/core.hpp>
44 #include <opencv2/objdetect/objdetect.hpp>
45
46 /*****************************************************************************
47  * filter_sys_t : filter descriptor
48  *****************************************************************************/
49 struct filter_sys_t
50 {
51     CvMemStorage* p_storage;
52     CvHaarClassifierCascade* p_cascade;
53     video_filter_event_info_t event_info;
54     int i_id;
55 };
56
57 /****************************************************************************
58  * Local prototypes
59  ****************************************************************************/
60 static int  OpenFilter ( vlc_object_t * );
61 static void CloseFilter( vlc_object_t * );
62
63 static picture_t *Filter( filter_t *, picture_t * );
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 vlc_module_begin ()
69     set_description( N_("OpenCV face detection example filter") )
70     set_shortname( N_( "OpenCV example" ))
71     set_capability( "opencv internal filter", 1 )
72     add_shortcut( "opencv_example" )
73
74     set_category( CAT_VIDEO )
75     set_subcategory( SUBCAT_VIDEO_VFILTER2 )
76     set_callbacks( OpenFilter, CloseFilter )
77
78     add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml",
79                           N_("Haar cascade filename"),
80                           N_("Name of XML file containing Haar cascade description"), false);
81 vlc_module_end ()
82
83 /*****************************************************************************
84  * OpenFilter: probe the filter and return score
85  *****************************************************************************/
86 static int OpenFilter( vlc_object_t *p_this )
87 {
88     filter_t *p_filter = (filter_t*)p_this;
89     filter_sys_t *p_sys;
90
91     /* Allocate the memory needed to store the decoder's structure */
92     if( ( p_filter->p_sys = p_sys =
93           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
94     {
95         return VLC_ENOMEM;
96     }
97
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;
101     p_sys->i_id = 0;
102
103     p_filter->pf_video_filter = Filter;
104
105     //create the VIDEO_FILTER_EVENT_VARIABLE
106     vlc_value_t val;
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", VIDEO_FILTER_EVENT_VARIABLE);
109
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", VIDEO_FILTER_EVENT_VARIABLE);
113
114     //OpenCV init specific to this example
115     char* filename = var_InheritString( p_filter, "opencv-haarcascade-file" );
116     p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 );
117     p_sys->p_storage = cvCreateMemStorage(0);
118     free( filename );
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * CloseFilter: clean up the filter
125  *****************************************************************************/
126 static void CloseFilter( vlc_object_t *p_this )
127 {
128     filter_t *p_filter = (filter_t*)p_this;
129     filter_sys_t *p_sys = p_filter->p_sys;
130
131     if( p_sys->p_cascade )
132         cvReleaseHaarClassifierCascade( &p_sys->p_cascade );
133
134     if( p_sys->p_storage )
135         cvReleaseMemStorage( &p_sys->p_storage );
136
137     free( p_sys->event_info.p_region );
138     free( p_sys );
139
140     var_Destroy( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE);
141 }
142
143 /****************************************************************************
144  * Filter: Check for faces and raises an event when one is found.
145  ****************************************************************************/
146 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
147 {
148     IplImage** p_img = NULL;
149     CvPoint pt1, pt2;
150     int i, scale = 1;
151     filter_sys_t *p_sys = p_filter->p_sys;
152  
153     if ((!p_pic) )
154     {
155         msg_Err( p_filter, "no image array" );
156         return NULL;
157     }
158     //(hack) cast the picture_t to array of IplImage*
159     p_img = (IplImage**) p_pic;
160
161     //check the image array for validity
162     if ((!p_img[0]))    //1st plane is 'I' i.e. greyscale
163     {
164         msg_Err( p_filter, "no image" );
165         return NULL;
166     }
167
168     //perform face detection
169     cvClearMemStorage(p_sys->p_storage);
170     if( p_sys->p_cascade )
171     {
172         //we should make some of these params config variables
173         CvSeq *faces = cvHaarDetectObjects( p_img[0], p_sys->p_cascade,
174                                             p_sys->p_storage, 1.15, 5,
175                                             CV_HAAR_DO_CANNY_PRUNING,
176                                             cvSize(20, 20) );
177         //create the video_filter_region_info_t struct
178         if (faces && (faces->total > 0))
179         {
180             //msg_Dbg( p_filter, "Found %d face(s)", faces->total );
181             free( p_sys->event_info.p_region );
182             p_sys->event_info.p_region = (video_filter_region_info_t*)
183                     calloc( faces->total, sizeof(video_filter_region_info_t));
184             if( !p_sys->event_info.p_region )
185                 return NULL;
186             p_sys->event_info.i_region_size = faces->total;
187         }
188
189         //populate the video_filter_region_info_t struct
190         for( i = 0; i < (faces ? faces->total : 0); i++ )
191         {
192             CvRect *r = (CvRect*)cvGetSeqElem( faces, i );
193             pt1.x = r->x*scale;
194             pt2.x = (r->x+r->width)*scale;
195             pt1.y = r->y*scale;
196             pt2.y = (r->y+r->height)*scale;
197             cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 );
198
199             *(CvRect*)(&(p_sys->event_info.p_region[i])) = *r;
200             p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
201             p_sys->event_info.p_region[i].p_description = "Face Detected";
202         }
203
204         if (faces && (faces->total > 0))    //raise the video filter event
205             var_TriggerCallback( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE );
206     }
207     else
208         msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
209
210     return p_pic;
211 }
212