1 /*
2 * GStreamer
3 * Copyright (C) 2013 Miguel Casas-Sanchez <miguelecasassanchez@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Alternatively, the contents of this file may be used under the
24 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25 * which case the following provisions apply instead of the ones
26 * mentioned above:
27 *
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Library General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
32 *
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Library General Public License for more details.
37 *
38 * You should have received a copy of the GNU Library General Public
39 * License along with this library; if not, write to the
40 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41 * Boston, MA 02110-1301, USA.
42 */
43
44 /**
45 * SECTION:element-skindetect
46 *
47 * Human skin detection on videos and images
48 *
49 * ## Example launch line
50 *
51 * |[
52 * gst-launch-1.0 videotestsrc ! decodebin ! videoconvert ! skindetect ! videoconvert ! xvimagesink
53 * ]|
54 */
55
56 #ifdef HAVE_CONFIG_H
57 #include <config.h>
58 #endif
59
60 #include "gstskindetect.h"
61 #include <opencv2/imgproc.hpp>
62
63 GST_DEBUG_CATEGORY_STATIC (gst_skin_detect_debug);
64 #define GST_CAT_DEFAULT gst_skin_detect_debug
65
66 /* Filter signals and args */
67 enum
68 {
69 /* FILL ME */
70 LAST_SIGNAL
71 };
72
73 enum
74 {
75 PROP_0,
76 PROP_POSTPROCESS,
77 PROP_METHOD,
78 PROP_MASK
79 };
80 typedef enum
81 {
82 HSV,
83 RGB
84 } GstSkindetectMethod;
85
86 #define GST_TYPE_SKIN_DETECT_METHOD (gst_skin_detect_method_get_type ())
87 static GType
gst_skin_detect_method_get_type(void)88 gst_skin_detect_method_get_type (void)
89 {
90 static GType etype = 0;
91 if (etype == 0) {
92 static const GEnumValue values[] = {
93 {HSV, "Classic HSV thresholding", "hsv"},
94 {RGB, "Normalised-RGB colorspace thresholding", "rgb"},
95 {0, NULL, NULL},
96 };
97 etype = g_enum_register_static ("GstSkindetectMethod", values);
98 }
99 return etype;
100 }
101
102 G_DEFINE_TYPE_WITH_CODE (GstSkinDetect, gst_skin_detect,
103 GST_TYPE_OPENCV_VIDEO_FILTER,
104 GST_DEBUG_CATEGORY_INIT (gst_skin_detect_debug, "skindetect", 0,
105 "Performs skin detection on videos and images");
106 );
107 GST_ELEMENT_REGISTER_DEFINE (skindetect, "skindetect", GST_RANK_NONE,
108 GST_TYPE_SKIN_DETECT);
109
110 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
111 GST_PAD_SINK,
112 GST_PAD_ALWAYS,
113 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")));
114
115 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
116 GST_PAD_SRC,
117 GST_PAD_ALWAYS,
118 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")));
119
120
121 static void gst_skin_detect_set_property (GObject * object, guint prop_id,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_skin_detect_get_property (GObject * object, guint prop_id,
124 GValue * value, GParamSpec * pspec);
125
126 static GstFlowReturn gst_skin_detect_transform (GstOpencvVideoFilter * filter,
127 GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
128
129 static void gst_skin_detect_finalize (GObject * object);
130 static gboolean
131 gst_skin_detect_set_caps (GstOpencvVideoFilter * transform,
132 gint in_width, gint in_height, int in_cv_type,
133 gint out_width, gint out_height, int out_cv_type);
134
135 /* initialize the skindetect's class */
136 static void
gst_skin_detect_class_init(GstSkinDetectClass * klass)137 gst_skin_detect_class_init (GstSkinDetectClass * klass)
138 {
139 GObjectClass *gobject_class;
140 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
141 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
142
143 gobject_class = (GObjectClass *) klass;
144 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
145
146 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_skin_detect_finalize);
147 gobject_class->set_property = gst_skin_detect_set_property;
148 gobject_class->get_property = gst_skin_detect_get_property;
149
150 gstopencvbasefilter_class->cv_trans_func = gst_skin_detect_transform;
151
152 g_object_class_install_property (gobject_class, PROP_POSTPROCESS,
153 g_param_spec_boolean ("postprocess", "Postprocess",
154 "Apply opening-closing to skin detection to extract large, significant blobs ",
155 TRUE, (GParamFlags)
156 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
157 g_object_class_install_property (gobject_class, PROP_METHOD,
158 g_param_spec_enum ("method",
159 "Method to use",
160 "Method to use",
161 GST_TYPE_SKIN_DETECT_METHOD, HSV,
162 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
163
164 gst_element_class_set_static_metadata (element_class,
165 "skindetect",
166 "Filter/Effect/Video",
167 "Performs non-parametric skin detection on input",
168 "Miguel Casas-Sanchez <miguelecasassanchez@gmail.com>");
169
170 gst_element_class_add_static_pad_template (element_class, &src_factory);
171 gst_element_class_add_static_pad_template (element_class, &sink_factory);
172
173 gstopencvbasefilter_class->cv_set_caps = gst_skin_detect_set_caps;
174
175 gst_type_mark_as_plugin_api (GST_TYPE_SKIN_DETECT_METHOD, (GstPluginAPIFlags) 0);
176 }
177
178 /* initialize the new element
179 * instantiate pads and add them to element
180 * set pad callback functions
181 * initialize instance structure
182 */
183 static void
gst_skin_detect_init(GstSkinDetect * filter)184 gst_skin_detect_init (GstSkinDetect * filter)
185 {
186 filter->postprocess = TRUE;
187 filter->method = HSV;
188
189 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
190 FALSE);
191 }
192
193
194 static void
gst_skin_detect_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)195 gst_skin_detect_set_property (GObject * object, guint prop_id,
196 const GValue * value, GParamSpec * pspec)
197 {
198 GstSkinDetect *filter = GST_SKIN_DETECT (object);
199
200 switch (prop_id) {
201 case PROP_POSTPROCESS:
202 filter->postprocess = g_value_get_boolean (value);
203 break;
204 case PROP_METHOD:
205 filter->method = g_value_get_enum (value);
206 break;
207 default:
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 break;
210 }
211 }
212
213 static void
gst_skin_detect_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)214 gst_skin_detect_get_property (GObject * object, guint prop_id,
215 GValue * value, GParamSpec * pspec)
216 {
217 GstSkinDetect *filter = GST_SKIN_DETECT (object);
218
219 switch (prop_id) {
220 case PROP_POSTPROCESS:
221 g_value_set_boolean (value, filter->postprocess);
222 break;
223 case PROP_METHOD:
224 g_value_set_enum (value, filter->method);
225 break;
226 default:
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 break;
229 }
230 }
231
232 /* GstElement vmethod implementations */
233 /* this function handles the link with other elements */
234 static gboolean
gst_skin_detect_set_caps(GstOpencvVideoFilter * transform,gint in_width,gint in_height,int in_cv_type,gint out_width,gint out_height,int out_cv_type)235 gst_skin_detect_set_caps (GstOpencvVideoFilter * transform,
236 gint in_width, gint in_height, int in_cv_type,
237 gint out_width, gint out_height, int out_cv_type)
238 {
239 GstSkinDetect *filter = GST_SKIN_DETECT (transform);
240 cv::Size size = cv::Size (in_width, in_height);
241
242 filter->cvRGB.create (size, CV_8UC3);
243 filter->cvChA.create (size, CV_8UC1);
244 filter->width = in_width;
245 filter->height = in_height;
246
247 filter->cvHSV.create (size, CV_8UC3);
248 filter->cvH.create (size, CV_8UC1); /* Hue component. */
249 filter->cvH2.create (size, CV_8UC1); /* Hue component, 2nd threshold */
250 filter->cvS.create (size, CV_8UC1); /* Saturation component. */
251 filter->cvV.create (size, CV_8UC1); /* Brightness component. */
252 filter->cvSkinPixels1.create (size, CV_8UC1); /* Greyscale output image */
253
254 filter->cvR.create (size, CV_8UC1); /* R component. */
255 filter->cvG.create (size, CV_8UC1); /* G component. */
256 filter->cvB.create (size, CV_8UC1); /* B component. */
257 filter->cvAll.create (size, CV_32FC1); /* (R+G+B) component. */
258 filter->cvR2.create (size, CV_32FC1); /* R component, 32bits */
259 filter->cvRp.create (size, CV_32FC1); /* R' and >0.4 */
260 filter->cvGp.create (size, CV_32FC1); /* G' and > 0.28 */
261 filter->cvRp2.create (size, CV_32FC1); /* R' <0.6 */
262 filter->cvGp2.create (size, CV_32FC1); /* G' <0.4 */
263 filter->cvSkinPixels2.create (size, CV_32FC1); /* Greyscale output image. */
264 filter->cvdraft.create (size, CV_8UC1); /* Greyscale output image. */
265
266 return TRUE;
267 }
268
269 /* Clean up */
270 static void
gst_skin_detect_finalize(GObject * object)271 gst_skin_detect_finalize (GObject * object)
272 {
273 GstSkinDetect *filter = GST_SKIN_DETECT (object);
274
275 filter->cvRGB.release ();
276 filter->cvChA.release ();
277 filter->cvHSV.release ();
278 filter->cvH.release ();
279 filter->cvH2.release ();
280 filter->cvS.release ();
281 filter->cvV.release ();
282 filter->cvSkinPixels1.release ();
283 filter->cvR.release ();
284 filter->cvG.release ();
285 filter->cvB.release ();
286 filter->cvAll.release ();
287 filter->cvR2.release ();
288 filter->cvRp.release ();
289 filter->cvGp.release ();
290 filter->cvRp2.release ();
291 filter->cvGp2.release ();
292 filter->cvdraft.release ();
293 filter->cvSkinPixels2.release ();
294
295 G_OBJECT_CLASS (gst_skin_detect_parent_class)->finalize (object);
296 }
297
298 static GstFlowReturn
gst_skin_detect_transform(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img,GstBuffer * outbuf,cv::Mat outimg)299 gst_skin_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
300 cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
301 {
302 GstSkinDetect *filter = GST_SKIN_DETECT (base);
303
304 std::vector < cv::Mat > channels (3);
305 filter->cvRGB = cv::Mat (img);
306
307 /* SKIN COLOUR BLOB DETECTION */
308 if (HSV == filter->method) {
309 cv::cvtColor (filter->cvRGB, filter->cvHSV, cv::COLOR_RGB2HSV);
310 cv::split (filter->cvHSV, channels);
311 filter->cvH = channels.at (0);
312 filter->cvS = channels.at (1);
313 filter->cvV = channels.at (2);
314
315 /* Detect which pixels in each of the H, S and V channels are probably skin pixels.
316 Assume that skin has a Hue between 0 to 18 (out of 180), and Saturation above 50, and Brightness above 80. */
317 cv::threshold (filter->cvH, filter->cvH2, 10, UCHAR_MAX, cv::THRESH_BINARY); /* (hue > 10) */
318 cv::threshold (filter->cvH, filter->cvH, 20, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (hue < 20) */
319 cv::threshold (filter->cvS, filter->cvS, 48, UCHAR_MAX, cv::THRESH_BINARY); /* (sat > 48) */
320 cv::threshold (filter->cvV, filter->cvV, 80, UCHAR_MAX, cv::THRESH_BINARY); /* (val > 80) */
321
322 /* erode the HUE to get rid of noise. */
323 cv::erode (filter->cvH, filter->cvH, cv::Mat (), cv::Point (-1, -1), 1);
324
325 /* Combine all 3 thresholded color components, so that an output pixel will only
326 be white (255) if the H, S and V pixels were also white.
327 imageSkin = (hue > 10) ^ (hue < 20) ^ (sat > 48) ^ (val > 80), where ^ mean pixels-wise AND */
328 cv::bitwise_and (filter->cvH, filter->cvS, filter->cvSkinPixels1);
329 cv::bitwise_and (filter->cvSkinPixels1, filter->cvH2,
330 filter->cvSkinPixels1);
331 cv::bitwise_and (filter->cvSkinPixels1, filter->cvV, filter->cvSkinPixels1);
332
333 cv::cvtColor (filter->cvSkinPixels1, filter->cvRGB, cv::COLOR_GRAY2RGB);
334 } else if (RGB == filter->method) {
335 cv::split (filter->cvRGB, channels);
336 filter->cvR = channels.at (0);
337 filter->cvG = channels.at (1);
338 filter->cvB = channels.at (2);
339 cv::add (filter->cvR, filter->cvG, filter->cvAll);
340 cv::add (filter->cvB, filter->cvAll, filter->cvAll); /* All = R + G + B */
341 cv::divide (filter->cvR, filter->cvAll, filter->cvRp, 1.0, filter->cvRp.type ()); /* R' = R / ( R + G + B) */
342 cv::divide (filter->cvG, filter->cvAll, filter->cvGp, 1.0, filter->cvGp.type ()); /* G' = G / ( R + G + B) */
343
344 filter->cvR.convertTo (filter->cvR2, filter->cvR2.type (), 1.0, 0.0);
345 filter->cvGp.copyTo (filter->cvGp2);
346 filter->cvRp.copyTo (filter->cvRp2);
347
348 cv::threshold (filter->cvR2, filter->cvR2, 60, UCHAR_MAX, cv::THRESH_BINARY); /* (R > 60) */
349 cv::threshold (filter->cvRp, filter->cvRp, 0.42, UCHAR_MAX, cv::THRESH_BINARY); /* (R'> 0.4) */
350 cv::threshold (filter->cvRp2, filter->cvRp2, 0.6, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (R'< 0.6) */
351 cv::threshold (filter->cvGp, filter->cvGp, 0.28, UCHAR_MAX, cv::THRESH_BINARY); /* (G'> 0.28) */
352 cv::threshold (filter->cvGp2, filter->cvGp2, 0.4, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (G'< 0.4) */
353
354 /* Combine all 3 thresholded color components, so that an output pixel will only
355 be white (255) if the H, S and V pixels were also white. */
356
357 cv::bitwise_and (filter->cvR2, filter->cvRp, filter->cvSkinPixels2);
358 cv::bitwise_and (filter->cvRp, filter->cvSkinPixels2,
359 filter->cvSkinPixels2);
360 cv::bitwise_and (filter->cvRp2, filter->cvSkinPixels2,
361 filter->cvSkinPixels2);
362 cv::bitwise_and (filter->cvGp, filter->cvSkinPixels2,
363 filter->cvSkinPixels2);
364 cv::bitwise_and (filter->cvGp2, filter->cvSkinPixels2,
365 filter->cvSkinPixels2);
366
367 filter->cvSkinPixels2.convertTo (filter->cvdraft, filter->cvdraft.type (),
368 1.0, 0.0);
369 cv::cvtColor (filter->cvdraft, filter->cvRGB, cv::COLOR_GRAY2RGB);
370 }
371
372 /* After this we have a RGB Black and white image with the skin, in
373 filter->cvRGB. We can postprocess by applying 1 erode-dilate and 1
374 dilate-erode, or alternatively 1 opening-closing all together, with
375 the goal of removing small (spurious) skin spots and creating large
376 connected areas */
377 if (filter->postprocess) {
378 cv::split (filter->cvRGB, channels);
379 filter->cvChA = channels.at (0);
380
381 cv::Mat element =
382 cv::getStructuringElement (cv::MORPH_RECT, cv::Size (3, 3),
383 cv::Point (1, 1));
384 cv::erode (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 1);
385 cv::dilate (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 2);
386 cv::erode (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 1);
387
388 cv::cvtColor (filter->cvChA, filter->cvRGB, cv::COLOR_GRAY2RGB);
389 }
390
391 filter->cvRGB.copyTo (outimg);
392
393 return GST_FLOW_OK;
394 }
395