python - 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)
解决方案
推荐阅读
- oracle - 索引嵌套循环连接忽略 SQL 语句
- sql - SQL查询比较不相邻行的日期间隔?
- python - 如何在 Python 中修复我的重新编译语句
- java - 无法获取“https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/4.0.1/gradle-4.0.1.pom”
- reactjs - 检查作为参数传递的现有数组项的数组
- vb.net - 将 VB 网络中的数据插入 Postgresql
- go - 恐慌()堆栈跟踪不显示函数参数
- git - Git 使用 rebase 重写历史
- python - Python Selenium 访问内部元素
- java - Removing lock on non-graceful application shutdown