Skip to main content
 首页 » 编程设计

python之将 TFRecords 转换回 JPEG 图像

2025年05月04日21pander-it

我有一个包含数百个 TFRecords 的文件。每个 TFRecord 文件包含 1,024 条记录。每条记录包含以下信息:

The Example proto contains the following fields: 
 
image/height: integer, image height in pixels 
image/width: integer, image width in pixels 
image/colorspace: string, specifying the colorspace, always 'RGB' 
image/channels: integer, specifying the number of channels, always 3 
image/class/label: integer, specifying the index in a normalized classification layer 
image/class/raw: integer, specifying the index in the raw (original) classification layer 
image/class/source: integer, specifying the index of the source (creator of the image) 
image/class/text: string, specifying the human-readable version of the normalized label 
image/format: string, specifying the format, always 'JPEG' 
image/filename: string containing the basename of the image file 
image/id: integer, specifying the unique id for the image 
image/encoded: string, containing JPEG encoded image in RGB colorspace 

我将这些 TFRecords 中的每一个都存储在目录路径/Data/train 中。 python 中是否有一种不太复杂的方法将 TFRecord 中的这些图像转换回 JPEG 格式并将它们保存到另一个目录/data/image.jpeg 中?我看过看起来很痛苦的 TensorFlow 文档,还有这个 script它将 TFRecord 转换为数组,但我遇到了问题。任何帮助、更正或反馈将不胜感激!谢谢。

我正在处理的数据是 MARCO 图像数据:

https://marco.ccr.buffalo.edu/download

请您参考如下方法:

这应该有效:

record_iterator = tf.python_io.tf_record_iterator(path_to_tfrecords_file) 
 
    for string_record in record_iterator: 
        example = tf.train.Example() 
        example.ParseFromString(string_record) 
 
        image = example.features.feature["encoded"].bytes_list.value[0] 
 
        # save image to file 
        # ...