In [1]:
%matplotlib inline
from matplotlib import pyplot as plt, cm
from skimage import io
from skimage import data, segmentation, filters, color, img_as_float, img_as_ubyte, exposure, feature, measure, morphology
from skimage.color import rgb2gray
from skimage.feature import hog
from skimage.morphology import square
import cv2
import numpy as np
from glob import glob
import os
from sklearn.metrics import classification_report
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from time import time
In [ ]:
# plan 1
# train data : captcha 1000(feature : HOG)
# test data : captcha 200 images (feature : HOG)
In [38]:
# 새로운 200개의 이미지를 test 데이터로 만들자.
p = "./data_test"
md5list = glob(os.path.join(p, "*.png"))
md5list = [os.path.split(fname)[1] for fname in md5list]
print "the number of files is %s" %len(md5list)
features = []
lables = []
t0 = time()
# captcha를 preprossing후 mnist처럼 numpy array로 만들자
for fname in md5list:
lable = os.path.split(fname)[1].split("_")[1][:5]
im = io.imread(os.path.join(p, fname))
w, h, _ = im.shape
for x in range(w):
for j in range(h):
if im[x][j][0] == im[x][j][1] and im[x][j][1] == im[x][j][2] and im[x][j][2] == im[x][j][0]:
im[x][j][0] = 255
im[x][j][1] = 255
im[x][j][2] = 255
im_gray = rgb2gray(im)
im_gray = img_as_ubyte(im_gray)
im_gray = morphology.opening(im_gray, square(2))
im_gray_equalize = exposure.equalize_hist(im_gray)
threshold = filters.threshold_otsu(im_gray_equalize).copy()
threshold = im_gray_equalize < threshold
threshold = img_as_ubyte(threshold)
bw = morphology.closing(im_gray_equalize < threshold, square(3))
cleared = bw.copy()
im_th = cleared
ctrs, hier = cv2.findContours(img_as_ubyte(im_th.copy()), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rects = sorted(rects, key=lambda tup: tup[0])
if len(rects) != 5:
continue
for rect, l in zip(rects, lable):
# Draw the rectangles
cv2.rectangle(threshold, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1)
# Make the rectangular region around the digit
roi = threshold[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = morphology.closing(roi, square(4))
features.append(roi.ravel())
lables.append([l])
features = np.array(features, 'int16')
labels = np.array(lables, 'int').ravel()
# features, lables의 차원을 출력
print features.shape
print labels.shape
print "escape time : ", round(time()-t0, 3), "s"
t0 = time()
list_hog_fd = []
for feature in features:
fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
list_hog_fd.append(fd)
hog_features = np.array(list_hog_fd, 'float64')
print "escape time : ", round(time()-t0, 3), "s"
classifiers = glob("./pkl/hog/skt/*.pkl")
for classifier in classifiers:
clf = joblib.load(classifier)
print clf
print classification_report(labels, clf.predict(hog_features))
print accuracy_score(labels, clf.predict(hog_features))
print confusion_matrix(labels, clf.predict(hog_features))
print "=" * 100
In [ ]:
# plan 2
# train data : captcha 1000(feature : No)
# test data : captcha 200 images (feature : No)
In [40]:
# 새로운 200개의 이미지를 test 데이터로 만들자.
p = "./data_test"
md5list = glob(os.path.join(p, "*.png"))
md5list = [os.path.split(fname)[1] for fname in md5list]
print "the number of files is %s" %len(md5list)
features = []
lables = []
t0 = time()
# captcha를 preprossing후 mnist처럼 numpy array로 만들자
for fname in md5list:
lable = os.path.split(fname)[1].split("_")[1][:5]
im = io.imread(os.path.join(p, fname))
w, h, _ = im.shape
for x in range(w):
for j in range(h):
if im[x][j][0] == im[x][j][1] and im[x][j][1] == im[x][j][2] and im[x][j][2] == im[x][j][0]:
im[x][j][0] = 255
im[x][j][1] = 255
im[x][j][2] = 255
im_gray = rgb2gray(im)
im_gray = img_as_ubyte(im_gray)
im_gray = morphology.opening(im_gray, square(2))
im_gray_equalize = exposure.equalize_hist(im_gray)
threshold = filters.threshold_otsu(im_gray_equalize).copy()
threshold = im_gray_equalize < threshold
threshold = img_as_ubyte(threshold)
bw = morphology.closing(im_gray_equalize < threshold, square(3))
cleared = bw.copy()
im_th = cleared
ctrs, hier = cv2.findContours(img_as_ubyte(im_th.copy()), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rects = sorted(rects, key=lambda tup: tup[0])
if len(rects) != 5:
continue
for rect, l in zip(rects, lable):
# Draw the rectangles
cv2.rectangle(threshold, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1)
# Make the rectangular region around the digit
roi = threshold[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = morphology.closing(roi, square(4))
features.append(roi.ravel())
lables.append([l])
features = np.array(features, 'int16')
labels = np.array(lables, 'int').ravel()
# features, lables의 차원을 출력
print features.shape
print labels.shape
print "escape time : ", round(time()-t0, 3), "s"
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
classifiers = glob("./pkl/scale/skt/*.pkl")
for classifier in classifiers:
clf = joblib.load(classifier)
print clf
print classification_report(labels, clf.predict(features))
print accuracy_score(labels, clf.predict(features))
print confusion_matrix(labels, clf.predict(features))
print "=" * 100
In [28]:
# plan 3
# train data : MNIST(feature : HOG)
# test data : captcha 200 images (feature : HOG)
In [23]:
# 새로운 200개의 이미지를 test 데이터로 만들자.
p = "./data_test"
md5list = glob(os.path.join(p, "*.png"))
md5list = [os.path.split(fname)[1] for fname in md5list]
print "the number of files is %s" %len(md5list)
features = []
lables = []
t0 = time()
# captcha를 preprossing후 mnist처럼 numpy array로 만들자
for fname in md5list:
lable = os.path.split(fname)[1].split("_")[1][:5]
im = io.imread(os.path.join(p, fname))
w, h, _ = im.shape
for x in range(w):
for j in range(h):
if im[x][j][0] == im[x][j][1] and im[x][j][1] == im[x][j][2] and im[x][j][2] == im[x][j][0]:
im[x][j][0] = 255
im[x][j][1] = 255
im[x][j][2] = 255
im_gray = rgb2gray(im)
im_gray = img_as_ubyte(im_gray)
im_gray = morphology.opening(im_gray, square(2))
im_gray_equalize = exposure.equalize_hist(im_gray)
threshold = filters.threshold_otsu(im_gray_equalize).copy()
threshold = im_gray_equalize < threshold
threshold = img_as_ubyte(threshold)
bw = morphology.closing(im_gray_equalize < threshold, square(3))
cleared = bw.copy()
im_th = cleared
ctrs, hier = cv2.findContours(img_as_ubyte(im_th.copy()), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rects = sorted(rects, key=lambda tup: tup[0])
if len(rects) != 5:
continue
for rect, l in zip(rects, lable):
# Draw the rectangles
cv2.rectangle(threshold, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1)
# Make the rectangular region around the digit
roi = threshold[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = morphology.closing(roi, square(4))
features.append(roi.ravel())
lables.append([l])
features = np.array(features, 'int16')
labels = np.array(lables, 'int').ravel()
# features, lables의 차원을 출력
print features.shape
print labels.shape
print "escape time : ", round(time()-t0, 3), "s"
t0 = time()
list_hog_fd = []
for feature in features:
fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
list_hog_fd.append(fd)
hog_features = np.array(list_hog_fd, 'float64')
print "escape time : ", round(time()-t0, 3), "s"
In [34]:
classifiers = glob("./pkl/hog/mnist/*.pkl")
for classifier in classifiers:
clf = joblib.load(classifier)
print clf
print classification_report(labels, clf.predict(hog_features))
print accuracy_score(labels, clf.predict(hog_features))
print confusion_matrix(labels, clf.predict(hog_features))
print "=" * 100
In [28]:
# plan 4
# train data : MNIST(feature : No)
# test data : captcha 200 images (feature : No)
In [2]:
# 새로운 200개의 이미지를 test 데이터로 만들자.
p = "./data_test"
md5list = glob(os.path.join(p, "*.png"))
md5list = [os.path.split(fname)[1] for fname in md5list]
print "the number of files is %s" %len(md5list)
features = []
lables = []
t0 = time()
# captcha를 preprossing후 mnist처럼 numpy array로 만들자
for fname in md5list:
lable = os.path.split(fname)[1].split("_")[1][:5]
im = io.imread(os.path.join(p, fname))
w, h, _ = im.shape
for x in range(w):
for j in range(h):
if im[x][j][0] == im[x][j][1] and im[x][j][1] == im[x][j][2] and im[x][j][2] == im[x][j][0]:
im[x][j][0] = 255
im[x][j][1] = 255
im[x][j][2] = 255
im_gray = rgb2gray(im)
im_gray = img_as_ubyte(im_gray)
im_gray = morphology.opening(im_gray, square(2))
im_gray_equalize = exposure.equalize_hist(im_gray)
threshold = filters.threshold_otsu(im_gray_equalize).copy()
threshold = im_gray_equalize < threshold
threshold = img_as_ubyte(threshold)
bw = morphology.closing(im_gray_equalize < threshold, square(3))
cleared = bw.copy()
im_th = cleared
ctrs, hier = cv2.findContours(img_as_ubyte(im_th.copy()), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rects = sorted(rects, key=lambda tup: tup[0])
if len(rects) != 5:
continue
for rect, l in zip(rects, lable):
# Draw the rectangles
cv2.rectangle(threshold, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1)
# Make the rectangular region around the digit
roi = threshold[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = morphology.closing(roi, square(4))
features.append(roi.ravel())
lables.append([l])
features = np.array(features, 'int16')
labels = np.array(lables, 'int').ravel()
# features, lables의 차원을 출력
print features.shape
print labels.shape
print "escape time : ", round(time()-t0, 3), "s"
t0 = time()
def scale(X, eps = 0.001):
# scale the data points s.t the columns of the feature space
# (i.e the predictors) are within the range [0, 1]
return (X - np.min(X, axis = 0)) / (np.max(X, axis = 0) + eps)
features = features.astype("float32")
features = scale(features)
print "escape time : ", round(time()-t0, 3), "s"
In [3]:
classifiers = glob("./pkl/scale/mnist/*.pkl")
for classifier in classifiers:
clf = joblib.load(classifier)
print clf
print classification_report(labels, clf.predict(features))
print accuracy_score(labels, clf.predict(features))
print confusion_matrix(labels, clf.predict(features))
print "=" * 100
In [53]:
def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues, labels=labels):
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(set(list(labels))))
plt.xticks(tick_marks, list(set(list(labels))), rotation=45)
plt.yticks(tick_marks, list(set(list(labels))))
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
In [54]:
# Confusion matrix of SVC on Plan1
# 새로운 200개의 이미지를 test 데이터로 만들자.
p = "./data_test"
md5list = glob(os.path.join(p, "*.png"))
md5list = [os.path.split(fname)[1] for fname in md5list]
print "the number of files is %s" %len(md5list)
features = []
lables = []
t0 = time()
# captcha를 preprossing후 mnist처럼 numpy array로 만들자
for fname in md5list:
lable = os.path.split(fname)[1].split("_")[1][:5]
im = io.imread(os.path.join(p, fname))
w, h, _ = im.shape
for x in range(w):
for j in range(h):
if im[x][j][0] == im[x][j][1] and im[x][j][1] == im[x][j][2] and im[x][j][2] == im[x][j][0]:
im[x][j][0] = 255
im[x][j][1] = 255
im[x][j][2] = 255
im_gray = rgb2gray(im)
im_gray = img_as_ubyte(im_gray)
im_gray = morphology.opening(im_gray, square(2))
im_gray_equalize = exposure.equalize_hist(im_gray)
threshold = filters.threshold_otsu(im_gray_equalize).copy()
threshold = im_gray_equalize < threshold
threshold = img_as_ubyte(threshold)
bw = morphology.closing(im_gray_equalize < threshold, square(3))
cleared = bw.copy()
im_th = cleared
ctrs, hier = cv2.findContours(img_as_ubyte(im_th.copy()), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rects = sorted(rects, key=lambda tup: tup[0])
if len(rects) != 5:
continue
for rect, l in zip(rects, lable):
# Draw the rectangles
cv2.rectangle(threshold, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1)
# Make the rectangular region around the digit
roi = threshold[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = morphology.closing(roi, square(4))
features.append(roi.ravel())
lables.append([l])
features = np.array(features, 'int16')
labels = np.array(lables, 'int').ravel()
# features, lables의 차원을 출력
print features.shape
print labels.shape
print "escape time : ", round(time()-t0, 3), "s"
t0 = time()
list_hog_fd = []
for feature in features:
fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
list_hog_fd.append(fd)
hog_features = np.array(list_hog_fd, 'float64')
print "escape time : ", round(time()-t0, 3), "s"
clf = joblib.load("./pkl/hog/skt/digits_SVC.pkl")
# Compute confusion matrix
cm = confusion_matrix(labels, clf.predict(hog_features))
np.set_printoptions(precision=2)
print('Confusion matrix, without normalization')
print(cm)
plt.figure()
plot_confusion_matrix(cm, labels=labels)
# Normalize the confusion matrix by row (i.e by the number of samples
# in each class)
# cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
# print('Normalized confusion matrix')
# print(cm_normalized)
# plt.figure()
# plot_confusion_matrix(cm_normalized, title='Normalized confusion matrix', labels=labels)
In [56]:
# Confusion matrix of KNeighbors on Plan3
# 새로운 200개의 이미지를 test 데이터로 만들자.
p = "./data_test"
md5list = glob(os.path.join(p, "*.png"))
md5list = [os.path.split(fname)[1] for fname in md5list]
print "the number of files is %s" %len(md5list)
features = []
lables = []
t0 = time()
# captcha를 preprossing후 mnist처럼 numpy array로 만들자
for fname in md5list:
lable = os.path.split(fname)[1].split("_")[1][:5]
im = io.imread(os.path.join(p, fname))
w, h, _ = im.shape
for x in range(w):
for j in range(h):
if im[x][j][0] == im[x][j][1] and im[x][j][1] == im[x][j][2] and im[x][j][2] == im[x][j][0]:
im[x][j][0] = 255
im[x][j][1] = 255
im[x][j][2] = 255
im_gray = rgb2gray(im)
im_gray = img_as_ubyte(im_gray)
im_gray = morphology.opening(im_gray, square(2))
im_gray_equalize = exposure.equalize_hist(im_gray)
threshold = filters.threshold_otsu(im_gray_equalize).copy()
threshold = im_gray_equalize < threshold
threshold = img_as_ubyte(threshold)
bw = morphology.closing(im_gray_equalize < threshold, square(3))
cleared = bw.copy()
im_th = cleared
ctrs, hier = cv2.findContours(img_as_ubyte(im_th.copy()), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rects = sorted(rects, key=lambda tup: tup[0])
if len(rects) != 5:
continue
for rect, l in zip(rects, lable):
# Draw the rectangles
cv2.rectangle(threshold, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 1)
# Make the rectangular region around the digit
roi = threshold[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi = morphology.closing(roi, square(4))
features.append(roi.ravel())
lables.append([l])
features = np.array(features, 'int16')
labels = np.array(lables, 'int').ravel()
# features, lables의 차원을 출력
print features.shape
print labels.shape
print "escape time : ", round(time()-t0, 3), "s"
t0 = time()
list_hog_fd = []
for feature in features:
fd = hog(feature.reshape((28, 28)), orientations=9, pixels_per_cell=(14, 14), cells_per_block=(1, 1), visualise=False)
list_hog_fd.append(fd)
hog_features = np.array(list_hog_fd, 'float64')
print "escape time : ", round(time()-t0, 3), "s"
clf = joblib.load("./pkl/hog/mnist/digits_KNeighborsClassifier.pkl")
# Compute confusion matrix
cm = confusion_matrix(labels, clf.predict(hog_features))
np.set_printoptions(precision=2)
print('Confusion matrix, without normalization')
print(cm)
plt.figure()
plot_confusion_matrix(cm, labels=labels)