Skip to content

Commit

Permalink
预测验证码
Browse files Browse the repository at this point in the history
  • Loading branch information
makelove committed Aug 8, 2017
1 parent 2bf5051 commit 7eb8da3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
8 changes: 6 additions & 2 deletions ch46-机器学习-K近邻/2-使用kNN对手写数字OCR.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
accuracy = correct * 100.0 / result.size
print('准确率', accuracy) # 准确率91.76%

''''''

# save the data
np.savez('knn_data.npz', train=train, train_labels=train_labels,test=test,test_labels=test_labels)

# Now load the data
with np.load('knn_data.npz') as data:
print(data.files)
Expand All @@ -56,5 +57,8 @@


#TODO 怎样预测数字?
retval, results=knn.predict(test[3:5])
retval, results=knn.predict(test[1003:1005])
# Docstring: predict(samples[, results[, flags]]) -> retval, results
print(retval, results)#(4.0, array([[ 4.],[ 4.]], dtype=float32))
#对比
cv2.imwrite('test[1005].jpg',test[1005].reshape((20,20)))
Binary file added ch46-机器学习-K近邻/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ch46-机器学习-K近邻/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ch46-机器学习-K近邻/knn_data.npz
Binary file not shown.
Binary file added ch46-机器学习-K近邻/test[1005].jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 23 additions & 1 deletion ch46-机器学习-K近邻/预测手写数字1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@
test = data['test']
test_labels = data['test_labels']

print('加载KNN,数据')
knn = cv2.ml.KNearest_create()
knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)
ret, result, neighbours, dist = knn.findNearest(test, k=5)
ret, result, neighbours, dist = knn.findNearest(test, k=5)

# 加载相片
print('加载相片')
img2 = cv2.imread('2.png', 0)
gray2 = cv2.resize(img2, (20, 20))
# gray2=gray2.reshape((400,))
gray21 = gray2.reshape((-1, 400)).astype(np.float32)

img6 = cv2.imread('6.png', 0)
gray6 = cv2.resize(img6, (20, 20))
# gray2=gray2.reshape((400,))
gray61 = gray6.reshape((-1, 400)).astype(np.float32)

g2 = np.append(gray21, gray61)
g3 = g2.reshape((2, 400))

# 预测
retval, results = knn.predict(g3)
print(retval, results) # 不准确
# (0.0, array([[ 0.],
# [ 5.]], dtype=float32))

0 comments on commit 7eb8da3

Please sign in to comment.