opencv_example.cpp
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:9k
源码类别:

midi

开发平台:

Unix_Linux

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