def mvndSkinDetection(): ''' Skin detection - compute a MVND for both skin and non-skin. Classify the test and training image using the classify helper function. Note that the "mask" binary images are used as the ground truth. ''' sdata = scipy.io.loadmat(os.path.join(dataPath,'skin.mat'))['sdata'] ndata = scipy.io.loadmat(os.path.join(dataPath,'nonskin.mat'))['ndata'] mvn_sskin = MVND(sdata) mvn_nskin = MVND(ndata) # Optain priors mask = imageHelper() mask.loadImageFromFile(os.path.join(dataPath, 'mask.png')) prior_skin = mask.getLinearImage().mean() prior_nonskin = 1-prior_skin print("TRAINING DATA") trainingmaskObj = imageHelper() trainingmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask.png')) trainingimageObj = imageHelper() trainingimageObj.loadImageFromFile(os.path.join(dataPath, 'image.png')) classify(trainingimageObj, trainingmaskObj, mvn_sskin, mvn_nskin, "Training", prior_skin, prior_nonskin) print("TEST DATA") testmaskObj = imageHelper() testmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask-test.png')) testimageObj = imageHelper() testimageObj.loadImageFromFile(os.path.join(dataPath, 'test.png')) classify(testimageObj, testmaskObj, mvn_sskin, mvn_nskin, "Test", prior_skin, prior_nonskin) plt.show()
def gmmSkinDetection() -> None: ''' Skin detection - train a GMM for both skin and non-skin. Classify the test and training image using the classify helper function. Note that the "mask" binary images are used as the ground truth. ''' K = 1 iter = 50 sdata = scipy.io.loadmat(os.path.join(dataPath, 'skin.mat'))['sdata'] ndata = scipy.io.loadmat(os.path.join(dataPath, 'nonskin.mat'))['ndata'] gmms = gmm_em(sdata, K, iter) gmmn = gmm_em(ndata, K, iter) print("TRAINING DATA") trainingmaskObj = imageHelper() trainingmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask.png')) trainingimageObj = imageHelper() trainingimageObj.loadImageFromFile(os.path.join(dataPath, 'image.png')) prior_skin, prior_nonskin = get_prior(trainingmaskObj) classify(trainingimageObj, trainingmaskObj, gmms, gmmn, "training", prior_skin=prior_skin, prior_nonskin=prior_nonskin) print("TEST DATA") testmaskObj = imageHelper() testmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask-test.png')) testimageObj = imageHelper() testimageObj.loadImageFromFile(os.path.join(dataPath, 'test.png')) classify(testimageObj, testmaskObj, gmms, gmmn, "test", prior_skin=prior_skin, prior_nonskin=prior_nonskin) plt.show()
def classify_data(self): # helper function to call the mthods in the Cluster Class trainingFileName = "training.csv" testingFileName = "testing.csv" outPutFileName = "prediction.csv" cla = classify(trainingFileName, testingFileName, outPutFileName) cla.NaiveBayesClassifier() return outPutFileName, trainingFileName
def mvndSkinDetection() -> None: ''' Skin detection - compute a MVND for both skin and non-skin. Classify the test and training image using the classify helper function. Note that the "mask" binary images are used as the ground truth. ''' sdata = scipy.io.loadmat(os.path.join(dataPath, 'skin.mat'))['sdata'] ndata = scipy.io.loadmat(os.path.join(dataPath, 'nonskin.mat'))['ndata'] ## Multivariate Gaussian for skin and non-skin mvn_sskin = [MVND(sdata)] mvn_nskin = [MVND(ndata)] # Optain priors mask = imageHelper() mask.loadImageFromFile(os.path.join(dataPath, 'mask.png')) # EXERCISE 2 - Compute the skin and nonskin prior prior_skin, prior_nonskin = get_prior(mask) print(prior_skin) print("TRAINING DATA") trainingmaskObj = imageHelper() trainingmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask.png')) trainingimageObj = imageHelper() trainingimageObj.loadImageFromFile(os.path.join(dataPath, 'image.png')) # (img: imageHelper, mask: imageHelper, skin_mvnd: List[MVND], notSkin_mvnd: List[MVND], fig: str = "", prior_skin: float = 0.5, prior_nonskin: float = 0.5) classify(trainingimageObj, trainingmaskObj, mvn_sskin, mvn_nskin, "Training", prior_skin, prior_nonskin) print("TEST DATA") testmaskObj = imageHelper() testmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask-test.png')) testimageObj = imageHelper() testimageObj.loadImageFromFile(os.path.join(dataPath, 'test.png')) classify(testimageObj, testmaskObj, mvn_sskin, mvn_nskin, "Test", prior_skin, prior_nonskin) plt.show()
def mvndSkinDetection() -> None: ''' Skin detection - compute a MVND for both skin and non-skin. Classify the test and training image using the classify helper function. Note that the "mask" binary images are used as the ground truth. ''' sdata = scipy.io.loadmat(os.path.join(dataPath, 'skin.mat'))['sdata'] ndata = scipy.io.loadmat(os.path.join(dataPath, 'nonskin.mat'))['ndata'] mvn_sskin = [MVND(sdata)] mvn_nskin = [MVND(ndata)] # Optain priors mask = imageHelper() mask.loadImageFromFile(os.path.join(dataPath, 'mask.png')) # TODO: EXERCISE 2 - Compute the skin and nonskin prior -> solved prior_skin, prior_nonskin = get_prior(mask) print("TRAINING DATA") trainingmaskObj = imageHelper() trainingmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask.png')) trainingimageObj = imageHelper() trainingimageObj.loadImageFromFile(os.path.join(dataPath, 'image.png')) classify(trainingimageObj, trainingmaskObj, mvn_sskin, mvn_nskin, "Training-MVND", prior_skin, prior_nonskin) print("TEST DATA PORTRAIT") testmaskObj = imageHelper() testmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask-test1.png')) testimageObj = imageHelper() testimageObj.loadImageFromFile(os.path.join(dataPath, 'test1.png')) classify(testimageObj, testmaskObj, mvn_sskin, mvn_nskin, "Test-portrait-MVND", prior_skin, prior_nonskin) print("TEST DATA FAMILY") testmaskObj = imageHelper() testmaskObj.loadImageFromFile(os.path.join(dataPath, 'mask-test2.png')) testimageObj = imageHelper() testimageObj.loadImageFromFile(os.path.join(dataPath, 'test2.png')) classify(testimageObj, testmaskObj, mvn_sskin, mvn_nskin, "Test-family-MVND", prior_skin, prior_nonskin) plt.show()