繁体   English   中英

在python中预处理svm模型的图像

[英]preprocessing images for svm model in python

我是机器学习的初学者,我正在尝试使用 python 制作 SVM 图像分类器。 我有自己的图像数据集。 我已完成以下步骤:1)为每个类(二进制类)创建不同的文件夹 2)将所有图像导入我的 jupyter notebook。

现在我在创建可以输入 SVM 模型的适当数据集时遇到问题。 我尝试将图像数组及其类附加到名为数据集的列表中。 但是现在我无法将图像展平为矢量。

请告诉我我的步骤是否正确? 如果正确,那么我应该怎么做才能正确地展平图像。

#path to the base dir
base_dir = "/home/khyati/projects/plant_project/try/dataset"

#path of various folders
apple_path = os.path.join(base_dir, "Apple___Apple_scab")
tomato_path = os.path.join(base_dir, "Tomato___Late_blight")

#list of available labels
classes = ["Apple___Apple_scab", "Tomato___Late_blight"]

dataset = []
for category in classes:
    path = os.path.join(base_dir, category)
    for img in os.listdir(path):
        #`enter code here`print(img)
        image = cv2.imread(os.path.join(path,img))
        label =classes.index(category)
        dataset.append([image,label])
print(dataset[1])

我希望这些数据采用可以输入分类模型的形式。

如果cv2.imread()返回一个numpy数组,则可以使用image.ravel() ,或者(对于常规数据结构)itertools链也可以,添加此import语句

from itertools import chain

比你能做的

dataset.append([list(chain(*image),label])

将图像作为平面列表或

dataset.append([np.fromiter(chain(*image),label])

得到一个numpy的数组

该站点详细介绍了如何使用SVM的图像数据集。 看看https://medium.com/@dataturks/understanding-svms-for-image-classification-cf4f01232700

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM