0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MNISTで実際の手書き文字を認識させる

Posted at

概要

タイトル通り、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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?