[vlc-devel] [PATCH] Port OpenCV facedetect example to C++ API

Stefan BrĂ¼ns stefan.bruens at rwth-aachen.de
Sun Jan 12 19:42:45 CET 2020


This allows compilation with OpenCV 3.4.9 (which removed some of
the old C API), and likely also works with OpenCV 4.

Tested with:
cvlc --video-filter=opencv_wrapper \
  --opencv-filter-name opencv_example \
  --opencv-chroma I420 \
  --opencv-haarcascade-file /tmp/haarcascade_frontalface_default.xml \
  v4l2:///dev/video0
---
 modules/video_filter/opencv_example.cpp | 117 +++++++++++-------------
 1 file changed, 52 insertions(+), 65 deletions(-)

diff --git a/modules/video_filter/opencv_example.cpp b/modules/video_filter/opencv_example.cpp
index 1334cd4c36..ce5f83ab4b 100644
--- a/modules/video_filter/opencv_example.cpp
+++ b/modules/video_filter/opencv_example.cpp
@@ -40,11 +40,7 @@
 #include <vlc_image.h>
 #include "filter_event_info.h"
 
-#include <opencv2/core/core_c.h>
-#include <opencv2/core/core.hpp>
-#include <opencv2/imgproc/imgproc_c.h>
-#include <opencv2/imgproc/imgproc.hpp>
-#include <opencv2/objdetect/objdetect.hpp>
+#include <opencv2/opencv.hpp>
 
 /*****************************************************************************
  * filter_sys_t : filter descriptor
@@ -52,10 +48,11 @@
 
 namespace {
 
-struct filter_sys_t
+class filter_sys_t
 {
-    CvMemStorage* p_storage;
-    CvHaarClassifierCascade* p_cascade;
+public:
+    cv::CascadeClassifier cascade;
+    std::vector<video_filter_region_info_t> event_info_storage;
     video_filter_event_info_t event_info;
     int i_id;
 };
@@ -97,15 +94,15 @@ static int OpenFilter( vlc_object_t *p_this )
     filter_sys_t *p_sys;
 
     /* Allocate the memory needed to store the decoder's structure */
