diff --git a/p5_classification/dataClassifier.py b/p5_classification/dataClassifier.py index 3ca48cd..aa5b484 100644 --- a/p5_classification/dataClassifier.py +++ b/p5_classification/dataClassifier.py @@ -54,6 +54,14 @@ def basicFeatureExtractorFace(datum): each pixel in the provided datum is an edge (1) or no edge (0) """ a = datum.getPixels() + features = util.Counter() + for x in range(DIGIT_DATUM_WIDTH): + for y in range(DIGIT_DATUM_HEIGHT): + if datum.getPixel(x, y) > 0: + features[(x,y)] = 1 + else: + features[(x,y)] = 0 + return features features = util.Counter() for x in range(FACE_DATUM_WIDTH): @@ -64,6 +72,43 @@ def basicFeatureExtractorFace(datum): features[(x,y)] = 0 return features + +def whiteAreasFeature(datum): + def getNeighbors(pixel): + x, y = pixel + potentialNeighbors = [ + (x - 1, y + 1), (x, y + 1), (x + 1, y + 1), + (x - 1, y), (x + 1, y), + (x - 1, y - 1), (x, y - 1), (x + 1, y - 1), + ] + neighbors = [(x, y) for x, y in potentialNeighbors + if x >= 0 and x < DIGIT_DATUM_WIDTH + if y >= 0 and y < DIGIT_DATUM_HEIGHT] + return neighbors + + whitePixels = set() + for x in range(DIGIT_DATUM_WIDTH): + for y in range(DIGIT_DATUM_HEIGHT): + if datum.getPixel(x, y) > 0.9: + pass + else: + whitePixels.add((x, y)) + + count = 0 + while whitePixels: + count += 1 + queue = set([whitePixels.pop()]) + while queue: + currentPixel = queue.pop() + for neighbor in getNeighbors(currentPixel): + if neighbor in whitePixels: + whitePixels.discard(neighbor) + queue.add(neighbor) + return {("whiteAreas", i): + 1 if i == count else 0 + for i in range(1, 4)} + + def enhancedFeatureExtractorDigit(datum): """ Your feature extraction playground. @@ -76,14 +121,11 @@ def enhancedFeatureExtractorDigit(datum): ## """ features = basicFeatureExtractorDigit(datum) - - "*** YOUR CODE HERE ***" - util.raiseNotDefined() - + whiteAreas = whiteAreasFeature(datum) + features.update(whiteAreas) return features - def basicFeatureExtractorPacman(state): """ A basic feature extraction function. @@ -175,7 +217,8 @@ def analysis(classifier, guesses, testLabels, testData, rawTestData, printImage) # print "Predicted %d; truth is %d" % (prediction, truth) # print "Image: " # print rawTestData[i] - # break + # print whiteAreasFeature(rawTestData[i]) + # break ## =====================