使用opencv在python中读取大量图像

时间:2018-04-22 07:14:34

标签: python python-3.x opencv

我正在使用机器学习模型制作食物分类程序。我有101个类的非常大的数据集,每个类有大约1000个图像但是使用如此大的数据集导致我内存不足因此我使用了较小的数据集,我发现分类器偏向于一个训练类。我使用cv2.imread()来读取图像,然后我使用scikit来训练和测试模型。我的代码如下

def image_to_feature_vector(image, size=(128, 128)):
    # resize the image to a fixed size, then flatten the image into
    # a list of raw pixel intensities
    return cv2.resize(image, size).flatten()

def extract_color_histogram(image, bins=(32, 32, 32)):
    # extract a 3D color histogram from the HSV color space using
    # the supplied number of `bins` per channel
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    hist = cv2.calcHist([hsv], [0, 1, 2], None, bins,
        [0, 180, 0, 256, 0, 256])

    # handle normalizing the histogram if we are using OpenCV 2.4.X
    if imutils.is_cv2():
        hist = cv2.normalize(hist)

    # otherwise, perform "in place" normalization in OpenCV 3
    else:
        cv2.normalize(hist, hist)

    # return the flattened histogram as the feature vector
    return hist.flatten()



class_names=[]
read_images = []
# initialize the raw pixel intensities matrix, the features matrix,
# and labels list
rawImages = []
features = []
folders = glob.glob('E:\\food\\images\\*')
imagenames__list = []
for folder in folders:
    for f in glob.glob(folder+'/*.jpg'):
        imagenames__list.append(f)
        image=cv2.imread(f)
        #plt.imshow(image)
        a=os.path.basename(folder)
        class_names.append(a)


        pixels = image_to_feature_vector(image)
        hist = extract_color_histogram(image)# add the messages we got to the raw images, features matricies
        rawImages.append(pixels)
        features.append(hist)



print(class_names)
print(f)            
rawImages = np.array(rawImages)
print(len(rawImages))
features = np.array(features)
print(len(rawImages))
class_names = np.array(class_names)

您能否建议对代码进行任何更改,以便我可以使用完整的数据集。请指出此方法是否完全错误以及如何更改它。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容来使用训练过程中的所有图像。此代码适用于灰度图像。您必须根据您的要求进行相关更改。

def load_train(train_path, image_size, classes):
    images = []
    labels = []
    img_names = []
    cls = []

    print('Going to read training images')
    for fields in classes:
        index = classes.index(fields)
        print('Now going to read {} files (Index: {})'.format(fields, index))
        path = os.path.join(train_path, fields, '*g')
        files = glob.glob(path)
        for fl in files:
            image = cv2.imread(fl,0)
            image = cv2.resize(image, (image_size, image_size), 0, 0, cv2.INTER_LINEAR)
            image = image.astype(np.float32)
            image = np.multiply(image, 1.0 / 255.0)
            image1 = image.reshape(50,50,1)
            images.append(image1)
            label = np.zeros(len(classes))
            label[index] = 1.0
            labels.append(label)
            flbase = os.path.basename(fl)
            img_names.append(flbase)
            cls.append(fields)
    images = np.array(images)
    labels = np.array(labels)
    img_names = np.array(img_names)
    cls = np.array(cls)

    return images, labels, img_names, cls