So last night I went out for a few drinks with my colleague, James, a fellow computer vision researcher who I have known for years.
You see, James likes to party. He’s a “go hard” type of guy. He wanted to hit every single bar on Washington St. (there’s a lot of them)…and then hit ’em again on the way back.
It’s safe to say that by the end of the night that James was blotto. Wasted. Lights out.
Anyway, it was getting late. 1:47am, to be exact. I’m tired. I just want to go to bed. We’re at the last bar. I’m signing my name on the tab and heading for the door, looking forward to getting some sleep.
And James, in his clearly drunken and intoxicated state, walks (or rather, stumbles like a newborn foal) up to this blonde who looks and dresses like she’s straight out of South Beach and says: “You know…if you take your shirt off I have a Python script that can detect how much skin you’re showing. Wanna see?”
Now tell me, what do you think happens next?
That’s right. James gets a top shelf Manhattan thrown in his face by the South Beach girl. Honestly, he’s just lucky that he didn’t catch a well deserved fist to the nose.
Shocked, and fairly appalled, I led the drunken James back to my apartment to sleep it off. Not to mention have a conversation with him regarding how to treat women the next morning.
So here’s the deal…
In this blog post I’m going to show you how to detect skin in images using computer vision.
But use these superpowers for good, okay? Don’t be like James. Play it cool.
Read on and find out how simple it really is to detect skin in images using Python and OpenCV.
Looking for the source code to this post?
Jump Right To The Downloads SectionOpenCV and Python versions:
This example will run on Python 2.7/Python 3.4+ and OpenCV 2.4.X/OpenCV 3.0+.
Detecting Skin in Images & Video Using Python and OpenCV
You know the drill. Open up your favorite editor, create a new file, name it skindetector.py
, and let’s get to work:
# import the necessary packages from pyimagesearch import imutils import numpy as np import argparse import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help = "path to the (optional) video file") args = vars(ap.parse_args()) # define the upper and lower boundaries of the HSV pixel # intensities to be considered 'skin' lower = np.array([0, 48, 80], dtype = "uint8") upper = np.array([20, 255, 255], dtype = "uint8")
On Lines 2-5 we import the packages that we’ll need. We’ll use NumPy for some numerical processing, argparse
to parse our command line arguments, and cv2
for our OpenCV bindings.
We’ll also use a package called imutils
which contains a bunch of “convenience” image processing functions for resizing, rotating, etc. You can read more about the imutils
package in my Basics of Image Manipulations post.
From there, Lines 8-11 parse our command line arguments. We have only a single (optional) switch, --video
, which allows you to detect skin in a pre-supplied video. However, you could just as easily omit this switch from the command and use your webcam to detect skin in images. If you don’t have a webcam attached to your system, then you’ll have to supply a video.
From there, we define the lower and upper boundaries for pixel intensities to be considered skin on Lines 15 and 16. It’s important to note that these boundaries are for the HSV color space, NOT the RGB color space.
If you haven’t had a chance to take a look at my post on Color Detection Using Python and OpenCV, then now would be a good time to do so — we’ll be building off the fundamentals presented in the color detection post and extending our method to detect skin in images.
Alright, now let’s write some code to grab a reference to the webcam or video:
# if a video path was not supplied, grab the reference # to the gray if not args.get("video", False): camera = cv2.VideoCapture(0) # otherwise, load the video else: camera = cv2.VideoCapture(args["video"])
On Line 20 we make a check to see if the --video
switch was supplied. If it was not, then we grab reference to the webcam by passing a value of 0 to the cv2.VideoCapture
function.
However, if the --video
switch was supplied, then we’ll pass the path to the video to cv2.VideoCapture
on Lines 24 and 25.
Let’s start reading frames from our video:
# keep looping over the frames in the video while True: # grab the current frame (grabbed, frame) = camera.read() # if we are viewing a video and we did not grab a # frame, then we have reached the end of the video if args.get("video") and not grabbed: break # resize the frame, convert it to the HSV color space, # and determine the HSV pixel intensities that fall into # the speicifed upper and lower boundaries frame = imutils.resize(frame, width = 400) converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) skinMask = cv2.inRange(converted, lower, upper) # apply a series of erosions and dilations to the mask # using an elliptical kernel kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) skinMask = cv2.erode(skinMask, kernel, iterations = 2) skinMask = cv2.dilate(skinMask, kernel, iterations = 2) # blur the mask to help remove noise, then apply the # mask to the frame skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0) skin = cv2.bitwise_and(frame, frame, mask = skinMask) # show the skin in the image along with the mask cv2.imshow("images", np.hstack([frame, skin])) # if the 'q' key is pressed, stop the loop if cv2.waitKey(1) & 0xFF == ord("q"): break # cleanup the camera and close any open windows camera.release() cv2.destroyAllWindows()
We start looping over the frames in the video on Line 28 and grab the next frame on Line 30 using the camera.read()
method.
The camera.read()
function returns a tuple, consisting of grabbed
and frame
. The grabbed
variable is simply a boolean flag, indicating if the frame was successfully read or not. The frame
is the frame itself.
From there, we make a check on Line 34 to see if the frame
was not read. If it was not, and we are reading a video from file, we assume we have reached the end of the video and we can safely break from the loop. Otherwise, we keep on looping.
In order to speed up the skin detection process, we use our imutils.resize
convenience function to resize our frame to have a width of 400 pixels on Line 40.
The actual skin detection takes place on Line 41 and 42. First, we convert the image from the RGB color space to the HSV color space. Then, we apply the cv2.inRange
function, supplying our HSV frame, and our lower and upper boundaries as arguments, respectively.
The output of the cv2.inRange
function is our mask This mask is a single channel image, has the same width and height as the frame, and is of the 8-bit unsigned integer data type.
Pixels that are white (255) in the mask represent areas of the frame that are skin. Pixels that are black (0) in the mask represent areas that are not skin.
However, we may detect many small false-positive skin regions in the image. To remove these small regions, take a look at Lines 46-48. First, we create an elliptical structuring kernel. Then, we use this kernel to perform two iterations of erosions and dilations, respectively. These erosions and dilations will help remove the small false-positive skin regions in the image.
From there, we smooth the mask slightly using a Gaussian blur on Line 52. This smoothing step, while not critical to the skin detection process, produces a much cleaner mask.
We then apply the skin mask to our frame on Line 53.
Line 56 shows us a side-by-side view of the original frame along with the frame with skin detected in it.
We wait for a keypress on Line 59, and if it’s the q
key, then we break from the loop.
Finally, Lines 63 and 64 release the reference to our webcam/video and close any open windows.
Running our Skin Detector
To run our skin detector, open up a terminal and navigate to where our source code is stored.
If you are using the example video provided with the code downloads for this post (or an example video of your own), then issue the following command:
$ python skindetector.py --video video/skin_example.mov
Otherwise, if you want to use your own webcam, execute this command:
$ python skindetector.py
If all goes well, you should see my detected skin, as shown by the following figure:
And here is another example image from the video supplied with the code download:
Pretty cool, right?
Using this basic approach we were able to build a fairly rudimentary skin detection algorithm — that’s still able to obtain decent results!
Limitations
There are some pretty obvious limitations and drawbacks to this approach.
The main drawback is that we are framing skin detection as a “color detection” problem. This assumes that we can easily specify the HSV values for ranges of pixel intensities that are considered skin.
However, under different lighting conditions, this approach might not perform as well — and we would likely have to continue to tweak the HSV value ranges.
The HSV value ranges we supplied worked fairly well for this example post…but what about for other ethnicities?
For example, I’m a white male. And surely the same HSV values used to detect my skin could not be used to detect someone from Asia or Africa. This implies that we have some a priori knowledge regarding the skin tone of who we want to detect.
Of course, more robust approaches can be applied. A (highly simplified) example would be to perform face detection to an image, determine the color of the skin on their face, and then use that model to detect the rest of the skin on their body.
Not a bad approach, but as you can imagine, it’s definitely a little more complicated.
In the meantime, as long as you can specify the HSV values of the skin tone you want to detect, framing skin detection as a color detection problem should work quite well for you.
What's next? We recommend PyImageSearch University.
86 total classes • 115+ hours of on-demand code walkthrough videos • Last updated: October 2024
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
In this blog post I showed you how to detect skin in images using Python and OpenCV.
To accomplish our skin detection, we framed skin detection as an extension to color detection.
We were able to supply upper and lower ranges of pixel intensities in the HSV color space to detect skin in images.
While this is not a perfect or robust approach, the simplicity of our skin detection algorithm makes it a very good starting point to build more robust solutions.
But remember our promise, okay?
Don’t be like James. Use our skin detection superpowers for good, not evil.
Download the Source Code and FREE 17-page Resource Guide
Enter your email address below to get a .zip of the code and a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!
Wajih Ullah Baig
Bought your book yesterday.
It is one wonderful effort. Top of my list of OpenCV books!
Adrian Rosebrock
Awesome! I’m happy that you are enjoying it!
Orion Gump
Hi, I tried to compile the file but it was reported that
Traceback (most recent call last):
File “skindetector.py”, line 2, in
from pyimagesearch import imutils
ImportError: No module named pyimagesearch
May I know where I can download the pyimagesearch lib?
Adrian Rosebrock
Hi Orion, did you download the source code from the bottom of the page? Be sure to click the “Download the Code!” button at the bottom of this page and the ‘pyimagesearch’ module will be in the .zip file. Alternatively, shoot me an email and I’ll be happy to pass it along to you.
gol
I downloaded .zip file. but i cant import pyimagesearch.facedetector. I am using windows 8. python27. thanx
Adrian Rosebrock
Hey Gol, I’m not sure I understand your question — there is no
facedetector
module inside the skin detection .zip. If you are using code from the Case Studies book, then you’ll need to copy thefacedetector
into thepyimagesearch
directory of the skin detection example.
Suraj
As you told here that this works only for certain skin tones ,Are we supposed to write various test cases for different skin tones are is there any package available for it ??
Adrian Rosebrock
Hi Suraj, yes, that is correct. You will have to write your own test cases. Definitely consider using the HSV or L*a*b* color space for this.
SaadEddin
I did a couple of your tutorials the last few days… So I Bought your book today, I am looking forward to starting my journey.
I do have some experience with OpenCV, numpy, scipy, scikit image and scikit learn. I used them in a couple of projects and homeworks so I learned using some tutorials from here and there, so the learning process wasn’t structures.
So I decided to start with your book as I prefer a structured learning process. Especially at the beginning, as it will consolidate the skills much better I hope.
Thanks.
Adrian Rosebrock
Hi Saad. Awesome, thanks for picking up a copy of Practical Python and OpenCV! I’m sure you’ll learn a ton from the book!
Boris Tsipenyuk
this was a great proof of concept, what I found hilarious in my limited testing is that generally it detected skin (and hair) at least for blondes 🙂 so the bald sample set may be flawed a bit 🙂
Joe Landau
Using a Raspberry Pi with the Pi’s camera, line 21 does not work for me. It is said on the net that you need to install a V4L2 driver. (https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=62364)
Also, the comment on lines 18 and 19 mystifies me.
Adrian Rosebrock
Anytime you see a call to
cv2.VideoCapture
and you want to use the code with the Raspberry Pi, you’ll need to convert it to use thepicamera
module, like in this post or use the V4L2 driver. I really recommend converting the code. It’s much easier than getting the driver working.As for the comment, that’s simply a typo. If a video is not supplied, then we will grab the webcam reference.
Patrick Nowicki
I managed to get the Raspberry Pi camera working on my Pi 2 for this example, but the frame rate is only about 5 frames/sec on my output and the video is delayed about 1-2 seconds. Is this expected because of the processing power of the Pi? Just curious if there is anyway to make it smoother/faster.
Thanks!
Adrian Rosebrock
Very nice, congrats on getting your Pi 2 working for this email! 5 FPS sounds a tad low for this example though, even given the limited processing power of the Pi. I would suggest trying to resize the image even further, perhaps having a maximum width of 200-300 pixels.
Patrick Nowicki
Thanks for the super quick reply! A resize to 200 did make a huge difference. Thanks for the tip!
Christian Abbott
For anybody else running this on a Pi: Erosion and Dilation are, by far, the two biggest bottlenecks for your execution speed here. As an alternative, try performing Opening instead:
skinMask = cv2.morphologyEx(skinMask, cv2.MORPH_OPEN, kernel) as opposed to your two calls to Dilate() and Erode().
Adrian Rosebrock
Hey Christian — do you have any timings to confirm this?
Christian Abbott
Hi,
Here’s the output for 30 frames of execution, Erode & Dilate vs. Opening:
With erode & dilate: http://pastebin.ca/3428588
With opening: http://pastebin.ca/3428593
Pardon the awful cProfile formatting. Individual trial info is at the bottom.
Adrian Rosebrock
Awesome, thanks for sharing! I’ll also keep this in mind for future posts as well.
cm
Hi Adrian,
I have “import error” problem on pi for importing “pyimagesearch”.
Here is the message:
ImportError: No module named 'pyimagesearch'
I know that my system lacks of this package, however I don’t know how to
install it on the pi system.
Could you teach me how to do this?
Thanks
– CM
Adrian Rosebrock
The
pyimagesearch
package is included in the source code download of this post. Just download the .zip file, unzip it, and run the Python script. There is nothing to install.Sharon
Hi Adrian,
are there plans to make pyimagesearch a python package as you did imutils?
Adrian Rosebrock
No, the reason being is that what the “pyimagesearch” module contains is 100% dependent on the blog post itself. I use the “pyimagesearch” module to reach readers how to properly organize their Python packages. Each post could have a different “pyimagesearch” package.
thom
Hi when i use skin detection from your video ewerything works.
But when i try from my camera (cv)pi@raspberrypi:~ $ python skindetector.py
i got this eror
I downloaded from botom code.
Adrian Rosebrock
Based on your terminal, I assume you are using the Raspberry Pi. Are you using a USB webcam or the Pi camera module? I’m assuming the Pi camera module. In that case, you’ll need to update the code to access the Raspberry Pi camera rather than use the
cv2.VideoCapture
function.Gabriel
Hello, great tutorials! Quick question about the color: In the code you include the line:
# define the upper and lower boundaries of the HSV pixel
# intensities to be considered ‘skin’
upper = np.array([20, 255, 255], dtype = “uint8”)
If the values [20, 255, 255] are HSV, this is the same than [20, 100, 100], right? (in HSV you can’t go beyond 100%).
Thanks!
Adrian Rosebrock
Under the pure definition of the HSV color space, yes, that’s correct. However, OpenCV’s
cv2.cvtColor
function allows the Hue to be in the range [0, 180] and both the saturation to be in the range [0, 255].Niki
Hi Adrian,
Thank you for this wonderful blog. I only started looking at the a few posts, but so far I’ve learned a lot more than the last 2 weeks that I was randomly looking at different sources.
I have a question regarding this post. I have a video of hand washing and I want to detect the act of hand washing. One step is to detect the hands. Unfortunately, when I try your code on my videos, it cannot detect the hands at all. Instead in some of the videos, parts of the wall, that have a light pink color, are detected, and in some other videos nothing is detected at all.
My question is does this code work well when there is fast movement of hands like in the case of hand washing?
Another thing that I should mention is that the sink area has also a light color; can this cause a problem as well?
Do you think this algorithm still works for me by performing some changes or I should try something else?
Your help is greatly appreciated!
Adrian Rosebrock
This method of skin detection relies on color thresholding, which can work well in some situations — and fail in others. It mainly depends on lighting conditions and the color of your own skin. You might need to tweak the values to
cv2.inRange
to obtain a better segmentation of the skin.That said, I don’t think this method is best for your particular application. Presuming your camera is fixed and non-moving, you can likely use simple motion detection to detect the hands as they go under the water stream.
Tim Clemans
I’m struggling to learn how to specify lower and upper boundaries. How can I generate the lower and upper boundaries? Is there a free computer program that will generate a rang for me?
Adrian Rosebrock
Please see this blog post where I mention the
range-detector
script that can be used to help define the upper and lower boundaries in a given color space.Oscar
Hi Adrian,
first of all, thanks for the tutorial.
I am using the Raspberry Pi and the Pi camera module. in other reply, you said it´s necessary to update the code to access the Raspberry Pi camera rather than use the cv2.VideoCapture function, but what code I have to add or change in skindetector.py?
Thanks in advance.
Adrian Rosebrock
You’ll need to change the camera initialization and a function call that actually reads a frame from the video stream. I suggest using the VideoStream class which can access both a builtin/USB webcam and the Raspberry Pi camera module with minimal changes.
Vinith
Hi
I keep getting the error numpy module not found and cv2 module too. I downloaded both files online but problem still persists.
Adrian Rosebrock
Have you installed OpenCV? Did you install using a tutorial on PyImageSearch? If so, you’re probably forgetting to use the
workon cv
command prior to executing your Python script.Alf
Dear Mr. Rosebrock,
Such a Great Job!
But is it possible to achieve similar result by usage of Bayesian pixel-based skin segmentation?
How could I do this?
Regards,
Alf.
Adrian Rosebrock
There are many different ways to perform segmentation, and yes, Bayesian methods can be applied for this. However, I do not have any tutorials on this particular method — I can’t promise I’ll cover it in the future, but I can certainly look into it.
Alf
Thank you for quick response!
Deger
Hello Adrian,
Thanks for the tutorial. I have 2 questions for you.
1- Are you using Hubspot? Because you have amazing and excellent marketing jobs.
2- I have sent you an e-mail about buying your book but maybe for a specific tutorial (object recognition and feature detection)
I am looking forward to hear from you.
Thank you.
Adrian Rosebrock
Hey Deger — I’m actually not using HubSpot. Do you think I should be? I’ve also responded to your email, be sure to give my response a look.
DantePy
Hi Adrian,
I’m pretty new in cv, anyway I’m very excited of such field, thanks a lot for your wonderful blog.
I have a question about the code showed above, I know that dilate is the complementary operation of erode, so what is the meaning of those two lines of code? Why they don’t elude each other?
Adrian Rosebrock
Performing an erosion will remove small speckles of noise from your binary map. But then you apply a dilation to grow back the regions of actual objects you’re interested in. In essence, an erosion followed by a dilation will remove noise from your images.
muhammad iqbal
Is there any other way to detect hand.like other than color problem.
Adrian Rosebrock
Certainly, there are many methods that can be used to detect hands in images. Some methods rely on machine learning and treat the problem as an object detection task (like Haar cascades, HOG + Linear SVM detectors, or CNN localizers). Other methods use hardware to facilitate hand detection such as stereo vision and 3D cameras — it really just depends on the actual problem and the setup.
Shilpa
Hi Adrian,
Can You please let me know how can one find the nails from human finger.Any help wold be appreciated.
Thanks!
Adrian Rosebrock
I don’t have any experience with fingernail detection but I wouldn’t rely on color thresholding due to varying skin tones. Have you considered training a custom object detector? With a fixed camera this should be fairly straightforward.
Parth Shah
Hi Adrian,
I was wondering is this face detection possible with help of neural-networks do you any books ot tutorials for that like your Practical Python and OpenCV ?
If there are any would really appreciate if you could mail me some links or blogs for some reference..
Thank you!
Adrian Rosebrock
You can certainly build a face detector using neural networks; however, most current methods that you see in OpenCV or other computer vision libraries rely on either Haar cascades (covered in Practical Python and OpenCV) or HOG + Linear SVM detectors (covered in the PyImageSearch Gurus course).
I’ll be announcing a new book soon which will cover neural networks and deep learning so be sure to be on the lookout for it!
Parth Shah
Thank you for replying on short notice!
But is there any other reference you have anything that can get me a jump-start for Neural Networks, with a level of your explanation xD ?
Also, I tried using your range-detector script mentioned in this blog, I was using webcam, 2 windows pop for output one is original and other is thres..
But thres is always blank can you tell me exactly how to use it..
I am kinda new to all this but suprisingly ComputerVision is quite interesting!
Thank you
Adrian Rosebrock
The PyImageSearch Gurus course covers NNs, CNNs, and Deep Learning with my level of explanation. I’m also currently writing a book on deep learning + image classification.
myheadhurts
Hi Adrian- when I downloaded skindetection.zip, it landed in downloads. I unzipped it there, but I’m pretty sure I needed to extract it to some other spot, because the program can’t find the ‘pyimagesearch’ module. Could not locate this problem in the other replies, so I’m bugging you for some direction. Where do I extract to?
Thanks!
Adrian Rosebrock
Change directory into the
skindetection
directory and then execute the Python script from there. You need to be in the same directory as thepyimagesearch
module for it to import.vivek
hello sir
how do you define the range for lower and upper boundaries for pixel intensities to be considered skin on?
Adrian Rosebrock
You can do it one of two ways:
1. Read various publications on skin detection and see which ranges/methods the authors recommend.
2. Use scripts such as range-detector to manually determine the color thresholds.
alice
Hey Adiran,
wonderful work thank very much fo ur blogs wihch help me a lot to understand how to use opencv and python
but I just have a question about this line of the programm
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
why did u use 11 ? and for the iteration (the line after ) why did u use 2
thanks
ps : ( i m new on CV and PYTHON )
Adrian Rosebrock
The line you are referring to defines a circular kernel of size 11×11 pixels. This kernel is used to erode (remove) connected foreground pixels (to disconnect connected components) and then dilate (re-grow) the regions. If you’re new to OpenCV and image processing I would highly encourage you to read through Practical Python and OpenCV and/or the PyImageSearch Gurus course where I discuss these algorithms and techniques in more detail.
alicia
Hello ! need help really sorry im trying to record a video on raspberry and it s just giving the first image of the video durinf 20 secondes Can u help me ?
Adrian Rosebrock
It’s hard to say what the specific problem is here, but I would suggest making sure you can read the frames from your camera sensor before writing them to disk. You might also have a stray
cv2.waitKey
call in your code. Again, it’s hard to diagnose without more detail.alice
Thank you it s okay , please Ineed to get the value of the green and red canal of the skin (PIXEL )d by image did u do it or know how to start to ?
Syed Abdullah Hashmi
Its working amazingly thank you sir..
Manh Nguyen
HI Adrian,
Great post! This post is help me a lot. I have a question, hope you can reply me. Why do you change from RGB to HSV, I think you have some reasons for this work. Thank you!
Adrian Rosebrock
I convert to HSV from RGB because HSV is more similar to how humans perceive color. It’s also easier to define more robust color ranges in HSV than RGB. If you’re interested in learning more about color spaces and how to use them, take a look at the PyImageSearch Gurus course.
Raj
HI Adrian,
I am Raj,
“import error” problem on pi for importing “pyimagesearch”.
Here is the message:
ImportError: No module named ‘pyimagesearch’
Adrian Rosebrock
Please make sure you use the “Downloads” section of this blog post to download the source code. Inside you will find the “pyimagesearch” module.
lissou
Working well ! oh thanks very much great job
Instead Please I need to get the value of the green and red chanel of the skin by the video but don’t have any ideo to do it well ?
I ve understand that you made a conversion gro RGB to HSB why ? and to get the pixel I have to do the opposite to get the red and green channel of the skin ?
Adrian Rosebrock
Refining color ranges tends to be easier and more intuitive in the HSV color space rather than RGB. For defining your own color ranges, take a look at the range-detector script in the imutils library.
licia
Great job ! it s workiing well but when I put the path to work on my video file it doesn’t work need HELP please
Adrian Rosebrock
Hey licia — it sounds like either (1) your path to your input video is invalid, (2) you don’t have the proper video codec installed on your system, or (3) you compiled OpenCV without video support. Double-check your input video path as that is normally the error. Otherwise you should try following one of my OpenCV install tutorials which will compile + install OpenCV with video support.
licia
Hello yes I think I was typing a wrong path it’s working well sorry how can I get the video when the skin is detected to split the video and do image processing how to get the video not the original the other ? thanks thanks
Adrian Rosebrock
Hm, I’m not sure what you mean. Could you try to explain that again?
licia
Okay, the program is working well and the two videos are generated , I want now to save the video (when the skin is detected) in a file for exemple skin.avi and I can t I tried with
out = write (skin)
but no success
Adrian Rosebrock
Take a look at this post on saving key clips to file.
Hossein
Hi
How can I combine this code with face detection (object detection)
For example just execute this code on my face , not my hands
Adrian Rosebrock
You would:
1. Detect the face in the image (see this post to get you started)
2. Extract the face ROI bounding box
3. Apply skin detection to the face ROI
Rohit
how to pass the path of a video in this code?
Adrian Rosebrock
You can do so via command line arguments. If you are new to command line arguments be sure to read this blog post.
Saikiran Siriki
Hi, thanks for the awesome tutorial. I’m working on a project where I’m generating facemask from your dlib 68 landmarks tutorial. However I would like to change the skintone of the obtained image(just the face within the 68 landmarks), how would I go about approaching the problem.
Ankit agrawal
Hello,
I am going through your tutorials in OpenCV ’Skin Detection’.I am a beginner.
I am getting this error again and again:
Traceback (most recent call last):
…
(h, w) = image.shape[:2]
AttributeError: ‘NoneType’ object has no attribute ‘shape’
Can you please guide me to solve this.
Adrian Rosebrock
Hey Ankit — make sure you read this post on NoneType errors to resolve the error 🙂
Ashish
Hello,
cv2.videocapture().read() returning a false value.
I have used a .mp4 video.
Is there any other way to read video files.
Thankyou.
Adrian Rosebrock
It sounds like your machine does not have the proper video codecs installed. Make sure you follow one of my OpenCV tutorials and from there you should be able to open the MP4 file.
Akhil
Sir, you are doing a great job. You are helping the generation. this quality is very rare and many don’t do it for free as you are doing.
Adrian Rosebrock
Thank you Akhil, that is very kind 🙂 I really appreciate that.
benji kohen
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
why did u use 11?
Adrian Rosebrock
The kernel is used to perform a series of erosions and dilations, removing small regions of noise.
vijay singh
Great post Adrian. It would be helpful for me in security intrusion detection. I will try to use it for my object.
perform face detection to an image, determine the color of the skin on their face, and then use that model to detect the rest of the skin on their body.
As it is based on color. I am afraid it can have false detection if the background of the image is same.
Adrian Rosebrock
In that case I think semantic segmentation would be a better option.
Kumar Ujjawal
Hi, I’m getting a blank image instead of skin.
Adrian Rosebrock
Blank as in a “black” output image? If so, that means you need to tune your threshold parameters.
Bishoy Youhana
how do I go about implementing this for individual photos?
Adrian Rosebrock
You can use the “cv2.imread” function to load an input image. From there you can apply the same thresholding techniques to the image.
Bhagya Amarasinghe
Hi, is there a way to cut the first half of the generated mask? I want to obtain the neck separately.
Adrian Rosebrock
You can use NumPy array slicing to extract arbitrary regions of the image/frame.