-1

I am trying to import some images into python to train a ML Algorithm. Unfortunately when i try to show them, an erro occurs:

TypeError: Image data of dtype object cannot be converted to float

The code is the following:

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
from PIL import Image
from scipy import ndimage, misc

DATADIR = r"C:\Users\robda\Desktop\Università\Corsi Luiss\ML & AI\dog vs cat\Petimages"
CATEGORIES = ["Dog", "Cat"]

for category in CATEGORIES:  # do dogs and cats
    path = os.path.join(DATADIR,category)  # create path to dogs and cats
    for img in os.listdir(path):  # iterate over each image per dogs and cats
        img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)  # convert to array
        plt.imshow(img_array, cmap='gray')  # graph it
        plt.show()  # display!
        break  
    break  
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Rob
  • 25
  • 1
  • 7

1 Answers1

1

Is it important to resize them all?

That mostly depends on the size of each image, but in most cases, yes.

Do you recommend compressing all those images in a single file to import in python?

No, I don't recommend you doing that, as it doesn't sound necessary to squeeze 8000 images into a single file. There should be another way.

If yes, what kind of file/extension shall I use?

JPG is usually a good choice for large images, as it's small in size.

How can I practically do that?

You can use the pillow module, as shown here.

Red
  • 26,798
  • 7
  • 36
  • 58