我的程序如何找到与程序在同一文件夹中的文件以使用它?

问题描述 投票:0回答:0

我的 python 程序正在思考,文件 image.jpg 不存在或不在正确的目录中。 我已经尝试给出更明确的显示它在哪里。但它只是说它不存在。这是代码:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2

# Laden des gespeicherten Modells
model = tf.keras.models.load_model('my_augmented_fashion_mnist_model/my_model.h5')

# Vorhersage für ein benutzerdefiniertes Bild machen
image_path = 'image.jpg'
image = mpimg.imread(image_path)
image = cv2.resize(image, (28, 28))
image = image / 255.0

# BGR zu RGB Konvertierung
image = np.flip(image, axis=-1)

prediction = model.predict(np.array([image]))
predicted_class = np.argmax(prediction[0])

# Konvertieren der Vorhersage in Klassennamen
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
predicted_class_name = class_names[predicted_class]

# Ausgabe der Vorhersage
print("Das Bild zeigt ein ", predicted_class_name)

我得到这里的错误:

 File "c:\Users\user\Desktop\path\Fashion Test KI.py", line 12, in <module>
    image = mpimg.imread(image_path)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\image.py", line 1563, in imread
    with img_open(fname) as image:
         ^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL\Image.py", line 3227, in open
    fp = builtins.open(filename, "rb")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'image.jpg'

图片是这样的: just a pullover in the jpg format 我在这里用时尚 mnist 系列制作了一个 ai,因为我想试试 ai 是如何工作的。

我期望我会得到一个显示图片ai准确性的答案。 我已经将格式和库从 cv2 更改为 matplotlib。并更改了几次目录。

python tensorflow matplotlib directory cv2
© www.soinside.com 2019 - 2024. All rights reserved.