-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
50 lines (41 loc) · 1.54 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# author: Adrian Rosebrock
# date: 27 January 2014
# website: http://www.pyimagesearch.com
# USAGE
# python index.py --dataset images --index index.cpickle
# import the necessary packages
from pyimagesearch.rgbhistogram import RGBHistogram
import argparse
import _pickle as cPickle
import glob
import cv2
# construct the argument parser and parse the arguments
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 computed index will be stored")
args = vars(ap.parse_args())
# initialize the index dictionary to store our our quantifed
# images, with the 'key' of the dictionary being the image
# filename and the 'value' our computed features
index = {}
# initialize our image descriptor -- a 3D RGB histogram with
# 8 bins per channel
desc = RGBHistogram([8, 8, 8])
# use glob to grab the image paths and loop over them
for imagePath in glob.glob(args["dataset"] + "/*[0-9][0-9][0-9].png"):
# extract our unique image ID (i.e. the filename)
k = imagePath[imagePath.rfind("/") + 1:]
# load the image, describe it using our RGB histogram
# descriptor, and update the index
image = cv2.imread(imagePath)
features = desc.describe(image)
index[k] = features
# we are now done indexing our image -- now we can write our
# index to disk
f = open(args["index"], "wb")
f.write(cPickle.dumps(index))
f.close()
# show how many images we indexed
print("done...indexed %d images" % (len(index)))