Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

face recognition with opencv and python

I'm trying to implement the code showed here http://answers.opencv.org/answers/1051/revisions/ to do face recognition. The code is as follows:

!/usr/bin/env python

Software License Agreement (BSD License)

#

Copyright (c) 2012, Philipp Wagner <bytefish[at]gmx[dot]de>.

All rights reserved.

#

Redistribution and use in source and binary forms, with or without

modification, are permitted provided that the following conditions

are met:

#

* Redistributions of source code must retain the above copyright

notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above

copyright notice, this list of conditions and the following

disclaimer in the documentation and/or other materials provided

with the distribution.

* Neither the name of the author nor the names of its

contributors may be used to endorse or promote products derived

from this software without specific prior written permission.

#

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS

"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT

LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS

FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE

COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,

INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,

BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;

LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT

LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN

ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE

POSSIBILITY OF SUCH DAMAGE.

import os import sys import cv2 import numpy as np

def normalize(X, low, high, dtype=None): """Normalizes a given array in X to a value between low and high.""" X = np.asarray(X) minX, maxX = np.min(X), np.max(X) # normalize to [0...1].
X = X - float(minX) X = X / float((maxX - minX)) # scale to [low...high]. X = X * (high-low) X = X + low if dtype is None: return np.asarray(X) return np.asarray(X, dtype=dtype)

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.
"""
c = 0
X,y = [], []
for dirname, dirnames, filenames in os.walk(path):
    for subdirname in dirnames:
        subject_path = os.path.join(dirname, subdirname)
        for filename in os.listdir(subject_path):
            try:
                im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
                # resize to given size (if given)
                if (sz is not None):
                    im = cv2.resize(im, sz)
                X.append(np.asarray(im, dtype=np.uint8))
                y.append(c)
            except IOError, (errno, strerror):
                print "I/O error({0}): {1}".format(errno, strerror)
            except:
                print "Unexpected error:", sys.exc_info()[0]
                raise
        c = c+1
return [X,y]

if __name__ == "__main__": # This is where we write the images, if an output_dir is given # in command line: out_dir = resultado # You'll need at least a path to your image data, please see # the tutorial coming with this source code on how to prepare # your image data: if len(sys.argv) < 2: print "USAGE: facerec_demo.py </images> [</resultado>]" sys.exit() # Now read in the image data. This must be a valid path! [X,y] = read_images(sys.argv[1]) # A line like: # [X,y] = read_images(path=sys.argv[1],sz=(70,70)) # # Will resize all images to 70x70 pixels. This is necessary for some # algorithms (Eigenfaces, Fisherfaces), so apply it if the size of # your images differ. # # Convert labels to 32bit integers. This is a workaround for 64bit machines, # because the labels will truncated else. This will be fixed in code as # soon as possible, so Python users don't need to know about this. # Thanks to Leo Dirac for reporting: y = np.asarray(y, dtype=np.int32) # If a out_dir is given, set it: if len(sys.argv) == 3: out_dir = sys.argv[2] # Create the Eigenfaces model. We are going to use the default # parameters for this simple example, please read the documentation # for thresholding: model = cv2.createFisherFaceRecognizer() # Read # Learn the model. Remember our function returns Python lists, # so we use np.asarray to turn them into NumPy lists to make # the OpenCV wrapper happy: model.train(np.asarray(X), np.asarray(y)) # We now get a prediction from the model! In reality you # should always use unseen images for testing your model. # But so many people were confused, when I sliced an image # off in the C++ version, so I am just using an image we # have trained with. # # model.predict is going to return the predicted label and # the associated confidence: [p_label, p_confidence] = model.predict(np.asarray(X[0])) # Print it: print "Predicted label = %d (confidence=%.2f)" % (p_label, p_confidence) # Cool! Finally we'll plot the Eigenfaces, because that's # what most people read in the papers are keen to see. # # Just like in C++ you have access to all model internal # data, because the cv::FaceRecognizer is a cv::Algorithm. # # You can see the available parameters with getParams(): print model.getParams() # Now let's get some data: mean = model.getMat("mean") eigenvectors = model.getMat("eigenvectors") cv2.imwrite("test.png", X[0]) # We'll save the mean, by first normalizing it: mean_norm = normalize(mean, 0, 255, dtype=np.uint8) mean_resized = mean_norm.reshape(X[0].shape) if out_dir is None: cv2.imshow("mean", mean_resized) else: cv2.imwrite("%s/mean.png" % (out_dir), mean_resized) # Turn the first (at most) 16 eigenvectors into grayscale # images. You could also use cv::normalize here, but sticking # to NumPy is much easier for now. # Note: eigenvectors are stored by column: for i in xrange(len(np.unique(y))-1): eigenvector_i = eigenvectors[:,i].reshape(X[0].shape) eigenvector_i_norm = normalize(eigenvector_i, 0, 255, dtype=np.uint8) eigenvector_i_colormap = cv2.applyColorMap(eigenvector_i_norm, cv2.COLORMAP_JET) # Show or save the images: if out_dir is None: cv2.imshow("%s/fisherface_%d" % (out_dir,i), eigenvector_i_colormap) else: cv2.imwrite("%s/fisherface_%d.png" % (out_dir,i), eigenvector_i_colormap) # Show the images: if out_dir is None: cv2.waitKey(0)

I have two folders, images and resultado, inside images I have folders person1 and person2, inside them I have pictures. first question: can the type of my image data be .jpg? second question: in this line [X,y] = read_images(path=sys.argv[1],sz=(70,70)) what is sys.argv[1]? Should I replace it by "images" because my data is there? When I try to run the program I find the erro: File "facerec_demo.py", line 103 [X,y] = read_images(path=sys.argv[1],sz=(70,70)) ^ IndentationError: unexpected indent

third question: I want my program take a image and compare with the data. Do this line do that?cv2.imwrite("test.png", X[0]).

face recognition with opencv and python

I'm trying to implement the code showed here http://answers.opencv.org/answers/1051/revisions/ to do face recognition. The code is as follows:

!/usr/bin/env python

#!/usr/bin/env python
# Software License Agreement (BSD License)

#

License) # # Copyright (c) 2012, Philipp Wagner <bytefish[at]gmx[dot]de>.

<bytefish[at]gmx[dot]de>. # All rights reserved.

#

reserved. # # Redistribution and use in source and binary forms, with or without

without # modification, are permitted provided that the following conditions

conditions # are met:

#

met: # # * Redistributions of source code must retain the above copyright

copyright # notice, this list of conditions and the following disclaimer.

disclaimer. # * Redistributions in binary form must reproduce the above

above # copyright notice, this list of conditions and the following

following # disclaimer in the documentation and/or other materials provided

provided # with the distribution.

distribution. # * Neither the name of the author nor the names of its

its # contributors may be used to endorse or promote products derived

derived # from this software without specific prior written permission.

#

permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS

CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT

NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS

FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE

THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,

INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,

(INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;

SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT

STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN

IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE

THE # POSSIBILITY OF SUCH DAMAGE.

DAMAGE. import os import sys import cv2 import numpy as np

np def normalize(X, low, high, dtype=None): """Normalizes a given array in X to a value between low and high.""" X = np.asarray(X) minX, maxX = np.min(X), np.max(X) # normalize to [0...1].
X = X - float(minX) X = X / float((maxX - minX)) # scale to [low...high]. X = X * (high-low) X = X + low if dtype is None: return np.asarray(X) return np.asarray(X, dtype=dtype)

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

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.
 """
 c = 0
 X,y = [], []
 for dirname, dirnames, filenames in os.walk(path):
     for subdirname in dirnames:
         subject_path = os.path.join(dirname, subdirname)
         for filename in os.listdir(subject_path):
             try:
                 im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
                 # resize to given size (if given)
                 if (sz is not None):
                     im = cv2.resize(im, sz)
                 X.append(np.asarray(im, dtype=np.uint8))
                 y.append(c)
             except IOError, (errno, strerror):
                 print "I/O error({0}): {1}".format(errno, strerror)
             except:
                 print "Unexpected error:", sys.exc_info()[0]
                 raise
         c = c+1
 return [X,y]

