'This method builds the bag of features codebook from a list of directories
with images in them. Each directory should be broken down by image class.
* imgdirs: This list of directories.
* patchsz: the dimensions of each codebook patch
* numcodes: the number of different patches in the codebook.
* imglayout: the shape of the resulting image in terms of patches - this must
match the size of numcodes. I.e. numcodes == img_layout[0]*img_layout[1]
* padding:the pixel padding of each patch in the resulting image.
* imgs_per_dir: this method can use a specified number of images per directory
* verbose: print output
Once the method has completed it will save the results to a local file
using the file name codebook.png
WARNING:
THIS METHOD WILL TAKE FOREVER'

def generate(self, imgdirs, numcodes=128, sz=(11, 11), imgs_per_dir=50, img_layout=(8, 16), padding=0, verbose=True):

    if (numcodes != (img_layout[0] * img_layout[1])):
        warnings.warn('Numcodes    must    match    the    size    of    image    layout.')
        return None
    self.mPadding = padding
    self.mLayout = img_layout
    self.mNumCodes = numcodes
    self.mPatchSize = sz
    rawFeatures = np.zeros((sz[0] * sz[1]))
    for path in imgdirs:
        fcount = 0
        files = []
        for ext in IMAGE_FORMATS:
            files.extend(glob.glob(os.path.join(path, ext)))
        nimgs = min(len(files), imgs_per_dir)
        for i in range(nimgs):
            infile = files[i]
            if verbose:
                print ((((path + '    ') + str(i)) + '    of    ') + str(imgs_per_dir))
                print ('Opening    file:    ' + infile)
            img = Image(infile)
            newFeat = self._getPatches(img, sz)
            if verbose:
                print (('                    Got    ' + str(len(newFeat))) + '    features.')
            rawFeatures = np.vstack((rawFeatures, newFeat))
            del img
    rawFeatures = rawFeatures[1:, :]
    if verbose:
        print '=================================='
        print (('Got    ' + str(len(rawFeatures))) + '    features    ')
        print 'Doing    K-Means    ....    this    will    take    a    long    time'
    self.mCodebook = self._makeCodebook(rawFeatures, self.mNumCodes)
    self.mCodebookImg = self._codebook2Img(self.mCodebook, self.mPatchSize, self.mNumCodes, self.mLayout, self.mPadding)
    self.mCodebookImg.save('codebook.png')
