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)
-
1$\begingroup$ Two questions: 1) What is the format (.png, .jpeg, .pdf, etc) of your images? 2) How are the labels stored (is it the name of the folder they are in)? $\endgroup$– Bruno LubascherCommented Nov 30, 2018 at 19:38
-
$\begingroup$ 1)jpeg 2)yes, there are two classes stored in two different folders $\endgroup$– user254087Commented Dec 1, 2018 at 8:29
2 Answers
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
Image.open()
- Convert to
np.array()
- 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") ])