if __name__ == "__main__": # This is where we write the images, if an output_dir is given # in command line: out_dir = resultado # You'll need at least a path to your image data, please see # the tutorial coming with this source code on how to prepare # your image data: if len(sys.argv) < 2: print "USAGE: facerec_demo.py </images> [</resultado>]" sys.exit() # Now read in the image data. This must be a valid path! [X,y] = read_images(sys.argv[1]) # A line like: # [X,y] = read_images(path=sys.argv[1],sz=(70,70)) # # Will resize all images to 70x70 pixels. This is necessary for some # algorithms (Eigenfaces, Fisherfaces), so apply it if the size of # your images differ. # # Convert labels to 32bit integers. This is a workaround for 64bit machines, # because the labels will truncated else. This will be fixed in code as # soon as possible, so Python users don't need to know about this. # Thanks to Leo Dirac for reporting: y = np.asarray(y, dtype=np.int32) # If a out_dir is given, set it: if len(sys.argv) == 3: out_dir = sys.argv[2] # Create the Eigenfaces model. We are going to use the default # parameters for this simple example, please read the documentation # for thresholding: model = cv2.createFisherFaceRecognizer() # Read # Learn the model. Remember our function returns Python lists, # so we use np.asarray to turn them into NumPy lists to make # the OpenCV wrapper happy: model.train(np.asarray(X), np.asarray(y)) # We now get a prediction from the model! In reality you # should always use unseen images for testing your model. # But so many people were confused, when I sliced an image # off in the C++ version, so I am just using an image we # have trained with. # # model.predict is going to return the predicted label and # the associated confidence: [p_label, p_confidence] = model.predict(np.asarray(X[0])) # Print it: print "Predicted label = %d (confidence=%.2f)" % (p_label, p_confidence) # Cool! Finally we'll plot the Eigenfaces, because that's # what most people read in the papers are keen to see. # # Just like in C++ you have access to all model internal # data, because the cv::FaceRecognizer is a cv::Algorithm. # # You can see the available parameters with getParams(): print model.getParams() # Now let's get some data: mean = model.getMat("mean") eigenvectors = model.getMat("eigenvectors") cv2.imwrite("test.png", X[0]) # We'll save the mean, by first normalizing it: mean_norm = normalize(mean, 0, 255, dtype=np.uint8) mean_resized = mean_norm.reshape(X[0].shape) if out_dir is None: cv2.imshow("mean", mean_resized) else: cv2.imwrite("%s/mean.png" % (out_dir), mean_resized) # Turn the first (at most) 16 eigenvectors into grayscale # images. You could also use cv::normalize here, but sticking # to NumPy is much easier for now. # Note: eigenvectors are stored by column: for i in xrange(len(np.unique(y))-1): eigenvector_i = eigenvectors[:,i].reshape(X[0].shape) eigenvector_i_norm = normalize(eigenvector_i, 0, 255, dtype=np.uint8) eigenvector_i_colormap = cv2.applyColorMap(eigenvector_i_norm, cv2.COLORMAP_JET) # Show or save the images: if out_dir is None: cv2.imshow("%s/fisherface_%d" % (out_dir,i), eigenvector_i_colormap) else: cv2.imwrite("%s/fisherface_%d.png" % (out_dir,i), eigenvector_i_colormap) # Show the images: if out_dir is None: cv2.waitKey(0)

