def test_vlad(): img = cv2.imread(constants.TESTING_IMG_PATH) option = input("Enter [1] for using ORB features or [2] to use SIFT features.\n") if option == 1: des = descriptors.orb(img) else: des = descriptors.sift(img) des_name = constants.ORB_FEAT_NAME if option == constants.ORB_FEAT_OPTION else constants.SIFT_FEAT_NAME k = 128 codebook_filename = "codebook_{0}_{1}.csv".format(k, des_name) centers = np.loadtxt(codebook_filename, delimiter=constants.NUMPY_DELIMITER) vlad_vector = descriptors.vlad(des, centers) print(vlad_vector) return vlad_vector
def get_data_and_labels(self, img_set, codebook, des_option = constants.ORB_FEAT_OPTION): """ Calculates all the local descriptors for an image set and then uses a codebook to calculate the VLAD global descriptor for each image and stores the label with the class of the image. Args: img_set (string array): The list of image paths for the set. codebook (numpy float matrix): Each row is a center and each column is a dimension of the centers. des_option (integer): The option of the feature that is going to be used as local descriptor. Returns: NumPy float matrix: Each row is the global descriptor of an image and each column is a dimension. NumPy float array: Each element is the number of the class for the corresponding image. """ target = open("result.txt", 'a') y = [] x = None #print len(img_set) #print img_set[0] for class_number in range(len(img_set)): img_paths = img_set[class_number] #print img_paths step = round(constants.STEP_PERCENTAGE * len(img_paths) / 100) for i in range(len(img_paths)): end = img_paths[i].find('.png', 5) target.write(img_paths[i][5:end]+",\n") if (step > 0) and (i % step == 0): percentage = (100 * i) / len(img_paths) print("Calculating global descriptors for image number {0} of {1}({2}%)".format( i, len(img_paths), percentage) ) #print img_paths[i] img = cv2.imread(img_paths[i]) if des_option == constants.ORB_FEAT_OPTION: des = descriptors.orb(img) else: des = descriptors.sift(img) if des is not None: des = nmpy.array(des, dtype=nmpy.float32) vlad_vector = descriptors.vlad(des, codebook) if x is None: x = vlad_vector y.append(class_number) else: x = nmpy.vstack((x, vlad_vector)) y.append(class_number) else: print("Img with None descriptor: {0}".format(img_paths[i])) y = nmpy.float32(y)[:, nmpy.newaxis] x = nmpy.matrix(x) return x, y
def test_one_img_classification(): img = cv2.imread("test.jpg") resize_to = 640 h, w, channels = img.shape img = utils.resize(img, resize_to, h, w) des = descriptors.sift(img) k = 128 des_name = "SIFT" codebook_filename = filenames.codebook(k, des_name) codebook = utils.load(codebook_filename) img_vlad = descriptors.vlad(des, codebook) svm_filename = filenames.svm(k, des_name) svm = cv2.SVM() svm.load(svm_filename) result = svm.predict(img_vlad) print("result is {0}".format(result))
def get_data_and_labels(self, img_set, codebook, des_option = constants.ORB_FEAT_OPTION): """ Calculates all the local descriptors for an image set and then uses a codebook to calculate the VLAD global descriptor for each image and stores the label with the class of the image. Args: img_set (string array): The list of image paths for the set. codebook (numpy float matrix): Each row is a center and each column is a dimension of the centers. des_option (integer): The option of the feature that is going to be used as local descriptor. Returns: NumPy float matrix: Each row is the global descriptor of an image and each column is a dimension. NumPy float array: Each element is the number of the class for the corresponding image. """ y = [] x = None for class_number in range(len(img_set)): img_paths = img_set[class_number] step = round(constants.STEP_PERCENTAGE * len(img_paths) / 100) for i in range(len(img_paths)): if (step > 0) and (i % step == 0): percentage = (100 * i) / len(img_paths) print("Calculating global descriptors for image number {0} of {1}({2}%)".format( i, len(img_paths), percentage) ) img = cv2.imread(img_paths[i]) if des_option == constants.ORB_FEAT_OPTION: des = descriptors.orb(img) else: des = descriptors.sift(img) if des is not None: des = np.array(des, dtype=np.float32) vlad_vector = descriptors.vlad(des, codebook) if x is None: x = vlad_vector y.append(class_number) else: x = np.vstack((x, vlad_vector)) y.append(class_number) else: print("Img with None descriptor: {0}".format(img_paths[i])) y = np.float32(y)[:, np.newaxis] x = np.matrix(x) return x, y