In [27]:
from PIL import Image, ImageFont, ImageDraw
im = Image.open('./realcap/captcha (4).gif')
%matplotlib inline
In [28]:
im
Out[28]:
In [29]:
from zheye import util
In [30]:
im = util.CenterExtend(im, radius=20)
In [31]:
vec = util.Img2Vec(im).copy()
In [32]:
Y = []
for i in range(vec.shape[0]):
for j in range(vec.shape[1]):
if vec[i][j] <= 150:
Y.append([i, j])
In [33]:
import numpy as np
import itertools
from scipy import linalg
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn import mixture
In [34]:
import time
start_time = time.time()
ini = np.array([[64, 60 ],[64, 100],[64, 140],[64, 180],[64, 220],[64, 260],[64, 300]])
gmm = mixture.GaussianMixture(n_components=7, covariance_type='tied', reg_covar=1e2, tol=1e3, n_init=9)
gmm.fit(Y)
print("--- %s seconds ---" % (time.time() - start_time))
In [35]:
gmm.means_
Out[35]:
In [36]:
centers = gmm.means_.copy()
In [37]:
centers = centers[np.argsort(centers[:, 1])]
In [38]:
util.PaintPoint(im.convert("RGB"), centers)
Out[38]:
In [62]:
import keras
model = keras.models.load_model('zheye/zheyeV5.keras')
In [42]:
from matplotlib.pyplot import imshow
import matplotlib.pyplot as plt
In [55]:
index = 2
import time
start_time = time.time()
a = [[0]*3 for i in range(3) ]
for i in range(3):
for j in range(3):
x = -2 + i + centers[index][0]
y = -2 + j + centers[index][1]
cr = util.crop(im, x, y, radius=20)
cr = cr.resize((40, 40), Image.ANTIALIAS)
#X = np.asarray(cr.convert('1'), dtype='float')
X = np.asarray(cr.convert('L'), dtype='float64')
X[X <= 150] = -1
X[X > 150] = 1
x0 = np.expand_dims(X, axis=0)
x1 = np.expand_dims(x0, axis=3)
m_y = model.predict(x1)
a[i][j] = m_y[0][0]
print("--- %s seconds ---" % (time.time() - start_time))
In [56]:
cr
Out[56]:
In [57]:
score = np.array(a)
In [60]:
score
Out[60]:
In [47]:
score[score > 0.5]
Out[47]:
In [48]:
plt.ion()
for i in range(7):
x = centers[i][0]
y = centers[i][1]
cr = util.crop(im, x, y, radius=20)
cr = cr.resize((40, 40), Image.ANTIALIAS)
X = np.asarray(cr.convert('L'), dtype='float64')
mm = Image.fromarray(X)
plt.imshow(mm, cmap='hot')
plt.show()
X[X <= 150] = -1
X[X > 150] = 1
x0 = np.expand_dims(X, axis=0)
x1 = np.expand_dims(x0, axis=3)
m_y = model.predict(x1)
print(m_y[0])
In [68]:
testT_X = np.load('./testT_X.npy')
testT_Y = np.load('./testT_Y.npy')
In [69]:
score = model.evaluate(testT_X, testT_Y, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])