test_img = glob.glob(test_dir + '*.jpg')
时间: 2024-04-20 22:26:21 浏览: 18
`glob.glob()` 是一个用于获取指定文件夹下所有符合特定模式的文件路径的函数。在这里,`test_dir + '*.jpg'` 是一个指定文件路径模式的字符串,它将匹配 `test_dir` 文件夹下所有以 `.jpg` 结尾的文件,并返回一个包含这些文件路径的列表 `test_img`。这样就可以方便地获取指定文件夹下所有符合条件的图片文件的路径了。
相关问题
test_img = glob.glob(test_dir + '*.jpg')之后如何修改列表长度为16
如果 `test_img` 列表的长度不足 16,您可以使用一些方法来增加列表的长度。以下是一种可能的方法:
```python
import glob
# Assuming test_dir is the directory path containing the images
test_img = glob.glob(test_dir + '*.jpg')
# Check if the length of test_img is less than 16
if len(test_img) < 16:
# Calculate the number of additional images needed
additional_images_needed = 16 - len(test_img)
# Repeat the existing images to fill up the additional slots
test_img += test_img[:additional_images_needed]
# Now test_img has a length of 16 or more
```
在上面的代码中,我们首先检查 `test_img` 列表的长度是否小于 16。如果是,则计算需要添加的额外图像数量,并使用切片将现有图像重复添加到列表末尾,直到列表长度达到 16 或更多。
请注意,这只是一种简单的实现方式,假设您希望重复使用现有的图像来填充列表。如果您有其他要求或想要使用不同的方式来增加列表长度,请根据您的需求进行相应的修改。
import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from PIL import Image import matplotlib.pyplot as plt import input_data import model import numpy as np import xlsxwriter num_threads = 4 def evaluate_one_image(): test_dir = 'data/test/' import glob import xlwt test_img = glob.glob(test_dir + '*.jpg') workbook = xlsxwriter.Workbook('formatting.xlsx') worksheet = workbook.add_worksheet('My Worksheet') for index,img in enumerate(test_img): image = Image.open(img) image = image.resize([208, 208]) image_array = np.array(image) with tf.Graph().as_default(): BATCH_SIZE = 1 N_CLASSES = 4 image = tf.cast(image_array, tf.float32) image = tf.image.per_image_standardization(image) image = tf.reshape(image, [1, 208, 208, 3]) logit = model.cnn_inference(image, BATCH_SIZE, N_CLASSES) logit = tf.nn.softmax(logit) x = tf.placeholder(tf.float32, shape=[208, 208, 3]) logs_train_dir = 'log/' saver = tf.train.Saver() with tf.Session() as sess: prediction = sess.run(logit, feed_dict={x: image_array}) max_index = np.argmax(prediction) workbook.close() if __name__ == '__main__': evaluate_one_image()改为多线程运算
要将代码改为多线程运算,您可以使用 TensorFlow 的 `tf.train.Coordinator()` 和 `tf.train.start_queue_runners()` 来管理和启动多个线程。以下是修改后的代码示例:
```python
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from PIL import Image
import matplotlib.pyplot as plt
import input_data
import model
import numpy as np
import xlsxwriter
num_threads = 4
def evaluate_one_image(image_array, index):
with tf.Graph().as_default():
BATCH_SIZE = 1
N_CLASSES = 4
image = tf.cast(image_array, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1, 208, 208, 3])
logit = model.cnn_inference(image, BATCH_SIZE, N_CLASSES)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[208, 208, 3])
logs_train_dir = 'log/'
saver = tf.train.Saver()
with tf.Session() as sess:
prediction = sess.run(logit, feed_dict={x: image_array})
max_index = np.argmax(prediction)
# 将结果写入 Excel 表格
workbook = xlsxwriter.Workbook('formatting.xlsx')
worksheet = workbook.add_worksheet('My Worksheet')
worksheet.write(index, 0, max_index)
workbook.close()
def evaluate_images(test_img):
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for index, img in enumerate(test_img):
image = Image.open(img)
image = image.resize([208, 208])
image_array = np.array(image)
# 在每个线程中调用 evaluate_one_image()
tf.compat.v1.threading.Thread(target=evaluate_one_image, args=(image_array, index)).start()
# 请求停止所有线程
coord.request_stop()
# 等待所有线程完成
coord.join(threads)
if __name__ == '__main__':
test_dir = 'data/test/'
import glob
import xlwt
test_img = glob.glob(test_dir + '*.jpg')
evaluate_images(test_img)
```
在修改后的代码中,我们首先定义了一个新的函数 `evaluate_one_image()`,它接受图像数组和索引作为参数,表示要评估的单个图像。在这个函数中,我们将使用传递的图像数组进行评估操作。
然后,在 `evaluate_images()` 函数中,我们使用 `tf.train.Coordinator()` 和 `tf.train.start_queue_runners()` 启动了多个线程。在每个线程中,我们使用 `tf.compat.v1.threading.Thread()` 创建一个新的线程,并在其中调用 `evaluate_one_image()` 函数来评估图像。
最后,在主函数中通过调用 `evaluate_images()` 函数来启动评估过程。
这样,您就可以利用多线程同时评估多个图像了。
希望对您有帮助!如果您还有其他问题,请随时提问。