
"""
this module create feature vectors of dataset.

NOTES :     
    
    - feature extractor algortithms genrates data by feature_table_extractor.
    - feature vector generated by extractor function.
    - this class madule can 

Todo:
        - create vector with single image with img2vector function.
        - create list of vectors and image paths from our dataset 
        - save all features on .pkl files.
"""


# libraris 
import numpy as np
import cv2
import features as fe
import glob
import pickle as pkl
import pandas as pd
import json

class FeatureExtractor:
    def __str__(self):
        pass
    def __init__(self):
        with open('conf/conf.json') as f:  
            self.config = json.load(f)
        with open('conf/feature_names.json') as f:    
            self.features_name = json.load(f)  
        self.csv_path  = self.config["csv_path"]
        self.data = pd.read_csv(self.csv_path,index_col=0)
        self.image_size = tuple((500, 500))

        #objects of feature extractor
        self.fe_glb = fe.Global_feature_extraction()
        self.fe_lcl = fe.Local_feature_extractor()

        
    def feature_table(self,image_path, product_id ,*args):
        feature_table = {'product_id':None,'shape':None,'texture':None,'color':None,'SIFT':None,'SURF':None,'KAZE':None}
        feature_table['product_id'] = product_id
        Image = cv2.imread(image_path)
        Image = cv2.resize(Image,self.image_size)

        for ar in args:
            if ar == 'shape':
                shape = self.fe_glb.shape(Image)
                feature_table['shape']=shape   

            elif ar == 'texture':
                texture = self.fe_glb.texture(Image)
                feature_table['texture']=texture

            elif ar=='color':
                
                color = self.fe_glb.color(Image)
                feature_table['color']=color

            elif ar=='sift' or ar=='SIFT':
                
                _ , sift_des = self.fe_lcl.SIFT(image_path)
                feature_table['SIFT']= sift_des

            elif ar=='SURF' or ar=='surf':
                
                _ , surf_des = self.fe_lcl.SURF(image_path)
                feature_table['SURF']= surf_des

            elif ar=='ORB' or ar=='orb':
                
                _ , orb_des = self.fe_lcl.ORB(image_path)
                feature_table['ORB']= orb_des

            elif ar=='BRIEF' or ar=='brief':
                
                _ , brief_des = self.fe_lcl.BRIEF(image_path)
                feature_table['BRIEF']= brief_des

            elif ar=='KAZE' or ar=='kaze':
                
                _ , kaze_des = self.fe_lcl.KAZE(image_path)
                feature_table['KAZE']= kaze_des
        return feature_table

    def csv_extractor(self):
        features = []
        for i in range(len(self.data['image_path'])):
            img_path = self.data['image_path'][i]
            product_id = self.data['product_id'][i]
            # feature_table = self.feature_table(img_path,product_id,'shape','texture','color')
            feature_table = self.feature_table(img_path,product_id,'shape','texture','color','SIFT','SURF','KAZE','Dense')
            features.append(feature_table)
        return features


    def save(self):
        features = self.csv_extractor()
        with open('data.pkl','wb') as f:
            pkl.dump(features,f)