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 * <refsect2>
50 * <title>Example launch line</title>
51 * |[
52 * gst-launch-1.0 videotestsrc ! decodebin ! videoconvert ! skindetect ! videoconvert ! xvimagesink
53 * ]|
54 * </refsect2>
55 */
56
57 #ifdef HAVE_CONFIG_H
58 #include <config.h>
59 #endif
60
61 #include "gstskindetect.h"
62 #include <opencv2/imgproc.hpp>
63
64 GST_DEBUG_CATEGORY_STATIC (gst_skin_detect_debug);
65 #define GST_CAT_DEFAULT gst_skin_detect_debug
66
67 /* Filter signals and args */
68 enum
69 {
70 /* FILL ME */
71 LAST_SIGNAL
72 };
73
74 enum
75 {
76 PROP_0,
77 PROP_POSTPROCESS,
78 PROP_METHOD,
79 PROP_MASK
80 };
81 typedef enum
82 {
83 HSV,
84 RGB
85 } GstSkindetectMethod;
86
87 #define GST_TYPE_SKIN_DETECT_METHOD (gst_skin_detect_method_get_type ())
88 static GType
gst_skin_detect_method_get_type(void)89 gst_skin_detect_method_get_type (void)
90 {
91 static GType etype = 0;
92 if (etype == 0) {
93 static const GEnumValue values[] = {
94 {HSV, "Classic HSV thresholding", "hsv"},
95 {RGB, "Normalised-RGB colorspace thresholding", "rgb"},
96 {0, NULL, NULL},
97 };
98 etype = g_enum_register_static ("GstSkindetectMethod", values);
99 }
100 return etype;
101 }
102
103 G_DEFINE_TYPE (GstSkinDetect, gst_skin_detect, GST_TYPE_OPENCV_VIDEO_FILTER);
104 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
105 GST_PAD_SINK,
106 GST_PAD_ALWAYS,
107 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")));
108
109 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
110 GST_PAD_SRC,
111 GST_PAD_ALWAYS,
112 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")));
113
114
115 static void gst_skin_detect_set_property (GObject * object, guint prop_id,
116 const GValue * value, GParamSpec * pspec);
117 static void gst_skin_detect_get_property (GObject * object, guint prop_id,
118 GValue * value, GParamSpec * pspec);
119
120 static GstFlowReturn gst_skin_detect_transform (GstOpencvVideoFilter * filter,
121 GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
122
123 static void gst_skin_detect_finalize (GObject * object);
124 static gboolean
125 gst_skin_detect_set_caps (GstOpencvVideoFilter * transform,
126 gint in_width, gint in_height, int in_cv_type,
127 gint out_width, gint out_height, int out_cv_type);
128
129 /* initialize the skindetect's class */
130 static void
gst_skin_detect_class_init(GstSkinDetectClass * klass)131 gst_skin_detect_class_init (GstSkinDetectClass * klass)
132 {
133 GObjectClass *gobject_class;
134 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
135 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
136
137 gobject_class = (GObjectClass *) klass;
138 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
139
140 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_skin_detect_finalize);
141 gobject_class->set_property = gst_skin_detect_set_property;
142 gobject_class->get_property = gst_skin_detect_get_property;
143
144 gstopencvbasefilter_class->cv_trans_func = gst_skin_detect_transform;
145
146 g_object_class_install_property (gobject_class, PROP_POSTPROCESS,
147 g_param_spec_boolean ("postprocess", "Postprocess",
148 "Apply opening-closing to skin detection to extract large, significant blobs ",
149 TRUE, (GParamFlags)
150 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
151 g_object_class_install_property (gobject_class, PROP_METHOD,
152 g_param_spec_enum ("method",
153 "Method to use",
154 "Method to use",
155 GST_TYPE_SKIN_DETECT_METHOD, HSV,
156 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
157
158 gst_element_class_set_static_metadata (element_class,
159 "skindetect",
160 "Filter/Effect/Video",
161 "Performs non-parametric skin detection on input",
162 "Miguel Casas-Sanchez <miguelecasassanchez@gmail.com>");
163
164 gst_element_class_add_static_pad_template (element_class, &src_factory);
165 gst_element_class_add_static_pad_template (element_class, &sink_factory);
166
167 gstopencvbasefilter_class->cv_set_caps = gst_skin_detect_set_caps;
168 }
169
170 /* initialize the new element
171 * instantiate pads and add them to element
172 * set pad calback functions
173 * initialize instance structure
174 */
175 static void
gst_skin_detect_init(GstSkinDetect * filter)176 gst_skin_detect_init (GstSkinDetect * filter)
177 {
178 filter->postprocess = TRUE;
179 filter->method = HSV;
180
181 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
182 FALSE);
183 }
184
185
186 static void
gst_skin_detect_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)187 gst_skin_detect_set_property (GObject * object, guint prop_id,
188 const GValue * value, GParamSpec * pspec)
189 {
190 GstSkinDetect *filter = GST_SKIN_DETECT (object);
191
192 switch (prop_id) {
193 case PROP_POSTPROCESS:
194 filter->postprocess = g_value_get_boolean (value);
195 break;
196 case PROP_METHOD:
197 filter->method = g_value_get_enum (value);
198 break;
199 default:
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 break;
202 }
203 }
204
205 static void
gst_skin_detect_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)206 gst_skin_detect_get_property (GObject * object, guint prop_id,
207 GValue * value, GParamSpec * pspec)
208 {
209 GstSkinDetect *filter = GST_SKIN_DETECT (object);
210
211 switch (prop_id) {
212 case PROP_POSTPROCESS:
213 g_value_set_boolean (value, filter->postprocess);
214 break;
215 case PROP_METHOD:
216 g_value_set_enum (value, filter->method);
217 break;
218 default:
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 break;
221 }
222 }
223
224 /* GstElement vmethod implementations */
225 /* this function handles the link with other elements */
226 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)227 gst_skin_detect_set_caps (GstOpencvVideoFilter * transform,
228 gint in_width, gint in_height, int in_cv_type,
229 gint out_width, gint out_height, int out_cv_type)
230 {
231 GstSkinDetect *filter = GST_SKIN_DETECT (transform);
232 cv::Size size = cv::Size (in_width, in_height);
233
234 filter->cvRGB.create (size, CV_8UC3);
235 filter->cvChA.create (size, CV_8UC1);
236 filter->width = in_width;
237 filter->height = in_height;
238
239 filter->cvHSV.create (size, CV_8UC3);
240 filter->cvH.create (size, CV_8UC1); /* Hue component. */
241 filter->cvH2.create (size, CV_8UC1); /* Hue component, 2nd threshold */
242 filter->cvS.create (size, CV_8UC1); /* Saturation component. */
243 filter->cvV.create (size, CV_8UC1); /* Brightness component. */
244 filter->cvSkinPixels1.create (size, CV_8UC1); /* Greyscale output image */
245
246 filter->cvR.create (size, CV_8UC1); /* R component. */
247 filter->cvG.create (size, CV_8UC1); /* G component. */
248 filter->cvB.create (size, CV_8UC1); /* B component. */
249 filter->cvAll.create (size, CV_32FC1); /* (R+G+B) component. */
250 filter->cvR2.create (size, CV_32FC1); /* R component, 32bits */
251 filter->cvRp.create (size, CV_32FC1); /* R' and >0.4 */
252 filter->cvGp.create (size, CV_32FC1); /* G' and > 0.28 */
253 filter->cvRp2.create (size, CV_32FC1); /* R' <0.6 */
254 filter->cvGp2.create (size, CV_32FC1); /* G' <0.4 */
255 filter->cvSkinPixels2.create (size, CV_32FC1); /* Greyscale output image. */
256 filter->cvdraft.create (size, CV_8UC1); /* Greyscale output image. */
257
258 return TRUE;
259 }
260
261 /* Clean up */
262 static void
gst_skin_detect_finalize(GObject * object)263 gst_skin_detect_finalize (GObject * object)
264 {
265 GstSkinDetect *filter = GST_SKIN_DETECT (object);
266
267 filter->cvRGB.release ();
268 filter->cvChA.release ();
269 filter->cvHSV.release ();
270 filter->cvH.release ();
271 filter->cvH2.release ();
272 filter->cvS.release ();
273 filter->cvV.release ();
274 filter->cvSkinPixels1.release ();
275 filter->cvR.release ();
276 filter->cvG.release ();
277 filter->cvB.release ();
278 filter->cvAll.release ();
279 filter->cvR2.release ();
280 filter->cvRp.release ();
281 filter->cvGp.release ();
282 filter->cvRp2.release ();
283 filter->cvGp2.release ();
284 filter->cvdraft.release ();
285 filter->cvSkinPixels2.release ();
286
287 G_OBJECT_CLASS (gst_skin_detect_parent_class)->finalize (object);
288 }
289
290 static GstFlowReturn
gst_skin_detect_transform(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img,GstBuffer * outbuf,cv::Mat outimg)291 gst_skin_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
292 cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
293 {
294 GstSkinDetect *filter = GST_SKIN_DETECT (base);
295
296 std::vector < cv::Mat > channels (3);
297 filter->cvRGB = cv::Mat (img);
298
299 /* SKIN COLOUR BLOB DETECTION */
300 if (HSV == filter->method) {
301 cv::cvtColor (filter->cvRGB, filter->cvHSV, cv::COLOR_RGB2HSV);
302 cv::split (filter->cvHSV, channels);
303 filter->cvH = channels.at (0);
304 filter->cvS = channels.at (1);
305 filter->cvV = channels.at (2);
306
307 /* Detect which pixels in each of the H, S and V channels are probably skin pixels.
308 Assume that skin has a Hue between 0 to 18 (out of 180), and Saturation above 50, and Brightness above 80. */
309 cv::threshold (filter->cvH, filter->cvH2, 10, UCHAR_MAX, cv::THRESH_BINARY); /* (hue > 10) */
310 cv::threshold (filter->cvH, filter->cvH, 20, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (hue < 20) */
311 cv::threshold (filter->cvS, filter->cvS, 48, UCHAR_MAX, cv::THRESH_BINARY); /* (sat > 48) */
312 cv::threshold (filter->cvV, filter->cvV, 80, UCHAR_MAX, cv::THRESH_BINARY); /* (val > 80) */
313
314 /* erode the HUE to get rid of noise. */
315 cv::erode (filter->cvH, filter->cvH, cv::Mat (), cv::Point (-1, -1), 1);
316
317 /* Combine all 3 thresholded color components, so that an output pixel will only
318 be white (255) if the H, S and V pixels were also white.
319 imageSkin = (hue > 10) ^ (hue < 20) ^ (sat > 48) ^ (val > 80), where ^ mean pixels-wise AND */
320 cv::bitwise_and (filter->cvH, filter->cvS, filter->cvSkinPixels1);
321 cv::bitwise_and (filter->cvSkinPixels1, filter->cvH2,
322 filter->cvSkinPixels1);
323 cv::bitwise_and (filter->cvSkinPixels1, filter->cvV, filter->cvSkinPixels1);
324
325 cv::cvtColor (filter->cvSkinPixels1, filter->cvRGB, cv::COLOR_GRAY2RGB);
326 } else if (RGB == filter->method) {
327 cv::split (filter->cvRGB, channels);
328 filter->cvR = channels.at (0);
329 filter->cvG = channels.at (1);
330 filter->cvB = channels.at (2);
331 cv::add (filter->cvR, filter->cvG, filter->cvAll);
332 cv::add (filter->cvB, filter->cvAll, filter->cvAll); /* All = R + G + B */
333 cv::divide (filter->cvR, filter->cvAll, filter->cvRp, 1.0, filter->cvRp.type ()); /* R' = R / ( R + G + B) */
334 cv::divide (filter->cvG, filter->cvAll, filter->cvGp, 1.0, filter->cvGp.type ()); /* G' = G / ( R + G + B) */
335
336 filter->cvR.convertTo (filter->cvR2, filter->cvR2.type (), 1.0, 0.0);
337 filter->cvGp.copyTo (filter->cvGp2);
338 filter->cvRp.copyTo (filter->cvRp2);
339
340 cv::threshold (filter->cvR2, filter->cvR2, 60, UCHAR_MAX, cv::THRESH_BINARY); /* (R > 60) */
341 cv::threshold (filter->cvRp, filter->cvRp, 0.42, UCHAR_MAX, cv::THRESH_BINARY); /* (R'> 0.4) */
342 cv::threshold (filter->cvRp2, filter->cvRp2, 0.6, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (R'< 0.6) */
343 cv::threshold (filter->cvGp, filter->cvGp, 0.28, UCHAR_MAX, cv::THRESH_BINARY); /* (G'> 0.28) */
344 cv::threshold (filter->cvGp2, filter->cvGp2, 0.4, UCHAR_MAX, cv::THRESH_BINARY_INV); /* (G'< 0.4) */
345
346 /* Combine all 3 thresholded color components, so that an output pixel will only
347 be white (255) if the H, S and V pixels were also white. */
348
349 cv::bitwise_and (filter->cvR2, filter->cvRp, filter->cvSkinPixels2);
350 cv::bitwise_and (filter->cvRp, filter->cvSkinPixels2,
351 filter->cvSkinPixels2);
352 cv::bitwise_and (filter->cvRp2, filter->cvSkinPixels2,
353 filter->cvSkinPixels2);
354 cv::bitwise_and (filter->cvGp, filter->cvSkinPixels2,
355 filter->cvSkinPixels2);
356 cv::bitwise_and (filter->cvGp2, filter->cvSkinPixels2,
357 filter->cvSkinPixels2);
358
359 filter->cvSkinPixels2.convertTo (filter->cvdraft, filter->cvdraft.type (),
360 1.0, 0.0);
361 cv::cvtColor (filter->cvdraft, filter->cvRGB, cv::COLOR_GRAY2RGB);
362 }
363
364 /* After this we have a RGB Black and white image with the skin, in
365 filter->cvRGB. We can postprocess by applying 1 erode-dilate and 1
366 dilate-erode, or alternatively 1 opening-closing all together, with
367 the goal of removing small (spurious) skin spots and creating large
368 connected areas */
369 if (filter->postprocess) {
370 cv::split (filter->cvRGB, channels);
371 filter->cvChA = channels.at (0);
372
373 cv::Mat element =
374 cv::getStructuringElement (cv::MORPH_RECT, cv::Size (3, 3),
375 cv::Point (1, 1));
376 cv::erode (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 1);
377 cv::dilate (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 2);
378 cv::erode (filter->cvChA, filter->cvChA, element, cv::Point (1, 1), 1);
379
380 cv::cvtColor (filter->cvChA, filter->cvRGB, cv::COLOR_GRAY2RGB);
381 }
382
383 filter->cvRGB.copyTo (outimg);
384
385 return GST_FLOW_OK;
386 }
387
388 /* entry point to initialize the plug-in
389 * initialize the plug-in itself
390 * register the element factories and other features
391 */
392 gboolean
gst_skin_detect_plugin_init(GstPlugin * plugin)393 gst_skin_detect_plugin_init (GstPlugin * plugin)
394 {
395 /* debug category for fltering log messages
396 *
397 */
398 GST_DEBUG_CATEGORY_INIT (gst_skin_detect_debug, "skindetect",
399 0, "Performs skin detection on videos and images");
400
401 return gst_element_register (plugin, "skindetect", GST_RANK_NONE,
402 GST_TYPE_SKIN_DETECT);
403 }
404