CNN 无法识别图像文件
CNN Cannot Identify Image File
我做了一个简单的 CNN 来识别三种鱼。我正在尝试使用 CNN 对未包含在训练或验证集中的图像进行分类。图片是 grunts-saltwater.jpg 并且在 Gdrive 上。以下是用于预测现有 CNN 模型的代码:
grunts_url = "https://drive.google.com/file/d/1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd/view?usp=sharing"
grunts_path = tf.keras.utils.get_file('grunts-saltwater', origin=grunts_url)
img = keras.preprocessing.image.load_img(
grunts_path, target_size=(img_height, img_width))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)
但是,我收到以下错误:
Downloading data from https://drive.google.com/file/d/1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd/view?usp=sharing
8192/Unknown - 0s 0us/step
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-217-d031443047e1> in <module>()
3
4 img = keras.preprocessing.image.load_img(
----> 5 grunts_path, target_size=(img_height, img_width))
6
7 img_array = keras.preprocessing.image.img_to_array(img)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/preprocessing/image.py in load_img(path, grayscale, color_mode, target_size, interpolation)
299 """
300 return image.load_img(path, grayscale=grayscale, color_mode=color_mode,
--> 301 target_size=target_size, interpolation=interpolation)
302
303
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
112 'The use of `load_img` requires PIL.')
113 with open(path, 'rb') as f:
--> 114 img = pil_image.open(io.BytesIO(f.read()))
115 if color_mode == 'grayscale':
116 # if image is not already an 8-bit, 16-bit or 32-bit grayscale image
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2860 warnings.warn(message)
2861 raise UnidentifiedImageError(
-> 2862 "cannot identify image file %r" % (filename if filename else fp)
2863 )
2864
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f9002c637d8>
你能帮忙解决这个问题吗?谢谢
我设法解决了这个问题。我不认为我指的是来源 URL 是正确的。这是一个有效的例子。
gruntfish_url = "https://upload.wikimedia.org/wikipedia/commons/5/54/Blue_Stripe_Grunt._Haemulon_sciurus.jpg"
gruntfish_path = tf.keras.utils.get_file('Grunt.', origin=gruntfish_url)
这是因为图像的 google 驱动器 link 现在是可下载的行而不是视图 link。如果它是共享的,那么你可以将它转换成可下载的 link 这样 http 客户端就可以下载它们。
隐藏到可下载线路
您的共享图片 ID 是 1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd
,因此请使用 link "https://docs.google.com/uc?export=download&id=1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd"
固定代码:
grunts_url = "https://docs.google.com/uc?export=download&id=1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd"
grunts_path = tf.keras.utils.get_file('grunts-saltwater', origin=grunts_url)
img = keras.preprocessing.image.load_img(grunts_path, target_size=(100, 100))
我做了一个简单的 CNN 来识别三种鱼。我正在尝试使用 CNN 对未包含在训练或验证集中的图像进行分类。图片是 grunts-saltwater.jpg 并且在 Gdrive 上。以下是用于预测现有 CNN 模型的代码:
grunts_url = "https://drive.google.com/file/d/1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd/view?usp=sharing"
grunts_path = tf.keras.utils.get_file('grunts-saltwater', origin=grunts_url)
img = keras.preprocessing.image.load_img(
grunts_path, target_size=(img_height, img_width))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)
但是,我收到以下错误:
Downloading data from https://drive.google.com/file/d/1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd/view?usp=sharing
8192/Unknown - 0s 0us/step
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-217-d031443047e1> in <module>()
3
4 img = keras.preprocessing.image.load_img(
----> 5 grunts_path, target_size=(img_height, img_width))
6
7 img_array = keras.preprocessing.image.img_to_array(img)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/preprocessing/image.py in load_img(path, grayscale, color_mode, target_size, interpolation)
299 """
300 return image.load_img(path, grayscale=grayscale, color_mode=color_mode,
--> 301 target_size=target_size, interpolation=interpolation)
302
303
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
112 'The use of `load_img` requires PIL.')
113 with open(path, 'rb') as f:
--> 114 img = pil_image.open(io.BytesIO(f.read()))
115 if color_mode == 'grayscale':
116 # if image is not already an 8-bit, 16-bit or 32-bit grayscale image
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2860 warnings.warn(message)
2861 raise UnidentifiedImageError(
-> 2862 "cannot identify image file %r" % (filename if filename else fp)
2863 )
2864
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f9002c637d8>
你能帮忙解决这个问题吗?谢谢
我设法解决了这个问题。我不认为我指的是来源 URL 是正确的。这是一个有效的例子。
gruntfish_url = "https://upload.wikimedia.org/wikipedia/commons/5/54/Blue_Stripe_Grunt._Haemulon_sciurus.jpg"
gruntfish_path = tf.keras.utils.get_file('Grunt.', origin=gruntfish_url)
这是因为图像的 google 驱动器 link 现在是可下载的行而不是视图 link。如果它是共享的,那么你可以将它转换成可下载的 link 这样 http 客户端就可以下载它们。
隐藏到可下载线路
您的共享图片 ID 是 1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd
,因此请使用 link "https://docs.google.com/uc?export=download&id=1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd"
固定代码:
grunts_url = "https://docs.google.com/uc?export=download&id=1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd"
grunts_path = tf.keras.utils.get_file('grunts-saltwater', origin=grunts_url)
img = keras.preprocessing.image.load_img(grunts_path, target_size=(100, 100))