Commit a96c8d49 authored by WillBrennan's avatar WillBrennan

Added mean_color method

parent f5c59c18
......@@ -11,6 +11,7 @@ import cv2
import numpy
# Custom Modules
import scripts
import mean_color
class SkinDetector(object):
......@@ -76,6 +77,9 @@ class SkinDetector(object):
logger.debug('Initialising process')
dt = time.time()
self.assert_image(img)
logger.debug('Generating mean-color image')
#img = mean_color.img_mean(img)
logger.debug('Conducting thresholding')
self.n_mask = 0
self.mask = numpy.zeros(img.shape[:2], dtype=numpy.uint8)
self.get_mask_hsv(img)
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Will Brennan'
# Built-in modules
import time
import logging
# Standard modules
import numpy
import sklearn.utils
import sklearn.cluster as cluster
# Custom modules
logger = logging.getLogger('main')
def img_mean(frame, n_clusters=64):
result = frame.copy()
flat_frame = frame.reshape(-1, 3)
frame = sklearn.utils.shuffle(flat_frame)[:min(1000, frame.shape[0]*frame.shape[1])]
logger.debug('frame shape: {0}'.format(frame.shape))
logger.debug('starting training...')
t0 = time.time()
kmeans = cluster.KMeans(n_clusters=n_clusters, random_state=0).fit(frame)
logger.debug('training took {0}s'.format(round(time.time()-t0, 2)))
lookup = kmeans.predict(flat_frame)
label_idx = 0
for i in range(result.shape[0]):
for j in range(result.shape[1]):
result[i][j] = kmeans.cluster_centers_[lookup[label_idx]]
label_idx += 1
logger.debug('label shape: {0}'.format(result.shape))
result.astype(dtype=numpy.uint8)
return result
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment