import os import cv2 import numpy as np from sklearn.svm import SVC from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler from matplotlib import pyplot as plt
mega_histogram = np.array([np.zeros(n_clusters) for i in range(trainImageCount)]) for i in range(trainImageCount): l = len(descriptor_list[i]) for j in range(l): idx = kmeans_ret[i+j] mega_histogram[i][idx] += 1
1 2 3 4 5 6 7 8 9 10 11 12
mega_histogram x_scalar = np.arange(n_clusters) y_scalar = np.array([abs(np.sum(mega_histogram[:,h], dtype=np.int32)) for h in range(n_clusters)])
testImages, testImageCount = imageloader(test_path) predictions = [] for word, imlist in testImages.items(): print("processing ", word) for image in imlist: kp, des = sift_object.detectAndCompute(image, None) vocab = np.array([[0 for i in range(n_clusters)]]) test_ret = kmeans_obj.predict(des) for each in test_ret: vocab[0][each] += 1 vocab = scale.transform(vocab) lb = clf.predict(vocab) print(lb) predictions.append({ 'image':image, 'class':lb, 'object_name':name_dict[str(int(lb[0]))] }) for each in predictions: plt.imshow(cv2.cvtColor(each['image'], cv2.COLOR_GRAY2RGB)) plt.title(each['object_name']) plt.show()