首页 > 解决方案 > tensorflow.python.framework.errors_impl.InvalidArgumentError:未知的图像文件格式。需要 JPEG、PNG、GIF、BMP 之一

问题描述

我正在尝试使用 Tensorflow 2.x 检测对象。但是我在训练的时候遇到了这样的问题。我的数据是 .jpg 和 .jpeg 扩展名。Tensorflow 不训练带有 .jpg 扩展名的图片,你知道吗?你认为我可以如何解决这个错误?

标签: pythontensorflowartificial-intelligenceobject-detection

解决方案


张量确实适用于 jpg 扩展。我一直在使用它们。即使文件格式相同,我也不确定它是否会将 jpeg 作为扩展名除外,因此我使用 ImageDataGenerator 进行了尝试,它也可以正常工作。所以显然你有一些其他类型的问题。我建议您使用下面的代码遍历您的目录并检查文件是否是有效的图像文件并且具有 jpg 或 jpeg 扩展名

import os
import PIL
from PIL import Image
source_dir=r'c:\temp\spiders\storage'
source_list=os.listdir (source_dir)
for file in source_list:
    fpath=os.path.join(source_dir, file)
    try:
        img=Image.open(fpath)
        shape=img.size
        if file.endswith('jpeg') or file.endswith( 'jpg'):
            pass
        else:
            print ('file ', fpath, ' has an extension other than jpg or jpeg')
    except:
        print ('file ', fpath, ' is an invalid image file')
print ('***** process completed *****')

推荐阅读