110

自己动手做一个识别手写数字的web应用02

 6 years ago
source link: http://mp.weixin.qq.com/s/MZLViUfKdeaFi1Xxc69_SQ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

自己动手做一个识别手写数字的web应用02

Original shadow 无界社区mixlab 2017-11-06 09:42 Posted on

继续上文。

自己动手做一个识别手写数字的web应用01

再次进入docker容器

接着上一篇文章,我们继续使用上次新建好的容器,可以终端输入 :

docker ps -a

Image

如上图,找到上次run的容器,我这边是容器名(NAMES)为suspicious_cori,启动它,可以终端输入:

docker start  suspicious_cori

然后,终端再输入:

docker exec -i -t  suspicious_cori bash

即可在容器中开启一个交互模式的终端。

jupyter notebook

新建一个notebook

加载训练好的模型

加载上一篇训练好的模型,在新建的notebook里输入:

from keras.models import model_from_json

model=model_from_json(open('my_model_architecture.json').read())  

model.load_weights('my_model_weights.h5')

读取需要识别的手写字图片

引入用于读取图片的库:

import matplotlib.image as mpimg

读取位于kerasStudy目录下的图片:

img = mpimg.imread('test.png') 

matplotlib只支持PNG图像,读取和代码处于同一目录下的 test.png ,注意,读取后的img 就已经是一个 np.array 了,并且已经归一化处理。

上文的png图片是单通道图片(灰度),如果test.png是rgb通道的图片,可以rgb2gray进行转化,代码如下:

def rgb2gray(rgb):

    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

img = rgb2gray(img)

关于图片的通道,我们可以在photoshop里直观的查看:

Image

先查看下读取的图片数组维度:

print(img.shape)

输出是(28, 28)

转化成正确的输入格式:

img = img.reshape(1, 784)

打印出来看看:

print(img.shape)

输出是(1, 784)

识别的手写字图片

pre=model.predict_classes(img)   

打印出来即可:

print(pre)

识别出来是6:

1/1 [==============================] - 0s
[6]

至此,你已经学会了从训练模型到使用模型进行识别任务的全过程啦。

有兴趣可以试着替换其他的手写字图片进行识别看看。

当然也可以写个后端服务,部署成web应用。

关于第2部分Keras加载训练好的模型及预测的源代码文件,可以在后台留言,索取。


码字不易,开启新的打赏方式:

Image

本公众号定期更新关于

设计师、程序员发挥创意

互相融合的指南、作品。

主要技术栈:

nodejs、react native、electron

Elasticsearch

Solidity

Keras

欢迎关注,转发~

欢迎长按二维码

关注本号

Image

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK