100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 3.4输入手写数字图片输出识别结果

3.4输入手写数字图片输出识别结果

时间:2023-02-27 04:05:56

相关推荐

3.4输入手写数字图片输出识别结果

前一段时间忙于csgo,也roll了几个小皮肤,沉迷于csgo中,没有学习,有点颓。

今天呢,又跟老师抬杠,抬杠的时候着实很爽,很舒服。害,到头来还是面对现实。

言归正传,我们想要将数字图片喂给神经网络,让神经网络去识别图片上的数字,以验证模型训练的准确性

我们准备了10张数字图片,如下图所示

由前面训练模型,可知我们训练的图片都是28*28像素的黑底白字图片,所以我们得对图片进行预处理

def pre_pic(picName):#对图片进行处理img = Image.open(picName)reIm = img.resize((28,28), Image.ANTIALIAS)#消除图片的一种锯齿差im_arr = np.array(reIm.convert('L'))#把图片变成灰度图,以满足程序的需求threshold = 50for i in range(28):#因为输入的图片是黑底白字,要求的是白底黑字,所以对每一个像素进行反转for j in range(28):im_arr[i][j] = 255 - im_arr[i][j]if (im_arr[i][j] < threshold):im_arr[i][j] = 0else: im_arr[i][j] = 255nm_arr = im_arr.reshape([1, 784])nm_arr = nm_arr.astype(np.float32)img_ready = np.multiply(nm_arr, 1.0/255.0)return img_ready

处理完喂给训练好的神经网络模型

def restore_model(testPicArr):with tf.Graph().as_default() as tg:x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])y = mnist_forward.forward(x, None)preValue = tf.argmax(y, 1)variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)variables_to_restore = variable_averages.variables_to_restore()saver = tf.train.Saver(variables_to_restore)with tf.Session() as sess:ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)if ckpt and ckpt.model_checkpoint_path:saver.restore(sess, ckpt.model_checkpoint_path)preValue = sess.run(preValue, feed_dict={x:testPicArr})return preValueelse:print("No checkpoint file found")return -1

然后输出识别结果,全部代码如下

#coding:utf-8import tensorflow as tfimport numpy as npfrom PIL import Imageimport mnist_backwardimport mnist_forwardimport osos.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息 os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Errordef restore_model(testPicArr):with tf.Graph().as_default() as tg:x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])y = mnist_forward.forward(x, None)preValue = tf.argmax(y, 1)variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)variables_to_restore = variable_averages.variables_to_restore()saver = tf.train.Saver(variables_to_restore)with tf.Session() as sess:ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)if ckpt and ckpt.model_checkpoint_path:saver.restore(sess, ckpt.model_checkpoint_path)preValue = sess.run(preValue, feed_dict={x:testPicArr})return preValueelse:print("No checkpoint file found")return -1def pre_pic(picName):#对图片进行处理img = Image.open(picName)reIm = img.resize((28,28), Image.ANTIALIAS)#消除图片的一种锯齿差im_arr = np.array(reIm.convert('L'))#把图片变成灰度图,以满足程序的需求threshold = 50for i in range(28):#因为输入的图片是黑底白字,要求的是白底黑字,所以对每一个像素进行反转for j in range(28):im_arr[i][j] = 255 - im_arr[i][j]if (im_arr[i][j] < threshold):im_arr[i][j] = 0else: im_arr[i][j] = 255nm_arr = im_arr.reshape([1, 784])nm_arr = nm_arr.astype(np.float32)img_ready = np.multiply(nm_arr, 1.0/255.0)return img_readydef application():testNum = input("input the number of test pictures:")for i in range(int(testNum)):testPic = input("the path of test picture:")testPicArr = pre_pic(testPic)preValue = restore_model(testPicArr)print( "The prediction number is:", preValue)def main():application()if __name__ == '__main__':main()

为了实验的方便,图片直接以数字大小命名

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。