-    if( ( p_filter->p_sys = p_sys =
-          (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
+    p_filter->p_sys = p_sys = new filter_sys_t;
+    if(p_sys == nullptr)
     {
         return VLC_ENOMEM;
     }
 
     //init the video_filter_event_info_t struct
     p_sys->event_info.i_region_size = 0;
-    p_sys->event_info.p_region = NULL;
+    p_sys->event_info.p_region = nullptr;
     p_sys->i_id = 0;
 
     p_filter->pf_video_filter = Filter;
@@ -121,8 +118,9 @@ static int OpenFilter( vlc_object_t *p_this )
 
     //OpenCV init specific to this example
     char* filename = var_InheritString( p_filter, "opencv-haarcascade-file" );
-    p_sys->p_cascade = (CvHaarClassifierCascade*)cvLoad( filename, 0, 0, 0 );
-    p_sys->p_storage = cvCreateMemStorage(0);
+    if (!p_sys->cascade.load(filename)) {
+        msg_Err( p_filter, "Could not load %s", filename);
+    }
     free( filename );
 
     return VLC_SUCCESS;
@@ -136,14 +134,7 @@ static void CloseFilter( vlc_object_t *p_this )
     filter_t *p_filter = (filter_t*)p_this;
     filter_sys_t *p_sys = static_cast<filter_sys_t *>(p_filter->p_sys);
 
-    if( p_sys->p_cascade )
-        cvReleaseHaarClassifierCascade( &p_sys->p_cascade );
-
-    if( p_sys->p_storage )
-        cvReleaseMemStorage( &p_sys->p_storage );
-
-    free( p_sys->event_info.p_region );
-    free( p_sys );
+    delete p_sys;
 
     var_Destroy( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE);
 }
@@ -153,15 +144,13 @@ static void CloseFilter( vlc_object_t *p_this )
  ****************************************************************************/
 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 {
-    IplImage** p_img = NULL;
-    CvPoint pt1, pt2;
-    int scale = 1;
+    IplImage** p_img = nullptr;
     filter_sys_t *p_sys = static_cast<filter_sys_t *>(p_filter->p_sys);
- 
+
     if ((!p_pic) )
     {
         msg_Err( p_filter, "no image array" );
-        return NULL;
+        return nullptr;
     }
     //(hack) cast the picture_t to array of IplImage*
     p_img = (IplImage**) p_pic;
@@ -170,50 +159,48 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     if ((!p_img[0]))    //1st plane is 'I' i.e. greyscale
     {
         msg_Err( p_filter, "no image" );
-        return NULL;
+        return nullptr;
     }
+    // Wrapper for IplImage* -> cv::Mat (reference, not copy)
+    cv::Mat gray = cv::cvarrToMat(p_img[0]);
 
     //perform face detection
-    cvClearMemStorage(p_sys->p_storage);
-    if( p_sys->p_cascade )
+    if (p_sys->cascade.empty())
+        msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
+
+    //we should make some of these params config variables
+    std::vector<cv::Rect> faces;
+    p_sys->cascade.detectMultiScale(gray, faces,
+                                    1.15, 5,
+                                    CV_HAAR_DO_CANNY_PRUNING,
+                                    cv::Size(20, 20));
+    //create the video_filter_region_info_t struct
+    if (faces.empty())
+        return p_pic;
+
+    //msg_Dbg( p_filter, "Found %d face(s)", faces.size());
+    p_sys->event_info_storage.resize(faces.size());
+    p_sys->event_info.i_region_size = faces.size();
+    p_sys->event_info.p_region = p_sys->event_info_storage.data();
+    if( !p_sys->event_info.p_region )
+        return nullptr;
+
+    //populate the video_filter_region_info_t struct
+    for( int i = 0; i < faces.size(); i++ )
     {
-        //we should make some of these params config variables
-        CvSeq *faces = cvHaarDetectObjects( p_img[0], p_sys->p_cascade,
-                                            p_sys->p_storage, 1.15, 5,
-                                            CV_HAAR_DO_CANNY_PRUNING,
-                                            cvSize(20, 20) );
-        //create the video_filter_region_info_t struct
-        if (faces && (faces->total > 0))
-        {
-            //msg_Dbg( p_filter, "Found %d face(s)", faces->total );
-            free( p_sys->event_info.p_region );
-            p_sys->event_info.p_region = (video_filter_region_info_t*)
-                    calloc( faces->total, sizeof(video_filter_region_info_t));
-            if( !p_sys->event_info.p_region )
-                return NULL;
-            p_sys->event_info.i_region_size = faces->total;
-        }
-
-        //populate the video_filter_region_info_t struct
-        for( int i = 0; i < (faces ? faces->total : 0); i++ )
-        {
-            CvRect *r = (CvRect*)cvGetSeqElem( faces, i );
-            pt1.x = r->x*scale;
-            pt2.x = (r->x+r->width)*scale;
-            pt1.y = r->y*scale;
-            pt2.y = (r->y+r->height)*scale;
-            cvRectangle( p_img[0], pt1, pt2, CV_RGB(0,0,0), 3, 8, 0 );
-
-            *(CvRect*)(&(p_sys->event_info.p_region[i])) = *r;
-            p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
-            p_sys->event_info.p_region[i].p_description = "Face Detected";
-        }
-
-        if (faces && (faces->total > 0))    //raise the video filter event
-            var_TriggerCallback( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE );
+        const cv::Rect& r = faces[i];
+        cv::rectangle(gray, r, CV_RGB(0,0,0), 3);
+
+        p_sys->event_info.p_region[i].i_x = r.x;
+        p_sys->event_info.p_region[i].i_y = r.y;
+        p_sys->event_info.p_region[i].i_width = r.width;
+        p_sys->event_info.p_region[i].i_height = r.height;
+
+        p_sys->event_info.p_region[i].i_id = p_sys->i_id++;
+        p_sys->event_info.p_region[i].p_description = "Face Detected";
     }
-    else
-        msg_Err( p_filter, "No cascade - is opencv-haarcascade-file valid?" );
+
+    var_TriggerCallback( vlc_object_instance(p_filter), VIDEO_FILTER_EVENT_VARIABLE );
 
     return p_pic;
 }
-- 
2.24.1



More information about the vlc-devel mailing list