import csv
import os

from gamera.core import *

from tests.opencv.gameraf import Gamerafeatures



def rw(data):
    return ([str(x) for x in data])


run_once = 0
path = os.listdir('../training-set')
with open('featurers.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile)
    for folder in path:
        files = os.listdir('../training-set/' + str(folder))
        for imagename in files:
            fullpath = '../training-set/' + str(folder) + '/' + str(imagename)
            print fullpath
            image = load_image(fullpath)
            gf = Gamerafeatures(image)
            if run_once == 0:
                run_once = 1
                spamwriter.writerow([','.join(['imagenam'] +
                                    ['folder'] +
                                    [('area' + str(s)) for s in range(len(gf.area))] +
                                    [('aspect_ratio' + str(s)) for s in range(len(gf.aspect_ratio))] +
                                    [('black_area' + str(s)) for s in range(len(gf.black_area))] +
                                    [('compactness' + str(s)) for s in range(len(gf.compactness))] +
                                    [('diagonal_projection' + str(s)) for s in range(len(gf.diagonal_projection))] +
                                    [('moments' + str(s)) for s in range(len(gf.moments))] +
                                    [('ncols_feature' + str(s)) for s in range(len(gf.ncols_feature))] +
                                    [('nholes' + str(s)) for s in range(len(gf.nholes))] +
                                    [('nholes_extended' + str(s)) for s in range(len(gf.nholes_extended))] +
                                    [('nrows_feature' + str(s)) for s in range(len(gf.nrows_feature))] +
                                    [('skeleton_features' + str(s)) for s in range(len(gf.skeleton_features))] +
                                    [('top_bottom' + str(s)) for s in range(len(gf.top_bottom))] +
                                    [('volume' + str(s)) for s in range(len(gf.volume))] +
                                    [('volume16regions' + str(s)) for s in range(len(gf.volume16regions))] +
                                    [('volume64regions' + str(s)) for s in range(len(gf.volume64regions))] +
                                    [('zernike_moments' + str(s)) for s in range(len(gf.zernike_moments))] +
                                    ['height'] +
                                    ['width'])])
            spamwriter.writerow([imagename] + [folder] + rw(gf.area) + rw(gf.aspect_ratio) +  rw(gf.black_area)
                                + rw(gf.compactness) + rw(gf.diagonal_projection) + rw(gf.moments) + rw(gf.ncols_feature) + rw(gf.nholes)
                                + rw(gf.nholes_extended) + rw(gf.nrows_feature) + rw(gf.skeleton_features) + rw(gf.top_bottom) + rw(gf.volume)
                                + rw(gf.volume16regions) + rw(gf.volume64regions) + rw(gf.zernike_moments) + [str(image.height)] + [str(image.width)])


