Image Classification Example

This notebook imports the im_classif.py module which has been generated from the decision tree in im_classif.yml using the following command:

$ dectree examples/im_classif/im_classif.yml

In [1]:
import im_classif

In [2]:
from PIL import Image
import numpy as np

Import a test image


In [3]:
im = Image.open('test_im.jpg')

In [4]:
im


Out[4]:

Extract the R,G,B channels as Numpy arrays of type float64


In [5]:
red = np.array(im.getdata(band=0), dtype=np.float64)
green = np.array(im.getdata(band=1), dtype=np.float64)
blue = np.array(im.getdata(band=2), dtype=np.float64)

Classification input


In [6]:
inputs = im_classif.Inputs()
inputs.red = red
inputs.green = green
inputs.blue = blue

Classification output


In [7]:
outputs = im_classif.Outputs()

Classify


In [8]:
im_classif.apply_rules(inputs, outputs)

Display outputs


In [9]:
def truth_to_image(truth):
    im_data = np.array(255 * truth, dtype=np.uint8).reshape(im.height, im.width)
    return Image.fromarray(im_data)

In [10]:
truth_to_image(outputs.cloudy)


Out[10]:

In [11]:
truth_to_image(outputs.grey)


Out[11]:

In [12]:
truth_to_image(outputs.yellow)


Out[12]:

In [13]:
truth_to_image(outputs.dark_red)


Out[13]:

In [14]:
truth_to_image(outputs.dark)


Out[14]:

In [15]:
truth_to_image(outputs.not_dark)


Out[15]:

In [ ]: