def loadrecords(csvfile, cachedir):
    '''
    Loads the records described by the csv file and cropped images in the
    cachedir.
    This returns xdata as a numpy 3D array, which is an array of 2D images.
    The ydata will be a numpy 1D array of values of 1 or -1.  The ydata array
    represents the sex field of the csv file.  +1 represents male and -1
    represents female.
    >>> xdata, ydata = loadrecords(csvfile, cachedir)
    '''
    xdata = []
    ydata = []
    with open(csvfile, 'r') as trainFile:
        reader = csv.DictReader(trainFile, dialect='excel-tab')
        for line in reader:
            filename = os.path.basename(line['imagePath'])
            split = os.path.splitext(filename)
            cachepic = split[0] + '-' + line['line'] + '.png'
            xdata.append(imagefuncs.loadImage(os.path.join(cachedir, cachepic)))
            ydata.append(1 if line['truth-sex'] == 'M' else -1)
    return np.asarray(xdata), np.asarray(ydata)
def main(arguments):
    'Main entry point'
    args = parseArgs(arguments)
    with open(args.classifier, 'rb') as classifierFile:
        classifier = cPickle.load(classifierFile)
    xdata = []
    imagepaths = []
    print 'Reading in images...'
    for entry in os.listdir(args.input):
        filepath = os.path.join(args.input, entry)
        if os.path.isfile(filepath):
            try:
                xdata.append(loadImage(filepath))
            except IOError:
                # This probably wasn't an image file or bad permissions
                print >> sys.stderr, 'Warning: Could not read image', filepath
            else:
                # Only store the filepath if we were able to load the image
                imagepaths.append(filepath)
    print '  done'

    print 'Preprocessing data...'
    xdata = np.asarray(xdata)
    xdata = preprocessData(xdata, reshape=(not isinstance(classifier, ConvNet)))
    print '  done'

    print 'Classifying the data...'
    labels = classifier.predict(xdata)
    print '  done'

    # Output the results to a file
    with open(args.output, 'w') as outfile:
        writer = csv.writer(outfile, dialect='excel-tab')
        writer.writerow(['imagepath', 'classification'])
        for imagepath, label in zip(imagepaths, labels):
            labelString = 'M' if label == 1 else 'F'
            writer.writerow([imagepath, labelString])
    print 'Wrote classification data to', args.output