解析用于创建eval.tfrecord的文件

时间:2019-07-10 21:34:32

标签: python tensorflow tfrecord

我正在尝试获取用于创建eval.tfrecord的.jpg文件。

我无法使用python打开文件并在文件上运行正则表达式来查找整个文件中随机列出的所有.jpg文件。

现在,我使用以下代码打开文件,并以结构化形式查看我需要的.jpg文件之一:

  feature {
    key: "image/source_id"
    value {
      bytes_list {
        value: "path/to/image.jpg"
      }
    }
  }

下面是获得上述结果的代码:

#From the Tensorflow Docs:

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
tf.enable_eager_execution()

import numpy as np
import IPython.display as display

record_iterator = tf.python_io.tf_record_iterator("eval.tfrecord")

for string_record in record_iterator:
  example = tf.train.Example()
  example.ParseFromString(string_record)

  print(example)

  # Exit after 1 iteration as this is purely demonstrative.
  break

我只想获取使用的.jpgs,而不是文件中的所有数据。也许有某种方式我可以使用键指定功能:image / source_id以获取创建tfrecord时使用的所有.jpg?

1 个答案:

答案 0 :(得分:0)

我将“ print(example)”的输出重定向到文件中。然后能够读取该文件,并使用正则表达式正常解析该文件以查找.jpg文件。