• 大小: 53.54MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2022-05-24
  • 语言: Python
  • 标签: tensorflow  mnist  python  

资源简介

博客:https://blog.csdn.net/weixin_45775701/article/details/109446515

资源截图

代码片段和文件信息

import os
import glob
import tensorflow as tf
import mnist_inference
import mnist_train
from PIL import Image ImageFilter
import matplotlib.pyplot as plt
import cv2



def image_prepare(pic_name):
    # 读取图像,第二个参数是读取方式
    img = cv2.imread(pic_name 1)
    # 使用全局阈值,降噪
    retth1 = cv2.threshold(img127255cv2.THRESH_BINARY)
    # 把opencv图像转化为PIL图像
    im = Image.fromarray(cv2.cvtColor(th1cv2.COLOR_BGR2RGB))
    # 灰度化
    im = im.convert(‘L‘)
    # 为图片重新指定尺寸
    im = im.resize((2828) Image.ANTIALIAS)
    # plt.imshow(im)
    # plt.show()
    # 图像转换为list
    im_list = list(im.getdata())
    # 图像灰度反转
    result = [(255-x)*1.0/255.0 for x in im_list]
    return result


def main(argv=None):
    # 把要识别的图片放到下面的文件夹中
    img_path = ‘picture/‘
    imgs = glob.glob(os.path.join(img_path ‘*‘))
    for p in imgs:
        # 图像处理:降噪、灰度化、修改尺寸以及灰度反转
        pic = image_prepare(p)
        # 识别图像
        evaluate(pic p)

def evaluate(pic pic_name):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32 [None mnist_inference.INPUT_NODE] name=‘x-input‘)
        y_ = tf.placeholder(tf.float32 [None mnist_inference.OUTPUT_NODE] name=‘y-input‘)
        validate_feed = {x:[pic]}
        y = mnist_inference.inference(x None)
        result = tf.argmax(y 1)
        variable_averages = tf.train.ExponentialMovingAverage(mnist_train.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_train.MODEL_SAVE_PATH)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess ckpt.model_checkpoint_path)
                global_step = ckpt.model_checkpoint_path.split(‘/‘)[-1].split(‘-‘)[-1]
                number = sess.run(result feed_dict=validate_feed)
                pic_name = pic_name.split(‘\\‘)[-1]
                print(pic_name‘ is :‘number[0])
            else:
                print(‘No checkpoint file found‘)
                return


if __name__ == ‘__main__‘:
    main()

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-11-03 13:06  Handwritten-Numeral-Recognition\
     目录           0  2020-11-02 12:07  Handwritten-Numeral-Recognition\MNIST_data\
     文件       60008  2020-11-01 10:36  Handwritten-Numeral-Recognition\MNIST_data\train-labels.idx1-ubyte
     目录           0  2020-11-03 18:18  __MACOSX\
     目录           0  2020-11-03 18:18  __MACOSX\Handwritten-Numeral-Recognition\
     目录           0  2020-11-03 18:18  __MACOSX\Handwritten-Numeral-Recognition\MNIST_data\
     文件         232  2020-11-01 10:36  __MACOSX\Handwritten-Numeral-Recognition\MNIST_data\._train-labels.idx1-ubyte
     文件    47040016  2020-11-01 11:30  Handwritten-Numeral-Recognition\MNIST_data\train-images.idx3-ubyte
     文件         232  2020-11-01 11:30  __MACOSX\Handwritten-Numeral-Recognition\MNIST_data\._train-images.idx3-ubyte
     文件       10008  2020-11-01 10:35  Handwritten-Numeral-Recognition\MNIST_data\t10k-labels.idx1-ubyte
     文件         232  2020-11-01 10:35  __MACOSX\Handwritten-Numeral-Recognition\MNIST_data\._t10k-labels.idx1-ubyte
     文件     1648877  2020-11-01 11:41  Handwritten-Numeral-Recognition\MNIST_data\t10k-images-idx3-ubyte.gz
     文件     7840016  2020-11-01 10:36  Handwritten-Numeral-Recognition\MNIST_data\t10k-images.idx3-ubyte
     文件         232  2020-11-01 10:36  __MACOSX\Handwritten-Numeral-Recognition\MNIST_data\._t10k-images.idx3-ubyte
     文件     9912422  2020-11-01 11:37  Handwritten-Numeral-Recognition\MNIST_data\train-images-idx3-ubyte.gz
     文件       28881  2020-11-01 11:37  Handwritten-Numeral-Recognition\MNIST_data\train-labels-idx1-ubyte.gz
     文件        4542  2020-11-01 11:45  Handwritten-Numeral-Recognition\MNIST_data\t10k-labels-idx1-ubyte.gz
     文件         176  2020-11-01 11:45  __MACOSX\Handwritten-Numeral-Recognition\MNIST_data\._t10k-labels-idx1-ubyte.gz
     文件        1232  2019-06-05 15:21  Handwritten-Numeral-Recognition\mnist_inference.py
     文件         274  2019-06-05 15:21  __MACOSX\Handwritten-Numeral-Recognition\._mnist_inference.py
     文件        2683  2020-11-01 11:44  Handwritten-Numeral-Recognition\mnist_train.py
     文件         274  2020-11-01 11:44  __MACOSX\Handwritten-Numeral-Recognition\._mnist_train.py
     文件       32248  2020-11-01 20:08  Handwritten-Numeral-Recognition\timg.jpeg
     文件         176  2020-11-01 20:08  __MACOSX\Handwritten-Numeral-Recognition\._timg.jpeg
     文件       10265  2020-11-03 12:37  Handwritten-Numeral-Recognition\as.png
     文件         233  2020-11-03 12:37  __MACOSX\Handwritten-Numeral-Recognition\._as.png
     文件       14340  2020-11-03 13:06  Handwritten-Numeral-Recognition\.DS_Store
     文件         120  2020-11-03 13:06  __MACOSX\Handwritten-Numeral-Recognition\._.DS_Store
     目录           0  2020-11-01 19:30  Handwritten-Numeral-Recognition\MNIST_model\
     文件     3180084  2020-11-01 19:29  Handwritten-Numeral-Recognition\MNIST_model\mnist_model-6001.data-00000-of-00001
     文件     3180084  2019-06-05 14:53  Handwritten-Numeral-Recognition\MNIST_model\mnist_model-28001.data-00000-of-00001
............此处省略116个文件信息

评论

共有 条评论