cv2.waitKey(0)

I have two folders, images and resultado, inside images I have folders person1 and person2, inside them I have pictures. first question: can the type of my image data be .jpg? second question: in this line [X,y] = read_images(path=sys.argv[1],sz=(70,70)) what is sys.argv[1]? Should I replace it by "images" because my data is there? When I try to run the program I find the erro: File "facerec_demo.py", line 103 [X,y] = read_images(path=sys.argv[1],sz=(70,70)) ^ IndentationError: unexpected indent

third question: I want my program take a image and compare with the data. Do this line do that?cv2.imwrite("test.png", X[0]).

click to hide/show revision 3
No.3 Revision

updated 2015-05-22 07:52:30 -0600

berak gravatar image

face recognition with opencv and python

I'm trying to implement use the code showed here http://answers.opencv.org/answers/1051/revisions/ to do face recognition. The code is as follows:

#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2012, Philipp Wagner <bytefish[at]gmx[dot]de>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of the author nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.


import os
import sys
import cv2
import numpy as np

def normalize(X, low, high, dtype=None):
    """Normalizes a given array in X to a value between low and high."""
    X = np.asarray(X)
    minX, maxX = np.min(X), np.max(X)
    # normalize to [0...1].    
    X = X - float(minX)
    X = X / float((maxX - minX))
    # scale to [low...high].
    X = X * (high-low)
    X = X + low
    if dtype is None:
        return np.asarray(X)
    return np.asarray(X, dtype=dtype)


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.
    """
    c = 0
    X,y = [], []
    for dirname, dirnames, filenames in os.walk(path):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                try:
                    im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
                    # resize to given size (if given)
                    if (sz is not None):
                        im = cv2.resize(im, sz)
                    X.append(np.asarray(im, dtype=np.uint8))
                    y.append(c)
                except IOError, (errno, strerror):
                    print "I/O error({0}): {1}".format(errno, strerror)
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    raise
            c = c+1
    return [X,y]

if __name__ == "__main__":
    # This is where we write the images, if an output_dir is given
    # in command line:
    out_dir = resultado
    # You'll need at least a path to your image data, please see
    # the tutorial coming with this source code on how to prepare
    # your image data:
    if len(sys.argv) < 2:
        print "USAGE: facerec_demo.py </images> [</resultado>]"
        sys.exit()
    # Now read in the image data. This must be a valid path!
    [X,y] = read_images(sys.argv[1])
    # A line like:
    #
         [X,y] = read_images(path=sys.argv[1],sz=(70,70))
    #
    # Will resize all images to 70x70 pixels. This is necessary for some
    # algorithms (Eigenfaces, Fisherfaces), so apply it if the size of 
    # your images differ.
    #
    # Convert labels to 32bit integers. This is a workaround for 64bit machines,
    # because the labels will truncated else. This will be fixed in code as 
    # soon as possible, so Python users don't need to know about this.
    # Thanks to Leo Dirac for reporting:
    y = np.asarray(y, dtype=np.int32)
    # If a out_dir is given, set it:
    if len(sys.argv) == 3:
        out_dir = sys.argv[2]
    # Create the Eigenfaces model. We are going to use the default
    # parameters for this simple example, please read the documentation
    # for thresholding:
    model = cv2.createFisherFaceRecognizer()
    # Read
    # Learn the model. Remember our function returns Python lists,
    # so we use np.asarray to turn them into NumPy lists to make
    # the OpenCV wrapper happy:
    model.train(np.asarray(X), np.asarray(y))
    # We now get a prediction from the model! In reality you 
    # should always use unseen images for testing your model. 
    # But so many people were confused, when I sliced an image 
    # off in the C++ version, so I am just using an image we 
    # have trained with.
    #
    # model.predict is going to return the predicted label and
    # the associated confidence:
    [p_label, p_confidence] = model.predict(np.asarray(X[0]))
    # Print it:
    print "Predicted label = %d (confidence=%.2f)" % (p_label, p_confidence)
    # Cool! Finally we'll plot the Eigenfaces, because that's 
    # what most people read in the papers are keen to see.
    #
    # Just like in C++ you have access to all model internal
    # data, because the cv::FaceRecognizer is a cv::Algorithm.
    #
    # You can see the available parameters with getParams():
    print model.getParams()
    # Now let's get some data:
    mean = model.getMat("mean")
    eigenvectors = model.getMat("eigenvectors")
    cv2.imwrite("test.png", X[0])
    # We'll save the mean, by first normalizing it:
    mean_norm = normalize(mean, 0, 255, dtype=np.uint8)
    mean_resized = mean_norm.reshape(X[0].shape)
    if out_dir is None:
        cv2.imshow("mean", mean_resized)
    else:
        cv2.imwrite("%s/mean.png" % (out_dir), mean_resized)
    # Turn the first (at most) 16 eigenvectors into grayscale 
    # images. You could also use cv::normalize here, but sticking
    # to NumPy is much easier for now. 
    # Note: eigenvectors are stored by column:
    for i in xrange(len(np.unique(y))-1):
        eigenvector_i = eigenvectors[:,i].reshape(X[0].shape)
        eigenvector_i_norm = normalize(eigenvector_i, 0, 255, dtype=np.uint8)
        eigenvector_i_colormap = cv2.applyColorMap(eigenvector_i_norm, cv2.COLORMAP_JET)
        # Show or save the images:
        if out_dir is None:
            cv2.imshow("%s/fisherface_%d" % (out_dir,i), eigenvector_i_colormap)
        else:
            cv2.imwrite("%s/fisherface_%d.png" % (out_dir,i), eigenvector_i_colormap)
    # Show the images:
    if out_dir is None:
        cv2.waitKey(0)

I have two folders, images and resultado, inside images I have folders person1 and person2, inside them I have pictures. pictures.

  • first question: can the type of my image data be .jpg? .jpg?
  • second question: in this line [X,y] = read_images(path=sys.argv[1],sz=(70,70)) what is sys.argv[1]? Should I replace it by "images" because my data is there? When I try to run the program I find the erro: error:

    File "facerec_demo.py", line 103 [X,y] = read_images(path=sys.argv[1],sz=(70,70)) ^ IndentationError: unexpected indent

  • third question: I want my program take a image and compare with the data. Do this line do that?cv2.imwrite("test.png", X[0]).

The code is as follows:

#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2012, Philipp Wagner <bytefish[at]gmx[dot]de>.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of the author nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.


import os
import sys
import cv2
import numpy as np

def normalize(X, low, high, dtype=None):
    """Normalizes a given array in X to a value between low and high."""
    X = np.asarray(X)
    minX, maxX = np.min(X), np.max(X)
    # normalize to [0...1].    
    X = X - float(minX)
    X = X / float((maxX - minX))
    # scale to [low...high].
    X = X * (high-low)
    X = X + low
    if dtype is None:
        return np.asarray(X)
    return np.asarray(X, dtype=dtype)


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.
    """
    c = 0
    X,y = [], []
    for dirname, dirnames, filenames in os.walk(path):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                try:
                    im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
                    # resize to given size (if given)
                    if (sz is not None):
                        im = cv2.resize(im, sz)
                    X.append(np.asarray(im, dtype=np.uint8))
                    y.append(c)
                except IOError, (errno, strerror):
                    print "I/O error({0}): {1}".format(errno, strerror)
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    raise
            c = c+1
    return [X,y]

