How to handle "ValueError: Could not find a format to read the specified file in single-image mode"?
How to handle "ValueError: Could not find a format to read the specified file in single-image mode"?
源代码link:https://github.com/rndbrtrnd/udacity-deep-learning/blob/master/1_notmnist.ipynb
我正在使用一些已经编写的现有代码来读取图像数据,作为深度学习课程的一部分,使用 ndimage.imread 已被删除,我将其更改为 imageio.imread 以继续。但我仍然面临一些问题(我认为这与不良图像数据有关)。
使用ndimage.imread(折旧)读取图像数据的初始代码。此代码不起作用,因为 ndimage 从 SciPy.
的 1.2.0 版本中删除
try:
image_data = (ndimage.imread(image_file).astype(float) -
pixel_depth / 2) / pixel_depth
if image_data.shape != (image_size, image_size):
raise Exception('Unexpected image shape: %s' % str(image_data.shape))
dataset[image_index, :, :] = image_data
image_index += 1
except IOError as e:
print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
我修改它使用imageio.imread
try:
# read the image and normalize it
image_data = (imageio.imread(image_file).astype(float) - pixel_depth/2)/pixel_depth
if image_data.shape!= (image_size,image_size):
raise Exception('Unexpected images shape: %s' % str(image_data.shape))
dataset[image_index,:,:] = image_data
image_index+=1
except IOError as e:
print('could not read the',image_file ,':',e,'hence skipping it.')
问题描述:
使用 imageio.imread 时,会抛出以下错误:
ValueError: Could not find a format to read the specified file in single-image mode
我花了一整天的时间想办法解决这个问题,结果发现这个解决方案比我想象的要简单。
解决方法:
我能够通过捕获 ValueError 异常并继续读取图像文件来解决这个问题。
代码如下:
image_size = 28
pixel_depth = 255.0
def load_letter(folder,min_num_images):
# read all the images name from the folder
image_files = os.listdir(folder)
# create a 3D data array
dataset = np.ndarray(shape=.
(len(image_files),image_size,image_size),dtype=np.float32)
image_index = 0
print(folder)
# read images from name from folder
for image in image_files:
# create the path to the file
image_file = os.path.join(folder,image)
try:
# read the image and normalize it
image_data = (imageio.imread(image_file).astype(float) - pixel_depth/2)/pixel_depth
if image_data.shape!= (image_size,image_size):
raise Exception('Unexpected images shape: %s' % str(image_data.shape))
# store it in 3D data set
dataset[image_index,:,:] = image_data
image_index+=1
except (IOError,ValueError) as e:
print('could not read the',image_file ,':',e,'hence skipping it.')
输出:
继续输出和读取文件时打印的错误文件名。
Pickling notMNIST_large/A.pickle.
notMNIST_large/A
could not read the notMNIST_large/A/RnJlaWdodERpc3BCb29rSXRhbGljLnR0Zg==.png :
Could not find a format to read the specified file in single-image mode hence
skipping it.
could not read the notMNIST_large/A/Um9tYW5hIEJvbGQucGZi.png :
Could not find a format to read the specified file in single-image mode hence skipping it.
could not read the notMNIST_large/A/SG90IE11c3RhcmQgQlROIFBvc3Rlci50dGY=.png :
Could not find a format to read the specified file in single-image mode hence skipping it.
源代码link:https://github.com/rndbrtrnd/udacity-deep-learning/blob/master/1_notmnist.ipynb
我正在使用一些已经编写的现有代码来读取图像数据,作为深度学习课程的一部分,使用 ndimage.imread 已被删除,我将其更改为 imageio.imread 以继续。但我仍然面临一些问题(我认为这与不良图像数据有关)。
使用ndimage.imread(折旧)读取图像数据的初始代码。此代码不起作用,因为 ndimage 从 SciPy.
的 1.2.0 版本中删除try:
image_data = (ndimage.imread(image_file).astype(float) -
pixel_depth / 2) / pixel_depth
if image_data.shape != (image_size, image_size):
raise Exception('Unexpected image shape: %s' % str(image_data.shape))
dataset[image_index, :, :] = image_data
image_index += 1
except IOError as e:
print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
我修改它使用imageio.imread
try:
# read the image and normalize it
image_data = (imageio.imread(image_file).astype(float) - pixel_depth/2)/pixel_depth
if image_data.shape!= (image_size,image_size):
raise Exception('Unexpected images shape: %s' % str(image_data.shape))
dataset[image_index,:,:] = image_data
image_index+=1
except IOError as e:
print('could not read the',image_file ,':',e,'hence skipping it.')
问题描述: 使用 imageio.imread 时,会抛出以下错误:
ValueError: Could not find a format to read the specified file in single-image mode
我花了一整天的时间想办法解决这个问题,结果发现这个解决方案比我想象的要简单。
解决方法: 我能够通过捕获 ValueError 异常并继续读取图像文件来解决这个问题。
代码如下:
image_size = 28
pixel_depth = 255.0
def load_letter(folder,min_num_images):
# read all the images name from the folder
image_files = os.listdir(folder)
# create a 3D data array
dataset = np.ndarray(shape=.
(len(image_files),image_size,image_size),dtype=np.float32)
image_index = 0
print(folder)
# read images from name from folder
for image in image_files:
# create the path to the file
image_file = os.path.join(folder,image)
try:
# read the image and normalize it
image_data = (imageio.imread(image_file).astype(float) - pixel_depth/2)/pixel_depth
if image_data.shape!= (image_size,image_size):
raise Exception('Unexpected images shape: %s' % str(image_data.shape))
# store it in 3D data set
dataset[image_index,:,:] = image_data
image_index+=1
except (IOError,ValueError) as e:
print('could not read the',image_file ,':',e,'hence skipping it.')
输出: 继续输出和读取文件时打印的错误文件名。
Pickling notMNIST_large/A.pickle.
notMNIST_large/A
could not read the notMNIST_large/A/RnJlaWdodERpc3BCb29rSXRhbGljLnR0Zg==.png :
Could not find a format to read the specified file in single-image mode hence
skipping it.
could not read the notMNIST_large/A/Um9tYW5hIEJvbGQucGZi.png :
Could not find a format to read the specified file in single-image mode hence skipping it.
could not read the notMNIST_large/A/SG90IE11c3RhcmQgQlROIFBvc3Rlci50dGY=.png :
Could not find a format to read the specified file in single-image mode hence skipping it.