python - tensorflow.python.framework.errors_impl.InvalidArgumentError:未知的图像文件格式。需要 JPEG、PNG、GIF、BMP 之一
问题描述
我正在尝试使用 Tensorflow 2.x 检测对象。但是我在训练的时候遇到了这样的问题。我的数据是 .jpg 和 .jpeg 扩展名。Tensorflow 不训练带有 .jpg 扩展名的图片,你知道吗?你认为我可以如何解决这个错误?
解决方案
张量确实适用于 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 *****')
推荐阅读
- xslt - XSLT - 计算总计的最简单方法
- cluster-computing - 为什么建议创建节点数为奇数的集群
- c - C中的“控制到达非空函数的结尾”
- typescript - 类型 'any' 不能分配给类型 'never'
- powershell - 我的 New-RandomPassword 功能突然不起作用
- node.js - 为什么我无法从运行在端口 3000 上的 React 应用程序访问运行在端口 3001 上的节点服务器?
- joomla3.9 - 模板头中不包含 Joomla 模块样式和脚本
- sublimetext3 - Sublime Text 3 在打字时使用大量 CPU,但在空闲模式下大约 1%
- java - 以生产就绪方式执行 bazel-buildfarm,无需“bazel run”
- string - 如何将新的字符串元素添加到字符串列表中?