from pyimagesearch.cbir import HSVDescriptor
from imutils import paths
import progressbar
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument('-d', '--dataset', required=True, help='path to the directory that contains the images to be indexed')
ap.add_argument('-i', '--index', required=True, help='path to where the features index will be stored')
args = vars(ap.parse_args())

# initialize the color descriptor and open the output index file for writing
desc = HSVDescriptor((4, 6, 3))
output = open(args['index'], 'w')

# grab the list of image paths and initialize the progress bar
imagePaths = list(paths.list_images(args['dataset']))
widgets = ['Indexing:', progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA()]
pbar = progressbar.ProgressBar(maxval=len(imagePaths), widgets=widgets)

for (i, imagePath) in enumerate(sorted(imagePaths)):
    filename = imagePath[imagePath.rfind('/') + 1:]
    image = cv2.imread(imagePath)

    features = desc.describe(image)

    features = [str(x) for x in features]
    output.write('{}, {}\n'.format(filename, ','.join(features)))
    pbar.update(i)

pbar.finish()
print('[INFO] indexed {} images'.format(len(imagePaths)))
output.close()


