Img_to_array返回空白值

时间:2019-07-08 04:12:15

标签: python-3.x keras

我有一个图像增强脚本,可用于对图像执行一些操作。但是Keras的img_to_array函数在调用时返回Blank值。

我制作了用于扩充的脚本,但它遇到了一些问题。

from numpy import expand_dims
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from glob import glob
import glob
import cv2
import os


def get_images(m):
    count = 0
    img_list = []
    images_path = glob.glob(m)

    out = "C:\\Python35\\augmented_images\\horizontal_shift\\"

    if not os.path.exists(out):
        os.makedirs(out, exist_ok=True)

    for folder in images_path:
        for f in glob.glob(folder+"/*.jpg"):
            img_list.append(f)
            print(img_list)

        for i in range(len(img_list)):
            img_base = os.path.basename(img_list[i])
            img_name = os.path.splitext(img_base)[0]
            img = load_img(img_list[i])
            print(img)
            data = img_to_array(img)
            samples = expand_dims(data, 0)
            datagen = ImageDataGenerator(width_shift_range=[-200, 200])
            it = datagen.flow(samples, batch_size=1)
            for i in range(9):
                batch = it.next()
                cv2.imwrite(out + img_name+"_%d.jpg" % count, batch)
                count += 1


folders = ("C:\\Python35\\augment_img_data\\*")
get_images(folders)

为什么img_to_array保留空白。需要更改哪些内容才能执行增强操作

1 个答案:

答案 0 :(得分:1)

尝试一下。我认为您不必扩大尺寸。

img = image.load_img(file, target_size=(224, 224))
img = image.img_to_array(img)
#x = resnet50.preprocess_input(img)
x = np.array([img])
feature = model.predict(x)

或这个

img = load_img(img_list[i])
print(img)
data = img_to_array(img)
samples = expand_dims(data, axis= 1)