File 0001-Port-OpenCV-facedetect-example-to-C-API.patch of Package vlc
From f53677bf35c6b0a669d799182d6383f350d1503d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Br=C3=BCns?= <stefan.bruens@rwth-aachen.de>
Date: Sun, 12 Jan 2020 17:37:53 +0100
Subject: [PATCH] Port OpenCV facedetect example to C++ API
Tested with:
cvlc --video-filter=opencv_wrapper \
--opencv-filter-name opencv_example \
--opencv-chroma RGB32 \
--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 a7a0bd7821..87a3451f1a 100644
--- a/modules/video_filter/opencv_example.cpp
+++ b/modules/video_filter/opencv_example.cpp
@@ -40,19 +40,16 @@
#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
*****************************************************************************/
-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;
};
@@ -92,15 +89,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;
@@ -116,8 +113,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;
@@ -131,14 +129,7 @@ static void CloseFilter( vlc_object_t *p_this )
filter_t *p_filter = (filter_t*)p_this;
filter_sys_t *p_sys = 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( p_filter->obj.libvlc, VIDEO_FILTER_EVENT_VARIABLE);
}
@@ -148,15 +139,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 = 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;
@@ -165,50 +154,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( p_filter->obj.libvlc, 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( p_filter->obj.libvlc, VIDEO_FILTER_EVENT_VARIABLE );
return p_pic;
}
--
2.24.1