概要
タイトル通り、PCで撮影した手書き文字を認識できるか試しました
実装
ライブラリの読み込み
import sys
import struct
import matplotlib.pyplot as plt
import numpy as np
import os
from sklearn.neighbors import KNeighborsClassifier
from skimage import io, color
MNIST読み込み
def load_mnist(path, kind='train'):
labels_path = os.path.join(path,'%s-labels.idx1-ubyte'% kind)
images_path = os.path.join(path,'%s-images.idx3-ubyte'% kind)
#images_path = "datasets/t10k-images.idx3-ubyte"
#labels_path = "datasets/t10k-labels.idx1-ubyte"
with open(labels_path, 'rb') as lbpath:
magic, n = struct.unpack('>II',lbpath.read(8))
labels = np.fromfile(lbpath,dtype=np.uint8)
with open(images_path, 'rb') as imgpath:
magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16))
images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)
return images, labels, rows, cols
学習用と検証用に分けます
train_images, train_labels, train_rows, train_cols= load_mnist("datasets",kind="train")
test_images, test_labels, test_rows, train_cols = load_mnist("datasets",kind="t10k")
x_train = train_images
y_train = train_labels
x_test = test_images
y_test = test_labels
学習
k = KNeighborsClassifier()
k.fit(x_train, y_train)
手書きのファイルを読み込み
img = io.imread('ファイル名')
# 画像を色の値にする
img_color = color.rgb2gray(img)
# 1行にする
img_color = img_color.reshape(1, -1)
手書きの文字が読み取れるか検証する
pred = k.predict(img_color)
pred
詳細な実装ファイルは以下です
https://github.com/haru-cpu/ai_learn/blob/main/load_MNIST.ipynb