简体   繁体   中英

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. 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.

Now I am having an issue while creating a proper dataset that can be fed into the SVM model. 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

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

This site has a detailed explanation on how to work with image dataset for SVMs. Have a Look https://medium.com/@dataturks/understanding-svms-for-image-classification-cf4f01232700

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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