如何在Tensorflow decode_jpeg中确定哪个JPEG图片已损坏

时间:2018-04-18 03:06:34

标签: tensorflow

当我解码JPEG格式图片时,它会给出消息" 损坏的jpeg数据过早结束数据段"。我怎样才能确定哪张图片已损坏。 这是我的代码:

import os
import tensorflow as tf

directory = 'D:\\tfrecord\\read'
directories = []
class_names = []

photo_filenames = []

for filename in os.listdir(directory):
    path = os.path.join(directory, filename)
    photo_filenames.append(path)

with tf.Session() as sess:
    print("session")
    init = tf.global_variables_initializer()
    sess.run(init)
    for filename in photo_filenames:
        filecontents = tf.read_file(filename)
        image = tf.image.decode_jpeg(filecontents, channels = 3)
        try:
            sess.run(image)
        except Exception as e:
            print(filename)
            print (e)

1 个答案:

答案 0 :(得分:0)

插入

image = tf.Print(image,[filename]) 

之后

image = tf.image.decode_jpeg(filecontents, channels = 3)

文件名将在“损坏的jpeg数据...”之后显示。