Agent00
Agent00

Reputation: 11

Image File Import Failed

I just switched from Mac to Windows and in my corrent project i'm coding a ML programm to evaluate pictures.

I labeled them in an Excel file and the import of that works fine. However, when i import my pictures to put them into a tensor, it doesn't work. I listed all the pictures to make sure i use the correct path and it seems to work fine. I even changed the name of the to imported file, so that it suites the one in my dic. I'm really struggeling to find my mistake and hope, that somebody here can help me to solve this problem!

import os
os.getcwd()
a='C:/Users/sunja/Documents/Daten/Bilddaten_zugeschnitten'
os.listdir(a)

'._1_2_18.jpg',

'._1_2_19.jpg',

'._1_2_20.jpg',

'._1_2_21.jpg',

'._1_2_22.jpg',

'._1_2_23.jpg',

'._1_2_24.jpg',

and so on

import os
import shutil
import numpy as np
import pandas as pd
from sklearn.utils import shuffle
from openpyxl import load_workbook
import random
random.seed(40)

import numpy as np
np.random.seed(40)  
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist


path_labels = 'C:/Users/sunja/Documents/Daten/Labels.xlsx'


#import label data and construct label data frame
workbook = load_workbook(path_labels)
features = []
labels = []
for row in range (2, workbook['Tabelle1' ].max_row+1):
        cell_Bezeichnung = workbook ['Tabelle1']["{}{}".format('A', row)     ].value
        cell_Label = workbook['Tabelle1']["{}{}".format("B",row)].value

        features.append('._'+str(cell_Bezeichnung)+'.jpg')
        labels.append(str(cell_Label))

    data = pd.DataFrame(data={'Datei': features, 'Label': labels})
    data = shuffle(data)
    data = data.reset_index(drop=True)
    print(data.head())



    Datei Label

0 ._1_2_22.jpg 2

1 ._1_1_22.jpg 1

2 ._1_0_07.jpg 0

3 ._1_1_16.jpg 1

4 ._1_1_25.jpg 1

Using TensorFlow backend.

This is the working part. Now when i import the pictures from the folder, it says, that they don't excist. Why is that so?

from keras.preprocessing import image                  
from tqdm import tqdm
os.environ['KMP_DUPLICATE_LIB_OK']='True'
path_images='C:/Users/sunja/Documents/Daten/Bilddaten_zugeschnitten'
def path_to_tensor(img_path):
    # loads RGB image as PIL.Image.Image type
    img_path = path_images+img_path

    img = image.load_img(img_path.item(0), target_size=(256, 256))     #Variation mit mehr als 100x100 pixel
    #tf.image.rgb_to_grayscale(img,name=None)

    # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
    x = image.img_to_array(img)
    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
    return np.expand_dims(x, axis=0)


def paths_to_tensor(img_paths):
    list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
    return np.vstack(list_of_tensors)

    #ImageFile.LOAD_TRUNCATED_IMAGES = True
from IPython.display import display
from PIL import Image

# pre-process the data for Keras
tensors = paths_to_tensor(files.values).astype('float32')/255

It doesn't even start the import, but stops at the first file.

FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/sunja/Documents/Daten/Bilddaten_zugeschnitten._1_2_22.jpg'

Am I missing something?? Thanks in advance!

Upvotes: 0

Views: 144

Answers (2)

Agent00
Agent00

Reputation: 11

I found the solution. I seemed to bo a PIL bug. The version 5.3 is bugging, so i downgraded to 5.2. Its working now! Thank you!

Upvotes: 0

Supratim Haldar
Supratim Haldar

Reputation: 2416

I suppose a "/" is missing between your directory path and filename. Assuming you are passing the filename in img_path argument of path_to_tensor() function, try replacing img_path = path_images+img_path with,

img_path = os.path.join(path_images, img_path)

or

img_path = path_images+'/'+img_path

Upvotes: 3

Related Questions