如何把下面的代码改成能测试一个文件夹下所有的图片?

1.代码是猫狗大战改写的,测试图片的代码是get_one_image,每次只能测试目录下随机一张图片,那么怎么才能让它测试“test”文件夹下所有的图片呢?Python新人对这一块的方法很模糊,求大佬们解答
2附上test代码

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 18 08:05:21 2019

@author: pc
"""
# 评估模型
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import input_data
import model
import training

def get_one_image(train):
    n = len(train)
    ind = np.random.randint(0, n)
    img_dir = train[ind]

    image = Image.open(img_dir)
    plt.imshow(image)
    plt.show()
    image = image.resize([224, 224])
    image = np.array(image)
    return image


def evaluate_one_image():
    train_dir = "D:\\python\\Anaconda\\envs\\tensorflow\\shuidao\\data\\train\\"
    train, train_label = input_data.get_files(train_dir)
    image_array = get_one_image(train)

    with tf.Graph().as_default():
        BATCH_SIZE = 1
        N_CLASSES = 4

        image = tf.cast(image_array, tf.float32)
        image = tf.reshape(image, [1, 224, 224, 3])
        logit = model.inference(image, BATCH_SIZE, N_CLASSES)
        logit = tf.nn.softmax(logit)

        x = tf.placeholder(tf.float32, shape=[224, 224, 3])

        logs_train_dir = "D:\\python\\Anaconda\\envs\\tensorflow\\shuidao\\logs_1\\"
        saver = tf.train.Saver()

        with tf.Session() as sess:
            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(logs_train_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split("/")[-1].split("-")[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print("Loading success, global_step is %s" % global_step)
            else:
                print("No checkpoint file found")

            prediction = sess.run(logit, feed_dict={x: image_array})
            max_index = np.argmax(prediction)
            if max_index == 0:
                print("This is daowen with possibility %.6f" % prediction[:, 0])
            elif max_index == 1:
                print("This is baiye with possibility %.6f" % prediction[:, 1])
            elif max_index == 2:
                print("This is wenku with possibility %.6f" % prediction[:, 2])
            elif max_index == 3:
                print("This is emiao with possibility %.6f" % prediction[:, 3])

training.run_training()
evaluate_one_image()

-*- coding: utf-8 -*-

"""
Created on Mon Mar 18 08:05:21 2019

@author: pc
"""

评估模型

from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import input_data
import model
import training
import time

def get_one_image(imag_dir):

image = Image.open(img_dir)
plt.imshow(image)
plt.show()
image = image.resize([224, 224])
image = np.array(image)
return image

def evaluate_one_image(img_dir):
image_array = get_one_image(img_dir)

with tf.Graph().as_default():
    BATCH_SIZE = 1
    N_CLASSES = 4

    image = tf.cast(image_array, tf.float32)
    image = tf.reshape(image, [1, 224, 224, 3])
    logit = model.inference(image, BATCH_SIZE, N_CLASSES)
    logit = tf.nn.softmax(logit)

    x = tf.placeholder(tf.float32, shape=[224, 224, 3])

    logs_train_dir = "D:\\python\\Anaconda\\envs\\tensorflow\\shuidao\\logs_1\\"
    saver = tf.train.Saver()

    with tf.Session() as sess:
        print("Reading checkpoints...")
        ckpt = tf.train.get_checkpoint_state(logs_train_dir)
        if ckpt and ckpt.model_checkpoint_path:
            global_step = ckpt.model_checkpoint_path.split("/")[-1].split("-")[-1]
            saver.restore(sess, ckpt.model_checkpoint_path)
            print("Loading success, global_step is %s" % global_step)
        else:
            print("No checkpoint file found")

        prediction = sess.run(logit, feed_dict={x: image_array})
        max_index = np.argmax(prediction)
        if max_index == 0:
            print("This is daowen with possibility %.6f" % prediction[:, 0])
        elif max_index == 1:
            print("This is baiye with possibility %.6f" % prediction[:, 1])
        elif max_index == 2:
            print("This is wenku with possibility %.6f" % prediction[:, 2])
        elif max_index == 3:
            print("This is emiao with possibility %.6f" % prediction[:, 3])

training.run_training()
#evaluate_one_image()
train_dir = "D:\python\Anaconda\envs\tensorflow\shuidao\data\train\"
train, train_label = input_data.get_files(train_dir)
for img_dir in train:
evaluate_one_image(img_dir)
time.sleep(60) #暂定一儿,便于查看结果
plt.colse()

这样试试,依次处理每个图片,但没有测试,但思路应该是没问题的。

要想同时处理多个图片的话,可以参考train时的数据处理方式,过程是一样的。