首页 > 解决方案 > ValueError:使用 PIL 加载图像时设置带有序列的数组元素

问题描述

我在 data = np.array(data,dtype="float32") 行中遇到错误。如果我不使用 PIL.Image.open 而是使用 tensforflow.keras 的 load_img() ,它工作正常。有人可以帮忙解决我需要做哪些改变才能使用 Image.open() 并让它工作吗?该代码基于https://www.pyimagesearch.com/2020/05/04/covid-19-face-mask-detector-with-opencv-keras-tensorflow-and-deep-learning/

EPOCHS = 20
BS = 32

# grab the list of images in our dataset directory, then initialize
# the list of data (i.e., images) and class images
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset"]))


data = []
labels = []
size = 224,224
# loop over the image paths
for imagePath in imagePaths:
    # extract the class label from the filename
    label = imagePath.split(os.path.sep)[-2]

    image =Image.open(imagePath)
    image = image.convert('RGB')
    image.thumbnail(size, Image.ANTIALIAS)
    #image = load_img(imagePath, target_size=(224, 224))
    image = img_to_array(image)
    image = preprocess_input(image)

# update the data and labels lists, respectively
    data.append(image)
    labels.append(label)
    
# load the input image (224x224) and preprocess it


# convert the data and labels to NumPy arrays
data = np.array(data, dtype="float32")
labels = np.array(labels)



标签: pythonpython-3.xnumpytensorflowpython-imaging-library

解决方案


推荐阅读