Human Skin Detection Using RGB, HSV and YCbCr Color. this code is all algorithmic way to detect human skin and best for the preprocessing of the Deep learning as zero shot methods. This method is implemented from a paper!
This implementation is based on the paper: "Human Skin Detection Using RGB, HSV and YCbCr Color Models" by S.Kolkur, D.Kalbande, P.Shimpi, C.Bapat, J.Jatakia.
- Python : Popular language for implementing Neural Network
- Jupyter Notebook : Best tool for running python cell by cell
- Google Colab : Best Space for running Jupyter Notebook with hosted server
- OpenCV : Best Library for working with images
- Numpy : Best Library for working with arrays in python
- MatPlotLib : Library for showing the charts in python
You can easily run this code on google colab by just clicking this badge
we need to import these libraries :
cv2, numpy, matplotlib
import cv2
import numpy as np
import matplotlib.pyplot as pltWe need to Download the images from my Github repository or you can download other human pictures by your own.
!wget https://raw.githubusercontent.com/AsadiAhmad/Human-Skin-Detection/main/Pictures/Input/jensen_huang.jpg -O jensen_huang.jpg
!wget https://raw.githubusercontent.com/AsadiAhmad/Human-Skin-Detection/main/Pictures/Input/elon_musk.jpg -O elon_musk.jpg
!wget https://raw.githubusercontent.com/AsadiAhmad/Human-Skin-Detection/main/Pictures/Input/mark_zukerberg.jpg -O mark_zukerberg.jpg
!wget https://raw.githubusercontent.com/AsadiAhmad/Human-Skin-Detection/main/Pictures/Input/linus_torvalds.jpg -O linus_torvalds.jpgWe need to load images into python variables we ues OpenCV library to read the images also the format of the images are nd.array.
bgr_image = cv2.imread('jensen_huang.jpg')
elon_musk = cv2.imread('elon_musk.jpg')
mark_zukerberg = cv2.imread('mark_zukerberg.jpg')
linus_torvalds = cv2.imread('linus_torvalds.jpg')In this algorithmic approch we need to get teh HSV and YCrCb color formats for calculating the conditions
hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)
ycrcb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2YCrCb)HSV color format :
YCbCr color format :
For calculating the conditions we need to split each channel from each color space.
B, G, R = cv2.split(bgr_image)
H, S, V = cv2.split(hsv_image)
Y, Cr, Cb = cv2.split(ycrcb_image)We should apply all conditions from each color space. if each pixel pass all conditions it means that pixel is a skin pixel.
def conditions(r, g, b, h, s, v, y, cr, cb):
s = s / 255.0
condition_rgb = (r > 95) and (g > 40) and (b > 20) and (r > g) and (r > b) and (abs(r-g) > 15)
condition_hsv = (0 <= h <= 50) and (0.23 <= s <= 0.68)
condition_ycrcb = (cr > 135) and (cb > 85) and (y > 80) and (cr <= (1.5862*cb)+20) and (cr>=(0.3448*cb)+76.2069) and (cr >= (-4.5652*cb)+234.5652) and (cr <= (-1.15*cb)+301.75) and (cr <= (-2.2857*cb)+432.85)
return condition_rgb and condition_hsv and condition_ycrcbheight, width, channels = bgr_image.shape
image = np.zeros((height, width), np.uint8)
main_image = bgr_image.copy()
for i in range(height):
for j in range(width):
if conditions(R[i, j], G[i, j], B[i, j], H[i, j], S[i, j], V[i, j], Y[i, j], Cr[i, j], Cb[i, j]):
image[i, j] = 255
else:
main_image[i, j] = [0, 0, 0]Now we need to see what have done to images.
fig, axs = plt.subplots(1, 3, figsize=(12, 4))
axs[0].imshow(cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB))
axs[0].set_title('Original Image')
axs[0].axis('off')
axs[1].imshow(image, cmap='gray')
axs[1].set_title('Skin Mask')
axs[1].axis('off')
axs[2].imshow(cv2.cvtColor(main_image, cv2.COLOR_BGR2RGB))
axs[2].set_title('Detected Skin Areas')
axs[2].axis('off')
plt.tight_layout()
plt.show()So in this step we put all of things together and test that for other pictures.
def skin_detector(bgr_image):
hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)
ycrcb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2YCrCb)
B, G, R = cv2.split(bgr_image)
H, S, V = cv2.split(hsv_image)
Y, Cr, Cb = cv2.split(ycrcb_image)
height, width, channels = bgr_image.shape
image = np.zeros((height, width), np.uint8)
main_image = bgr_image.copy()
for i in range(height):
for j in range(width):
if conditions(R[i, j], G[i, j], B[i, j], H[i, j], S[i, j], V[i, j], Y[i, j], Cr[i, j], Cb[i, j]):
image[i, j] = 255
else:
main_image[i, j] = [0, 0, 0]
fig, axs = plt.subplots(1, 3, figsize=(12, 4))
axs[0].imshow(cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB))
axs[0].set_title('Original Image')
axs[0].axis('off')
axs[1].imshow(image, cmap='gray')
axs[1].set_title('Skin Mask')
axs[1].axis('off')
axs[2].imshow(cv2.cvtColor(main_image, cv2.COLOR_BGR2RGB))
axs[2].set_title('Detected Skin Areas')
axs[2].axis('off')
plt.tight_layout()
plt.show()This project is licensed under the MIT License.





