Keyboard Python Bag of Words

Edit: Here is the complete code for anyone with github.com problems

I am trying to complete a pattern recognition project using SIFT and BOW. So far I am trying to train and build a dictionary. I read images from 5 different classes, computed descriptors, and added them all to the python ([]) list side by side. Now I am trying to use the python version of the BOWMeansTrainer to cluster my descriptors with k = 5 (is this correct? For 5 classes?). I try to pass cluster () my descriptor vector, but I get an 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 quite sure what format the numpy array fits in, does anyone have any ideas?

 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 continue to add sift descriptors, and then try to convert to a numpy array (the required format).

+6
source share
1 answer

Just made it up because of the opencv forums, instead of using a different list (I used the descriptors above), just add the descriptors that you will find directly in your bag using 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 everyone who has problems, I downloaded the rest of the script here p>

+5
source

All Articles