├── face.png ├── results ├── result_HSV.png └── result_RGB.png ├── README.md └── tree └── tree.py /face.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrgloom/skin-detection-example/HEAD/face.png -------------------------------------------------------------------------------- /results/result_HSV.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrgloom/skin-detection-example/HEAD/results/result_HSV.png -------------------------------------------------------------------------------- /results/result_RGB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrgloom/skin-detection-example/HEAD/results/result_RGB.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # skin-detection-example 2 | 3 | Implemented in python. Dependencies: numpy, opencv, sklearn. 4 | 5 | Simple pixelwise skin detection using [decision tree](http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html) [1](http://scikit-learn.org/stable/modules/tree.html#tree-algorithms-id3-c4-5-c5-0-and-cart) using pixels in RGB or HSV color space as features. 6 | 7 | Skin Segmentation Data Set from https://archive.ics.uci.edu/ml/datasets/Skin+Segmentation 8 | 9 | face.png image from http://graphics.cs.msu.ru/ru/node/899 10 | 11 | Results: 12 | 13 | ![alt tag](https://github.com/mrgloom/Simple-skin-detection/blob/master/face.png) 14 | ![alt tag](https://github.com/mrgloom/Simple-skin-detection/blob/master/results/result_RGB.png) 15 | ![alt tag](https://github.com/mrgloom/Simple-skin-detection/blob/master/results/result_HSV.png) 16 | 17 | 18 | TODO: 19 | ~~~ 20 | Add tree visualization 21 | https://github.com/NikTRSK/Data-Science-and-Machine-Learning-with-Python---Hands-On/blob/master/DecisionTree.ipynb 22 | ~~~ 23 | -------------------------------------------------------------------------------- /tree/tree.py: -------------------------------------------------------------------------------- 1 | #Skin Segmentation Data Set from https://archive.ics.uci.edu/ml/datasets/Skin+Segmentation 2 | #face.png image from http://graphics.cs.msu.ru/ru/node/899 3 | 4 | import numpy as np 5 | import cv2 6 | 7 | from sklearn import tree 8 | from sklearn.cross_validation import train_test_split 9 | 10 | def ReadData(): 11 | #Data in format [B G R Label] from 12 | data = np.genfromtxt('../data/Skin_NonSkin.txt', dtype=np.int32) 13 | 14 | labels= data[:,3] 15 | data= data[:,0:3] 16 | 17 | return data, labels 18 | 19 | def BGR2HSV(bgr): 20 | bgr= np.reshape(bgr,(bgr.shape[0],1,3)) 21 | hsv= cv2.cvtColor(np.uint8(bgr), cv2.COLOR_BGR2HSV) 22 | hsv= np.reshape(hsv,(hsv.shape[0],3)) 23 | 24 | return hsv 25 | 26 | def TrainTree(data, labels, flUseHSVColorspace): 27 | if(flUseHSVColorspace): 28 | data= BGR2HSV(data) 29 | 30 | trainData, testData, trainLabels, testLabels = train_test_split(data, labels, test_size=0.20, random_state=42) 31 | 32 | print trainData.shape 33 | print trainLabels.shape 34 | print testData.shape 35 | print testLabels.shape 36 | 37 | clf = tree.DecisionTreeClassifier(criterion='entropy') 38 | clf = clf.fit(trainData, trainLabels) 39 | print clf.feature_importances_ 40 | print clf.score(testData, testLabels) 41 | 42 | return clf 43 | 44 | def ApplyToImage(path, flUseHSVColorspace): 45 | data, labels= ReadData() 46 | clf= TrainTree(data, labels, flUseHSVColorspace) 47 | 48 | img= cv2.imread(path) 49 | print img.shape 50 | data= np.reshape(img,(img.shape[0]*img.shape[1],3)) 51 | print data.shape 52 | 53 | if(flUseHSVColorspace): 54 | data= BGR2HSV(data) 55 | 56 | predictedLabels= clf.predict(data) 57 | 58 | imgLabels= np.reshape(predictedLabels,(img.shape[0],img.shape[1],1)) 59 | 60 | if (flUseHSVColorspace): 61 | cv2.imwrite('../results/result_HSV.png',((-(imgLabels-1)+1)*255))# from [1 2] to [0 255] 62 | else: 63 | cv2.imwrite('../results/result_RGB.png',((-(imgLabels-1)+1)*255)) 64 | 65 | 66 | #--------------------------------------------- 67 | ApplyToImage("../face.png", True) 68 | ApplyToImage("../face.png", False) --------------------------------------------------------------------------------