briansrls briansrls - 1 year ago 551
Python Question

Python Bag of Words clustering

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\", line 40, in <module>
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)

des = np.array(descriptors)

bow = cv2.BOWKMeansTrainer(k)

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

Answer Source

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)

#dictionary created
dictionary = BOW.cluster()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download