| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#include <cxcore.h> |
|---|
| 29 |
#include <cv.h> |
|---|
| 30 |
#include <highgui.h> |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
#ifdef HAVE_CONFIG_H |
|---|
| 34 |
# include "config.h" |
|---|
| 35 |
#endif |
|---|
| 36 |
|
|---|
| 37 |
#include <vlc_common.h> |
|---|
| 38 |
#include <vlc_plugin.h> |
|---|
| 39 |
#include <vlc_filter.h> |
|---|
| 40 |
#include "filter_common.h" |
|---|
| 41 |
#include <vlc_image.h> |
|---|
| 42 |
#include "filter_event_info.h" |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
struct filter_sys_t |
|---|
| 48 |
{ |
|---|
| 49 |
CvMemStorage* p_storage; |
|---|
| 50 |
CvHaarClassifierCascade* p_cascade; |
|---|
| 51 |
video_filter_event_info_t event_info; |
|---|
| 52 |
int i_id; |
|---|
| 53 |
}; |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
static int OpenFilter ( vlc_object_t * ); |
|---|
| 59 |
static void CloseFilter( vlc_object_t * ); |
|---|
| 60 |
|
|---|
| 61 |
static picture_t *Filter( filter_t *, picture_t * ); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
vlc_module_begin(); |
|---|
| 67 |
set_description( N_("OpenCV face detection example filter") ); |
|---|
| 68 |
set_shortname( N_( "OpenCV example" )); |
|---|
| 69 |
set_capability( "opencv example", 1 ); |
|---|
| 70 |
add_shortcut( "opencv_example" ); |
|---|
| 71 |
|
|---|
| 72 |
set_category( CAT_VIDEO ); |
|---|
| 73 |
set_subcategory( SUBCAT_VIDEO_VFILTER2 ); |
|---|
| 74 |
set_callbacks( OpenFilter, CloseFilter ); |
|---|
| 75 |
|
|---|
| 76 |
add_string( "opencv-haarcascade-file", "c:\\haarcascade_frontalface_alt.xml", NULL, |
|---|
| 77 |
N_("Haar cascade filename"), |
|---|
| 78 |
N_("Name of XML file containing Haar cascade description"), false); |
|---|
| 79 |
vlc_module_end(); |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
static int OpenFilter( vlc_object_t *p_this ) |
|---|
| 85 |
{ |
|---|
| 86 |
filter_t *p_filter = (filter_t*)p_this; |
|---|
| 87 |
filter_sys_t *p_sys; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
if( ( p_filter->p_sys = p_sys = |
|---|
| 91 |
(filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL ) |
|---|
| 92 |
{ |
|---|
| 93 |
return VLC_ENOMEM; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
p_sys->event_info.i_region_size = 0; |
|---|
| 98 |
p_sys->event_info.p_region = NULL; |
|---|
| 99 |
p_sys->i_id = 0; |
|---|
| 100 |
|
|---|
| 101 |
p_filter->pf_video_filter = Filter; |
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
vlc_value_t val; |
|---|
| 105 |
if (var_Create( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_ADDRESS | VLC_VAR_DOINHERIT ) != VLC_SUCCESS) |
|---|
| 106 |
msg_Err( p_filter, "Could not create %s\n", VIDEO_FILTER_EVENT_VARIABLE); |
|---|
| 107 |
|
|---|
| 108 |
val.p_address = &(p_sys->event_info); |
|---|
| 109 |
if (var_Set( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, val )!=VLC_SUCCESS) |
|---|
| 110 |
msg_Err( p_filter, "Could not set %s\n", VIDEO_FILTER_EVENT_VARIABLE); |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
char* filename = config_GetPsz( p_filter, "opencv-haarcascade-file" ); |
|---|
| 114 |
p_filter->p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 ); |
|---|
| 115 |
p_filter->p_sys->p_storage = cvCreateMemStorage(0); |
|---|
| 116 |
free( filename ); |
|---|
| 117 |
|
|---|
| 118 |
return VLC_SUCCESS; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
static void CloseFilter( vlc_object_t *p_this ) |
|---|
| 125 |
{ |
|---|
| 126 |
filter_t *p_filter = (filter_t*)p_this; |
|---|
| 127 |
filter_sys_t *p_sys = p_filter->p_sys; |
|---|
| 128 |
|
|---|
| 129 |
if( p_filter->p_sys->p_cascade ) |
|---|
| 130 |
cvReleaseHaarClassifierCascade( &p_filter->p_sys->p_cascade ); |
|---|
| 131 |
|
|---|
| 132 |
if( p_filter->p_sys->p_storage ) |
|---|
| 133 |
cvReleaseMemStorage( &p_filter->p_sys->p_storage ); |
|---|
| 134 |
|
|---|
| 135 |
if (NULL != p_filter->p_sys->event_info.p_region) |
|---|
| 136 |
free(p_filter->p_sys->event_info.p_region); |
|---|
| 137 |
|
|---|
| 138 |
free( p_sys ); |
|---|
| 139 |
|
|---|
| 140 |
var_Destroy( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE); |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic ) |
|---|
| 150 |
{ |
|---|
| 151 |
IplImage** p_img = NULL; |
|---|
| 152 |
int i_planes = 0; |
|---|
| 153 |
CvPoint pt1, pt2; |
|---|
| 154 |
int i, scale = 1; |
|---|
| 155 |
|
|---|
| 156 |
if ((!p_pic) ) |
|---|
| 157 |
{ |
|---|
| 158 |
msg_Err( p_filter, "no image array" ); |
|---|
| 159 |
return NULL; |
|---|
| 160 |
} |
|---|
| 161 |
if (!(p_pic->p_data_orig)) |
|---|
| 162 |
{ |
|---|
| 163 |
msg_Err( p_filter, "no image array" ); |
|---|
| 164 |
return NULL; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
p_img = (IplImage**) p_pic->p_data_orig; |
|---|
| 168 |
i_planes = p_pic->i_planes; |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
if ((!p_img[0])) |
|---|
| 172 |
{ |
|---|
| 173 |
msg_Err( p_filter, "no image" ); |
|---|
| 174 |
return NULL; |
|---|
| 175 |
} |
|---|
| 176 |
if ((p_pic->format.i_chroma != VLC_FOURCC('I','4','2','0'))) |
|---|
| 177 |
{ |
|---|
| 178 |
msg_Err( p_filter, "wrong chroma - use I420" ); |
|---|
| 179 |
return NULL; |
|---|
| 180 |
} |
|---|
| 181 |
if (i_planes<1) |
|---|
| 182 |
{ |
|---|
| 183 |
msg_Err( p_filter, "no image planes" ); |
|---|
| 184 |
return NULL; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
cvClearMemStorage(p_filter->p_sys->p_storage); |
|---|
| 189 |
CvSeq* faces = NULL; |
|---|
| 190 |
if( p_filter->p_sys->p_cascade ) |
|---|
| 191 |
{ |
|---|
| 192 |
|
|---|
| 193 |
faces = cvHaarDetectObjects( p_img[0], p_filter->p_sys->p_cascade, |
|---|
| 194 |
p_filter->p_sys->p_storage, 1.15, 5, CV_HAAR_DO_CANNY_PRUNING, |
|---|
| 195 |
cvSize(20, 20) ); |
|---|
| 196 |
|
|---|
| 197 |
CvRect* r; |
|---|
| 198 |
if (faces && (faces->total > 0)) |
|---|
| 199 |
{ |
|---|
| 200 |
|
|---|
| 201 |
if (NULL != p_filter->p_sys->event_info.p_region) |
|---|
| 202 |
{ |
|---|
| 203 |
free(p_filter->p_sys->event_info.p_region); |
|---|
| 204 |
p_filter->p_sys->event_info.p_region = NULL; |
|---|
| 205 |
} |
|---|
| 206 |
if( NULL == ( p_filter->p_sys->event_info.p_region = |
|---|
| 207 |
(video_filter_region_info_t *)malloc(faces->total*sizeof(video_filter_region_info_t)))) |
|---|
| 208 |
{ |
|---|
| 209 |
return NULL; |
|---|
| 210 |
} |
|---|
| 211 |
memset(p_filter->p_sys->event_info.p_region, 0, faces->total*sizeof(video_filter_region_info_t)); |
|---|
| 212 |
p_filter->p_sys->event_info.i_region_size = faces->total; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
for( i = 0; i < (faces ? faces->total : 0); i++ ) |
|---|
| 217 |
{ |
|---|
| 218 |
r = (CvRect*)cvGetSeqElem( faces, i ); |
|---|
| 219 |
pt1.x = r->x*scale; |
|---|
| 220 |
pt2.x = (r->x+r->width)*scale; |
|---|
| 221 |
pt1.y = r->y*scale; |
|---|
| 222 |
pt2.y = (r->y+r->height)*scale; |
|---|
| 223 |
cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 ); |
|---|
| 224 |
|
|---|
| 225 |
*(CvRect*)(&(p_filter->p_sys->event_info.p_region[i])) = *r; |
|---|
| 226 |
p_filter->p_sys->event_info.p_region[i].i_id = p_filter->p_sys->i_id++; |
|---|
| 227 |
p_filter->p_sys->event_info.p_region[i].p_description = "Face Detected"; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
if (faces && (faces->total > 0)) |
|---|
| 231 |
var_Change( p_filter->p_libvlc, VIDEO_FILTER_EVENT_VARIABLE, VLC_VAR_TRIGGER_CALLBACKS, NULL, NULL ); |
|---|
| 232 |
} |
|---|
| 233 |
else |
|---|
| 234 |
msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" ); |
|---|
| 235 |
|
|---|
| 236 |
return p_pic; |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|