Как пометить изображения после импорта их в python - PullRequest
0 голосов
/ 05 апреля 2019

У меня есть набор картинок с пометкой «собака, кошка, грузовик, самолет и машина» в папке.Как только я импортирую их в python, я хочу назначить им двоичные метки.Следующий код показывает, как я могу извлечь изображения из папки и сделать это для 1 класса, но как я могу сделать это для нескольких классов?Например, 1 для «собаки», 2 для «кошки», 3 для «грузовика», 4 для «самолета» и 5 для «машины».

Test_dir = "C:/Users/Instructor/Dropbox/Data Science/2.Temp_WORDFILES/test"
image_width = 32
image_height = 32

def read_images(directory, resize_to=(128, 128)):

"""This function extracts images from given
directory"""

    files = glob.glob(directory + "/*.jpg")
    images = []
    labels = []
    for f in tqdm.tqdm_notebook(files):
        im = Image.open(f)
        im = im.resize(resize_to)
        im = np.array(im) / 255.0
        im = im.astype("float32")
        images.append(im)

        label = 1 if "dog" in f.lower() else 0
        labels.append(label)

    return np.array(images), np.array(labels)

 X, y = read_images(directory=Test_dir, resize_to=(IM_WIDTH, IM_HEIGHT))

Ответы [ 2 ]

0 голосов
/ 05 апреля 2019

Определить словарь для сопоставления названия животного с ярлыком

animal_to_label = {'dog': 1,'cat': 2,'truck': 3,'airplane': 4,'car': 5 }

Test_dir = "C:/Users/Instructor/Dropbox/Data Science/2.Temp_WORDFILES/test"
image_width = 32
image_height = 32

def read_images(directory, resize_to=(128, 128)):

"""This function extracts images from given
directory"""

    files = glob.glob(directory + "/*.jpg")
    images = []
    labels = []
    switch_values = {'dog':1,'cat':2, 'truck':3, 'airplane':4 ,'car':5}
    for f in tqdm.tqdm_notebook(files):
        im = Image.open(f)
        im = im.resize(resize_to)
        im = np.array(im) / 255.0
        im = im.astype("float32")
        images.append(im)
        name = f.split("/")[-1].split(".")[0]
        label = animal_to_label[name.lower()]
        labels.append(label)

    return np.array(images), np.array(labels)

 X, y = read_images(directory=Test_dir, resize_to=(IM_WIDTH, IM_HEIGHT))
0 голосов
/ 05 апреля 2019
Test_dir = "C:/Users/Instructor/Dropbox/Data Science/2.Temp_WORD     FILES/test"
image_width = 32
image_height = 32

def read_images(directory, resize_to=(128, 128)):

    """This function extracts images from given
    directory"""

    files = glob.glob(directory + "/*.jpg")
    images = []
    labels = []
    switch_values = {'dog':1,'cat':2, 'truck':3, 'airplane':4 ,'car':5}
    for f in tqdm.tqdm_notebook(files):
        im = Image.open(f)
        im = im.resize(resize_to)
        im = np.array(im) / 255.0
        im = im.astype("float32")
        images.append(im)

        label = switch_values.get(f.lower())
        labels.append(label)

    return np.array(images), np.array(labels)

 X, y = read_images(directory=Test_dir, resize_to=(IM_WIDTH, IM_HEIGHT))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...