简体   繁体   English

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

[英]preprocessing images for svm model in python

I am a beginner in machine learning and I'm trying to make an SVM image classifier using python.我是机器学习的初学者,我正在尝试使用 python 制作 SVM 图像分类器。 I have my own dataset of images.我有自己的图像数据集。 I have done the following steps: 1) created different folders for each class(binary class) 2) Imported all the images into my jupyter notebook.我已完成以下步骤:1)为每个类(二进制类)创建不同的文件夹 2)将所有图像导入我的 jupyter notebook。

Now I am having an issue while creating a proper dataset that can be fed into the SVM model.现在我在创建可以输入 SVM 模型的适当数据集时遇到问题。 I tried to append the image array and its class into a list called dataset.我尝试将图像数组及其类附加到名为数据集的列表中。 But now I am unable to flatten the images as a vector.但是现在我无法将图像展平为矢量。

Please tell me if my steps are right?请告诉我我的步骤是否正确? If right, then what should I do to flatten the images properly.如果正确,那么我应该怎么做才能正确地展平图像。

#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])

I want this data to be in the form which can be fed into the classification model.我希望这些数据采用可以输入分类模型的形式。

If cv2.imread() returns a numpy array you can use image.ravel() , alternatively (for a general data structure) itertools chain would do, add this import statement 如果cv2.imread()返回一个numpy数组,则可以使用image.ravel() ,或者(对于常规数据结构)itertools链也可以,添加此import语句

from itertools import chain

and than you can do 比你能做的

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

to get the image as a flat list or 将图像作为平面列表或

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

to get a numpy array 得到一个numpy的数组

This site has a detailed explanation on how to work with image dataset for SVMs. 该站点详细介绍了如何使用SVM的图像数据集。 Have a Look https://medium.com/@dataturks/understanding-svms-for-image-classification-cf4f01232700 看看https://medium.com/@dataturks/understanding-svms-for-image-classification-cf4f01232700

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

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