Ask Your Question

Revision history [back]

PGM is an image format, just like JPEG, PNG, BMP or many others you probably know. OpenCV can read almost all of them (probably not GIF), so there's no limit in what files you can read.

Regarding your other problems: reading the documentation for any kind of software always helps a lot. I've tried to write as much documentation as possible, in order to answer exactely those kind of questions. So you have a good chance finding an answer in there. Some face recognition algorithms like the Eigenfaces and Fisherfaces require your images to be grayscale and of equal size, see the notes in the documentation:

  • http://docs.opencv.org/trunk/modules/contrib/doc/facerec/facerec_api.html#notes

This has algorithmic reasons and sorry, but there's simply no way to hide this kind of complexity from the user (or resizing the images to a default size).

So how do you preprocess the images to grayscale and equal size? It's already done within the script I have provided. First of all I read all images as grayscale, so no need to think about this one. And second, see the definition of read_images:

  • http://code.opencv.org/projects/opencv/repository/revisions/master/entry/samples/python2/facerec_demo.py#L55

Which is:

def read_images(path, sz=None):
    """Reads the images in a given folder, resizes images on the fly if size is given.

    Args:
        path: Path to a folder with subfolders representing the subjects (persons).
        sz: A tuple with the size Resizes 

    Returns:
        A list [X,y]

            X: The images, which is a Python list of numpy arrays.
            y: The corresponding labels (the unique number of the subject, person) in a Python list.
    """

That means if you pass the sz parameter, your images get resized to a given size while loading. So if you want to resize all of your images to 70 x 70 pixels, you would call the function with:

[X,y] = read_images(path=sys.argv[1],sz=(70,70))

And that's it.

PGM is an image format, just like JPEG, PNG, BMP or many others you probably know. OpenCV can read almost all of them (probably not GIF), so there's no limit in what files you can read.

Regarding your other problems: reading the documentation for any kind of software always helps a lot. I've tried to write as much documentation as possible, in order to answer exactely those kind of questions. So you have a good chance finding an answer in there. Some face recognition algorithms like the Eigenfaces and Fisherfaces require your images to be grayscale and of equal size, see the notes in the documentation:

  • http://docs.opencv.org/trunk/modules/contrib/doc/facerec/facerec_api.html#notes

This has algorithmic reasons and sorry, but there's simply no way to hide this kind of complexity from the user (or resizing the images to a default size).

So how do you preprocess the images to grayscale and equal size? It's already done within the script I have provided. First of all I read all images as grayscale, so no need to think about this one. And second, see the definition of read_images:

  • http://code.opencv.org/projects/opencv/repository/revisions/master/entry/samples/python2/facerec_demo.py#L55

Which is:

def read_images(path, sz=None):
    """Reads the images in a given folder, resizes images on the fly if size is given.

    Args:
        path: Path to a folder with subfolders representing the subjects (persons).
        sz: A tuple with the size Resizes 

    Returns:
        A list [X,y]

            X: The images, which is a Python list of numpy arrays.
            y: The corresponding labels (the unique number of the subject, person) in a Python list.
    """

That means if you pass the sz parameter, your images get resized to a given size while loading. So if you want to resize all of your images to 70 x 70 pixels, you would call the function with:

[X,y] = read_images(path=sys.argv[1],sz=(70,70))

And that's it.

Please note, I fixed a Python bug on 64bit machines with the latest commit. Thanks to Leo Dirac for reporting:

  • http://code.opencv.org/issues/2257#note-4