Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Bag of Words clustering

Edit: Here is the complete code for anyone having issues still github.com

I'm trying to do an image recognition project using SIFT and BOW. So far, I'm trying to train and build my dictionary. I've read in images from 5 different classes, computed descriptors and added them all to a python list ([]) side by side. Now, I'm trying to use the python version of BOWMeansTrainer to cluster my descriptors with k = 5 (is this correct? for 5 classes?). I'm trying to pass cluster() my descriptor vector, but I'm getting the error

Traceback (most recent call last):


File "C:\Python27\Project2\beginning.py", line 40, in <module>
    bow.cluster(des)
TypeError: descriptors data type = 17 is not supported

I'm not really sure what format to put my numpy array in, does anyone have an idea?

sift = cv2.SIFT()

descriptors = []
for path in training_paths:
    image = cv2.imread(path)
    print path
    gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    kp, dsc= sift.detectAndCompute(gray, None)
    descriptors.append(dsc)

des = np.array(descriptors)

k=5
bow = cv2.BOWKMeansTrainer(k)
bow.cluster(des)

As you can see I keep appending the sift descriptors, and then try to convert to a numpy array (the needed format).

like image 796
blurb Avatar asked Nov 14 '15 22:11

blurb


1 Answers

Just figured it out thanks to the opencv forums, instead of using another list (I used descriptors above), just add the descriptors you find directly to your bag with bow.add(dsc)

dictionarySize = 5

BOW = cv2.BOWKMeansTrainer(dictionarySize)

for p in training_paths:
    image = cv2.imread(p)
    gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    kp, dsc= sift.detectAndCompute(gray, None)
    BOW.add(dsc)

#dictionary created
dictionary = BOW.cluster()

EDIT: For anyone else having trouble I've uploaded the rest of the script here

like image 153
blurb Avatar answered Oct 20 '22 19:10

blurb