qt: playlist: use item title if available
[vlc.git] / modules / video_filter / opencv_example.cpp
blob3a4a30e101143dd0ddf0b7aaceaf1f759ba1db75
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 VLC authors and VideoLAN
7 * Copyright (C) 2012 Edward Wang
9 * Authors: Dugal Harris <dugalh@protoclea.co.za>
10 * Edward Wang <edward.c.wang@compdigitec.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include <vlc_picture.h>
39 #include <vlc_vout.h>
40 #include <vlc_image.h>
41 #include "filter_event_info.h"
43 #include <opencv2/core/core_c.h>
44 #include <opencv2/core/core.hpp>
45 #include <opencv2/imgproc/imgproc_c.h>
46 #include <opencv2/imgproc/imgproc.hpp>
47 #include <opencv2/objdetect/objdetect.hpp>
49 /*****************************************************************************
50 * filter_sys_t : filter descriptor
51 *****************************************************************************/
53 namespace {
55 struct filter_sys_t
57 CvMemStorage* p_storage;
58 CvHaarClassifierCascade* p_cascade;
59 video_filter_event_info_t event_info;
60 int i_id;
63 } // namespace
65 /****************************************************************************
66 * Local prototypes
67 ****************************************************************************/
68 static int OpenFilter ( vlc_object_t * );
69 static void CloseFilter( filter_t * );
71 static picture_t *Filter( filter_t *, picture_t * );
73 /*****************************************************************************
74 * Module descriptor
75 *****************************************************************************/
76 vlc_module_begin ()
77 set_description( N_("OpenCV face detection example filter") )
78 set_shortname( N_( "OpenCV example" ))
79 set_capability( "opencv internal filter", 1 )
80 add_shortcut( "opencv_example" )
82 set_category( CAT_VIDEO )
83 set_subcategory( SUBCAT_VIDEO_VFILTER )
84 set_callback( OpenFilter )
86 add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml",
87 N_("Haar cascade filename"),
88 N_("Name of XML file containing Haar cascade description"), false);
89 vlc_module_end ()
91 /*****************************************************************************
92 * OpenFilter: probe the filter and return score
93 *****************************************************************************/
94 static int OpenFilter( vlc_object_t *p_this )
96 filter_t *p_filter = (filter_t*)p_this;
97 filter_sys_t *p_sys;
99 /* Allocate the memory needed to store the decoder's structure */
100 if( ( p_filter->p_sys = p_sys =
101 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
103 return VLC_ENOMEM;
106 //init the video_filter_event_info_t struct
107 p_sys->event_info.i_region_size = 0;
108 p_sys->event_info.p_region = NULL;
109 p_sys->i_id = 0;
111 static const struct FilterOperationInitializer {
112 struct vlc_filter_operations ops {};
113 FilterOperationInitializer()
115 ops.filter_video = Filter;
116 ops.close = CloseFilter;
118 } filter_ops;
119 p_filter->ops = &filter_ops.ops;
121 //create the VIDEO_FILTER_EVENT_VARIABLE
122 vlc_value_t val;
123 if (var_Create( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_ADDRESS | VLC_VAR_DOINHERIT ) != VLC_SUCCESS)
124 msg_Err( p_filter, "Could not create %s", VIDEO_FILTER_EVENT_VARIABLE);
126 val.p_address = &(p_sys->event_info);
127 if (var_Set( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE, val )!=VLC_SUCCESS)
128 msg_Err( p_filter, "Could not set %s", VIDEO_FILTER_EVENT_VARIABLE);
130 //OpenCV init specific to this example
131 char* filename = var_InheritString( p_filter, "opencv-haarcascade-file" );
132 p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 );
133 p_sys->p_storage = cvCreateMemStorage(0);
134 free( filename );
136 return VLC_SUCCESS;
139 /*****************************************************************************
140 * CloseFilter: clean up the filter
141 *****************************************************************************/
142 static void CloseFilter( filter_t *p_filter )
144 filter_sys_t *p_sys = static_cast<filter_sys_t *>(p_filter->p_sys);
146 if( p_sys->p_cascade )
147 cvReleaseHaarClassifierCascade( &p_sys->p_cascade );
149 if( p_sys->p_storage )
150 cvReleaseMemStorage( &p_sys->p_storage );
152 free( p_sys->event_info.p_region );
153 free( p_sys );
155 var_Destroy( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE);
158 /****************************************************************************
159 * Filter: Check for faces and raises an event when one is found.
160 ****************************************************************************/
161 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
163 IplImage** p_img = NULL;
164 CvPoint pt1, pt2;
165 int scale = 1;
166 filter_sys_t *p_sys = static_cast<filter_sys_t *>(p_filter->p_sys);
168 if ((!p_pic) )
170 msg_Err( p_filter, "no image array" );
171 return NULL;
173 //(hack) cast the picture_t to array of IplImage*
174 p_img = (IplImage**) p_pic;
176 //check the image array for validity
177 if ((!p_img[0])) //1st plane is 'I' i.e. greyscale
179 msg_Err( p_filter, "no image" );
180 return NULL;
183 //perform face detection
184 cvClearMemStorage(p_sys->p_storage);
185 if( p_sys->p_cascade )
187 //we should make some of these params config variables
188 CvSeq *faces = cvHaarDetectObjects( p_img[0], p_sys->p_cascade,
189 p_sys->p_storage, 1.15, 5,
190 CV_HAAR_DO_CANNY_PRUNING,
191 cvSize(20, 20) );
192 //create the video_filter_region_info_t struct
193 if (faces && (faces->total > 0))
195 //msg_Dbg( p_filter, "Found %d face(s)", faces->total );
196 free( p_sys->event_info.p_region );
197 p_sys->event_info.p_region = (video_filter_region_info_t*)
198 calloc( faces->total, sizeof(video_filter_region_info_t));
199 if( !p_sys->event_info.p_region )
200 return NULL;
201 p_sys->event_info.i_region_size = faces->total;
204 //populate the video_filter_region_info_t struct
205 for( int i = 0; i < (faces ? faces->total : 0); i++ )
207 CvRect *r = (CvRect*)cvGetSeqElem( faces, i );
208 pt1.x = r->x*scale;
209 pt2.x = (r->x+r->width)*scale;
210 pt1.y = r->y*scale;
211 pt2.y = (r->y+r->height)*scale;
212 cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 );
214 *(CvRect*)(&(p_sys->event_info.p_region[i])) = *r;
215 p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
216 p_sys->event_info.p_region[i].p_description = "Face Detected";
219 if (faces && (faces->total > 0)) //raise the video filter event
220 var_TriggerCallback( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE );
222 else
223 msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
225 return p_pic;