I have a folder with a lot of images that I want to use to bild a classificator using a SVM model in python with sklearn. I've always used csv file as train/test set with sklearn, how can I make it? (a csv file with records corrisponding to images and a variable for every pixel)
You are describing a one-time pre-processing step that will crawl through your folder and turn each image into a line of data and then save the entire collection in a CSV file. In turn, that file becomes your gold standard dataset.
If I was in your position, I would look into the Keras pre-processing tools that already provide python libraries to quickly do this task. It's a common need for image processing, the Keras library is very mature and can do this for you.
It should be something like this:
- Read image with
- Convert to
- Flat the previous 3D array (height x width x channels) into 1D array
- Collect all the 1D arrays into list
- Convert list into np.array, resulting in 2D array (images x pixels)
Note: the code below is not tested
import glob import PIL import numpy data = np.array([ np.array(PIL.Image.open(f).convert("RGB")).ravel() for f in glob.glob("./folder/*.jpeg") ])