-
Notifications
You must be signed in to change notification settings - Fork 0
/
skinSegmentation_RealtimePredictions.py
191 lines (114 loc) · 4.53 KB
/
skinSegmentation_RealtimePredictions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import cv2
from sklearn.cluster import KMeans
from collections import Counter
import imutils
import pprint
from matplotlib import pyplot as plt
# In[2]:
def extractSkin(image):
# Taking a copy of the image
img = image.copy()
# Converting from BGR Colours Space to HSV
img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# Defining HSV Threadholds
lower_threshold = np.array([0, 48, 80], dtype=np.uint8)
upper_threshold = np.array([20, 255, 255], dtype=np.uint8)
# Single Channel mask,denoting presence of colours in the about threshold
skinMask = cv2.inRange(img,lower_threshold,upper_threshold)
# Cleaning up mask using Gaussian Filter
skinMask = cv2.GaussianBlur(skinMask,(3,3),0)
# Extracting skin from the threshold mask
skin = cv2.bitwise_and(img,img,mask=skinMask)
# Return the Skin image
return cv2.cvtColor(skin,cv2.COLOR_HSV2BGR)
# In[3]:
def removeBlack(estimator_labels, estimator_cluster):
# Check for black
hasBlack = False
# Get the total number of occurance for each color
occurance_counter = Counter(estimator_labels)
# Quick lambda function to compare to lists
compare = lambda x, y: Counter(x) == Counter(y)
# Loop through the most common occuring color
for x in occurance_counter.most_common(len(estimator_cluster)):
# Quick List comprehension to convert each of RBG Numbers to int
color = [int(i) for i in estimator_cluster[x[0]].tolist() ]
# Check if the color is [0,0,0] that if it is black
if compare(color , [0,0,0]) == True:
# delete the occurance
del occurance_counter[x[0]]
# remove the cluster
hasBlack = True
estimator_cluster = np.delete(estimator_cluster,x[0],0)
break
return (occurance_counter,estimator_cluster,hasBlack)
# In[4]:
def getColorInformation(estimator_labels, estimator_cluster,hasThresholding=False):
# Variable to keep count of the occurance of each color predicted
occurance_counter = None
# Output list variable to return
colorInformation = []
#Check for Black
hasBlack =False
# If a mask has be applied, remove th black
if hasThresholding == True:
(occurance,cluster,black) = removeBlack(estimator_labels,estimator_cluster)
occurance_counter = occurance
estimator_cluster = cluster
hasBlack = black
else:
occurance_counter = Counter(estimator_labels)
# Get the total sum of all the predicted occurances
totalOccurance = sum(occurance_counter.values())
# Loop through all the predicted colors
for x in occurance_counter.most_common(len(estimator_cluster)):
index = (int(x[0]))
# Quick fix for index out of bound when there is no threshold
index = (index-1) if ((hasThresholding & hasBlack)& (int(index) !=0)) else index
# Get the color number into a list
color = estimator_cluster[index].tolist()
# Get the percentage of each color
color_percentage= (x[1]/totalOccurance)
#make the dictionay of the information
colorInfo = {"cluster_index":index , "color": color , "color_percentage" : color_percentage }
# Add the dictionary to the list
colorInformation.append(colorInfo)
return colorInformation
# In[5]:
def extractDominantColor(image,number_of_colors=5,hasThresholding=False):
# Quick Fix Increase cluster counter to neglect the black(Read Article)
if hasThresholding == True:
number_of_colors +=1
# Taking Copy of the image
img = image.copy()
# Convert Image into RGB Colours Space
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# Reshape Image
img = img.reshape((img.shape[0]*img.shape[1]) , 3)
#Initiate KMeans Object
estimator = KMeans(n_clusters=number_of_colors, random_state=0)
# Fit the image
estimator.fit(img)
# Get Colour Information
colorInformation = getColorInformation(estimator.labels_,estimator.cluster_centers_,hasThresholding)
return colorInformation
# In[6]:
def plotColorBar(colorInformation):
#Create a 500x100 black image
color_bar = np.zeros((100,500,3), dtype="uint8")
top_x = 0
for x in colorInformation:
bottom_x = top_x + (x["color_percentage"] * color_bar.shape[1])
color = tuple(map(int,(x['color'])))
cv2.rectangle(color_bar , (int(top_x),0) , (int(bottom_x),color_bar.shape[0]) ,color , -1)
top_x = bottom_x
return color_bar
# In[7]:
def prety_print_data(color_info):
for x in color_info:
print(pprint.pformat(x))
print()