if __name__ == "__main__":
    # This is where we write the images, if an output_dir is given
    # in command line:
    out_dir = resultado
    # You'll need at least a path to your image data, please see
    # the tutorial coming with this source code on how to prepare
    # your image data:
    if len(sys.argv) < 2:
        print "USAGE: facerec_demo.py </images> [</resultado>]"
        sys.exit()
    # Now read in the image data. This must be a valid path!
    [X,y] = read_images(sys.argv[1])
    # A line like:
    #
         [X,y] = read_images(path=sys.argv[1],sz=(70,70))
    #
    # Will resize all images to 70x70 pixels. This is necessary for some
    # algorithms (Eigenfaces, Fisherfaces), so apply it if the size of 
    # your images differ.
    #
    # Convert labels to 32bit integers. This is a workaround for 64bit machines,
    # because the labels will truncated else. This will be fixed in code as 
    # soon as possible, so Python users don't need to know about this.
    # Thanks to Leo Dirac for reporting:
    y = np.asarray(y, dtype=np.int32)
    # If a out_dir is given, set it:
    if len(sys.argv) == 3:
        out_dir = sys.argv[2]
    # Create the Eigenfaces model. We are going to use the default
    # parameters for this simple example, please read the documentation
    # for thresholding:
    model = cv2.createFisherFaceRecognizer()
    # Read
    # Learn the model. Remember our function returns Python lists,
    # so we use np.asarray to turn them into NumPy lists to make
    # the OpenCV wrapper happy:
    model.train(np.asarray(X), np.asarray(y))
    # We now get a prediction from the model! In reality you 
    # should always use unseen images for testing your model. 
    # But so many people were confused, when I sliced an image 
    # off in the C++ version, so I am just using an image we 
    # have trained with.
    #
    # model.predict is going to return the predicted label and
    # the associated confidence:
    [p_label, p_confidence] = model.predict(np.asarray(X[0]))
    # Print it:
    print "Predicted label = %d (confidence=%.2f)" % (p_label, p_confidence)
    # Cool! Finally we'll plot the Eigenfaces, because that's 
    # what most people read in the papers are keen to see.
    #
    # Just like in C++ you have access to all model internal
    # data, because the cv::FaceRecognizer is a cv::Algorithm.
    #
    # You can see the available parameters with getParams():
    print model.getParams()
    # Now let's get some data:
    mean = model.getMat("mean")
    eigenvectors = model.getMat("eigenvectors")
    cv2.imwrite("test.png", X[0])
    # We'll save the mean, by first normalizing it:
    mean_norm = normalize(mean, 0, 255, dtype=np.uint8)
    mean_resized = mean_norm.reshape(X[0].shape)
    if out_dir is None:
        cv2.imshow("mean", mean_resized)
    else:
        cv2.imwrite("%s/mean.png" % (out_dir), mean_resized)
    # Turn the first (at most) 16 eigenvectors into grayscale 
    # images. You could also use cv::normalize here, but sticking
    # to NumPy is much easier for now. 
    # Note: eigenvectors are stored by column:
    for i in xrange(len(np.unique(y))-1):
        eigenvector_i = eigenvectors[:,i].reshape(X[0].shape)
        eigenvector_i_norm = normalize(eigenvector_i, 0, 255, dtype=np.uint8)
        eigenvector_i_colormap = cv2.applyColorMap(eigenvector_i_norm, cv2.COLORMAP_JET)
        # Show or save the images:
        if out_dir is None:
            cv2.imshow("%s/fisherface_%d" % (out_dir,i), eigenvector_i_colormap)
        else:
            cv2.imwrite("%s/fisherface_%d.png" % (out_dir,i), eigenvector_i_colormap)
    # Show the images:
    if out_dir is None:
        cv2.waitKey(0)