如何在Google Colab中链接图像pr文件

2024-09-25 00:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我没能显示图像, 代码下方,

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import cv2
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.optimizers import RMSprop

img= image.load_img("cnn_happy_notHappy/traing/happy/1.png")

错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-13-2c0e7bdac5f8> in <module>()
----> 1 img= image.load_img("cnn_happy_notHappy/traing/happy/1.png")

1 frames
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
    111         raise ImportError('Could not import PIL.Image. '
    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':

FileNotFoundError: [Errno 2] No such file or directory: 'cnn_happy_notHappy/traing/happy/1.png'

图像文件夹和文件所在的文件夹截图,Folder directory

请帮我解决这个问题,


Tags: from图像imageimportimgpngtensorflowas
1条回答
网友
1楼 · 发布于 2024-09-25 00:20:28

您可以先在google colab中导入图像,然后将其分配给img变量

# you can run this in first cell
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import cv2
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.optimizers import RMSprop
# you can run this is the second cell will show a button to upload the file in google collab
from google.colab import files
uploaded = files.upload()
# use this after image in uploaded in the previous cell successfullt
img= image.load_img("1.png")

相关问题 更多 >