In [2]:
import os
import numpy as np
from sklearn.svm import SVC
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from PIL import Image

In [3]:
datapath = './data/english-img/good-img/bmp/'

In [10]:
def resize_and_crop(image, size):
    img_ratio = image.size[0] / float(image.size[1])
    ratio = size[0] / float(size[1])
    
    if ratio > img_ratio:
        image = image.resize((size[0], size[0] * image.size[1] / image.size[0]), Image.ANTIALIAS)
        image = image.crop((0,0,30,30))
    elif ratio < img_ratio:
        image = image.resize((size[1] * image.size[0] / image.size[1], size[1]), Image.ANTIALIAS)
        image = image.crop((0,0,30,30))
    else:
        image = image.resize((size[0], size[1]), Image.ANTIALIAS)
    return image

In [11]:
X = []
y = []

In [12]:
for path, subdirs, files in os.walk(datapath):
    for filename in files:
        f = os.path.join(path, filename)
        img = Image.open(f).convert('L')
        img_resized = resize_and_crop(img, (30,30))
        img_resized = np.asarray(img_resized.getdata(), dtype=np.float64).reshape((img_resized.size[1] * img_resized.size[0], 1))
        target = filename[3:filename.index('-')]
        X.append(img_resized)
        y.append(target)

In [13]:
X = np.array(X)
X = X.reshape(X.shape[:2])

In [15]:
classifier = SVC(verbose=0, kernel='poly', degree=3)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
classifier.fit(X_train, y_train)
predictions = classifier.predict(X_test)
print classification_report(y_test, predictions)


             precision    recall  f1-score   support

        001       0.12      0.17      0.14        24
        002       0.09      0.15      0.11        20
        003       0.00      0.00      0.00        20
        004       0.38      0.50      0.43        12
        005       0.11      0.33      0.16        15
        006       0.12      0.43      0.19        14
        007       0.22      0.15      0.18        13
        008       0.25      0.12      0.17         8
        009       0.05      0.10      0.06        10
        010       0.10      0.29      0.15         7
        011       0.84      0.61      0.71       127
        012       0.19      0.31      0.24        35
        013       0.34      0.29      0.31        49
        014       0.42      0.33      0.37        46
        015       0.62      0.48      0.54       110
        016       0.03      0.06      0.04        16
        017       0.21      0.29      0.25        31
        018       0.50      0.27      0.35        55
        019       0.55      0.53      0.54        85
        020       0.18      0.09      0.12        23
        021       0.29      0.17      0.22        23
        022       0.72      0.54      0.62        52
        023       0.33      0.19      0.25        36
        024       0.76      0.55      0.64        85
        025       0.43      0.24      0.31       105
        026       0.27      0.14      0.18        50
        027       0.00      0.00      0.00         8
        028       0.45      0.34      0.39        99
        029       0.53      0.30      0.38        70
        030       0.78      0.54      0.64        80
        031       0.06      0.18      0.09        22
        032       0.24      0.28      0.26        29
        033       0.29      0.29      0.29        14
        034       0.10      0.06      0.08        16
        035       0.36      0.33      0.35        12
        036       0.18      0.18      0.18        11
        037       0.33      0.38      0.35        48
        038       0.15      0.29      0.20        14
        039       0.12      0.13      0.12        23
        040       0.50      0.20      0.29        10
        041       0.46      0.41      0.43        64
        042       0.04      0.09      0.05        11
        043       0.10      0.20      0.13        15
        044       0.13      0.33      0.19        15
        045       0.32      0.27      0.29        30
        046       0.25      0.14      0.18         7
        047       0.19      0.30      0.23        10
        048       0.24      0.24      0.24        21
        049       0.06      0.12      0.08         8
        050       0.38      0.39      0.38        36
        051       0.16      0.26      0.20        34
        052       0.12      0.10      0.11        10
        053       0.00      0.00      0.00        11
        054       0.26      0.42      0.32        26
        055       0.06      0.10      0.08        29
        056       0.53      0.29      0.37        28
        057       0.08      0.25      0.12         8
        058       0.00      0.00      0.00         9
        059       0.07      0.17      0.10         6
        060       0.20      0.12      0.15         8
        061       0.00      0.00      0.00         7
        062       0.09      0.14      0.11         7

avg / total       0.41      0.33      0.36      1927


In [ ]: