【问题标题】:tensorflow classify multiple imagestensorflow 对多张图片进行分类
【发布时间】:2016-10-16 05:05:22
【问题描述】:

我正在使用 Tensorflow 图像分类示例 (https://www.tensorflow.org/versions/r0.9/tutorials/image_recognition/index.html)。 如何一次对多个图像进行分类?

编辑:理想情况下,我只需传入一个图像和一个数字 (nb) 作为参数,然后对该图像进行输入-待分类 nb 迭代

文件是classify_image.py,重要的部分是:

def run_inference_on_image(image):
"""Runs inference on an image.

Args:
image: Image file name.

Returns:
Nothing
"""
if not tf.gfile.Exists(image):
tf.logging.fatal('File does not exist %s', image)
image_data = tf.gfile.FastGFile(image, 'rb').read()

# Creates graph from saved GraphDef.
create_graph()

with tf.Session() as sess:
# Some useful tensors:
# 'softmax:0': A tensor containing the normalized prediction across
#   1000 labels.
# 'pool_3:0': A tensor containing the next-to-last layer containing 2048
#   float description of the image.
# 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
#   encoding of the image.
# Runs the softmax tensor by feeding the image_data as input to the graph.
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
predictions = sess.run(softmax_tensor,
                       {'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)

# Creates node ID --> English string lookup.
node_lookup = NodeLookup()

top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
for node_id in top_k:
  human_string = node_lookup.id_to_string(node_id)
  score = predictions[node_id]
  print('%s (score = %.5f)' % (human_string, score))

def main(_):
maybe_download_and_extract()
image = (FLAGS.image_file if FLAGS.image_file else
       os.path.join(FLAGS.model_dir, 'cropped_panda.jpg'))
run_inference_on_image(image)

【问题讨论】:

  • 到目前为止你的代码是什么?请将其添加到您的问题中。
  • 您所需要的应该很容易实现,但我们需要您阅读图像并调用您所显示内容的部分。
  • 不是在 main 中进行的调用,并且在前几行中完成了图像读取( image_data = tf.gfile.FastGFile(image, 'rb').read() )?跨度>

标签: python image tensorflow classification


【解决方案1】:

与您相关的代码将是此部分:

def main(_):
  maybe_download_and_extract()
  image = (FLAGS.image_file if FLAGS.image_file else
           os.path.join(FLAGS.model_dir, 'cropped_panda.jpg'))
  run_inference_on_image(image)

为了预测“images”文件夹中的所有 png、jpeg 或 jpg 文件,您可以这样做:

def main(_):
  maybe_download_and_extract()

  # search for files in 'images' dir
  files_dir = os.getcwd() + '/images'
  files = os.listdir(files_dir)

  # loop over files, print prediction if it is an image
  for f in files:
    if f.lower().endswith(('.png', '.jpg', '.jpeg')):
      image_path = files_dir + '/' + f
      print run_inference_on_image(image_path)

这应该打印出该文件夹中所有图像的预测

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 2022-12-06
    • 2020-02-09
    • 2018-11-07
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 2017-12-15
    • 2019-10-18
    相关资源
    最近更新 更多