Example #1
0
def store_test_des(dataset):
    ''' Stores the descriptors for the testing set in one file per object.
		Each file has a matrix where a row represent a descriptor.
	'''
    # e.g. test = [[bear/img1.jpg, bear/img2.jpg], [cat/img1.jpg, ...], ...]
    test = dataset.get_test_set()
    counter = 0
    n_files = 30
    folders = glob.glob("test/*")
    for files in test:
        s = "Getting descriptors in class {0}.".format(counter)
        print(s)
        for i in range(n_files):
            # iteration over files inside a class
            # if i % step == 0:
            percentage = (i * 100) / n_files
            print("Getting SIFT from file {0} of {1} ({2}%) ...".format(
                i, n_files, percentage))
            fname = files[i]
            test_img = cv2.imread(fname)
            kp, current_des = descriptors.sift(test_img)

            # Store current descriptors
            if "/" in fname:
                obj_name = fname.split("/")[-1]
                obj_name = obj_name.split(".")[0]
                storage_name = folders[counter] + "/" + obj_name + ".mat"
            else:
                obj_name = fname.split("\\")[-1]
                obj_name = obj_name.split(".")[0]
                storage_name = folders[counter] + "\\" + obj_name + ".mat"
            data = {"stored": current_des}
            sio.savemat(storage_name, data)
        counter += 1
Example #2
0
def test_sift():
	img_path = glob.glob("dataset/bear/*.JPEG")[0]
	img = cv2.imread(img_path)
	kp, des = descriptors.sift(img)
	img = cv2.drawKeypoints(img, kp, color=(255,0,0))
	cv2.imshow("image", img)
	cv2.waitKey(0)
	print("descriptors len = {0}".format(len(des)))
	print("descriptors[0] len = {0}".format(len(des[0])))
	print("descriptors[0][:5] = {0}".format(np.array(des[0][:], dtype=np.uint16)))
Example #3
0
def test_sift():
    img_path = glob.glob("dataset/bear/*.JPEG")[0]
    img = cv2.imread(img_path)
    kp, des = descriptors.sift(img)
    img = cv2.drawKeypoints(img, kp, color=(255, 0, 0))
    cv2.imshow("image", img)
    cv2.waitKey(0)
    print("descriptors len = {0}".format(len(des)))
    print("descriptors[0] len = {0}".format(len(des[0])))
    print("descriptors[0][:5] = {0}".format(
        np.array(des[0][:], dtype=np.uint16)))
Example #4
0
    def get_data_and_labels(self, img_set, test_image, cluster_model, k, des_name, codebook,isTrain, 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
        img_descs = []
        
        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)
                img = cv2.imread(img_paths[i])
                
                des,y = descriptors.sift(img,img_descs,y,class_number)
                """ des : descriptor
                    y : class_number
                """
        isTrain = int(isTrain)


        X, X_test, y1, testimg_hist, cluster_model = descriptors.cluster_features(des, test_image, cluster_model=KMeans(n_clusters=128))
        """
        X: histogram of words
        cluster_model : MiniBatchKMeans
        testimg_hist : testImage histogram
        """
        """
        if isTrain == 1:
        else:
            X = descriptors.img_to_vect(des,cluster_model)
        """

        print('X',X.shape,X)
        y = np.float32(y)[:,np.newaxis]
        x = np.matrix(X)
        y1 = np.float32(y1)[:, np.newaxis]
        x_test = np.matrix(X_test)
        testimg_h = np.matrix(testimg_hist)
        return x, y, x_test, y1, testimg_h, cluster_model
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
Example #6
0
    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
Example #7
0
def process_dataset(codebook, set_paths):
    vlad = Vlad(codebook)
    X = None
    y = []
    i = 0
    for class_paths in set_paths:
        for img_path in class_paths:
            keypoints, image_descriptors = descriptors.sift(cv2.imread(img_path.replace('\\','/')))
            image_vlad = vlad.get_image_vlad(image_descriptors)
            if X == None:
                X = image_vlad
            else:
                X = np.vstack((X, image_vlad))
        y += len(class_paths)*[i]
        i += 1
    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))
Example #9
0
def class_descriptors(class_files):
    class_des = None
    step = (len(class_files) * 5) / 100
    for i in range(len(class_files)):
        if i % step == 0:
            percentage = (i * 100) / len(class_files)
            print("Getting SIFT from image {0} of {1}({2}%) ...".format(
                i, len(class_files), percentage))
        path = class_files[i]
        img = cv2.imread(path)
        kp, des = descriptors.sift(img)
        if class_des is None:
            class_des = np.array(des, dtype=np.uint16)
        else:
            class_des = np.vstack((class_des, np.array(des, dtype=np.uint16)))
    return class_des
Example #10
0
def process_dataset(codebook, set_paths):
    vlad = Vlad(codebook)
    X = None
    y = []
    i = 0
    for class_paths in set_paths:
        for img_path in class_paths:
            keypoints, image_descriptors = descriptors.sift(
                cv2.imread(img_path.replace('\\', '/')))
            image_vlad = vlad.get_image_vlad(image_descriptors)
            if X == None:
                X = image_vlad
            else:
                X = np.vstack((X, image_vlad))
        y += len(class_paths) * [i]
        i += 1
    return X, y
def test_sift():
	opt = input(
		"Choose an option:\n"\
		" [0] Write the image in this folder\n"\
		" [1] Show image\n"
	)
	n = input("Enter the image number to use.\n")
	WRITE = (opt == 0)
	dataset = pickle.load(open("dataset.obj", "rb"))
	img_path = dataset.get_train_set()[8][n]
	first_img = cv2.imread(img_path)
	kp, des = descriptors.sift(first_img)
	img = cv2.drawKeypoints(first_img, kp)
	if WRITE:
		cv2.imwrite("sift.jpg", img)
	else:
		cv2.imshow("Keypoints", img)
		cv2.waitKey()
def test_sift():
    opt = input(
     "Choose an option:\n"\
     " [0] Write the image in this folder\n"\
     " [1] Show image\n"
    )
    n = input("Enter the image number to use.\n")
    WRITE = (opt == 0)
    dataset = pickle.load(open("dataset.obj", "rb"))
    img_path = dataset.get_train_set()[8][n]
    first_img = cv2.imread(img_path)
    kp, des = descriptors.sift(first_img)
    img = cv2.drawKeypoints(first_img, kp)
    if WRITE:
        cv2.imwrite("sift.jpg", img)
    else:
        cv2.imshow("Keypoints", img)
        cv2.waitKey()
    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
Example #14
0
def store_train_des(filenames, class_name):
    class_des = None
    step = (len(filenames) * 5) / 100
    for i in range(len(filenames)):
        path = filenames[i]
        if i % step == 0:
            percentage = (i * 100) / len(filenames)
            print("Getting SIFT from image {0} of {1}({2}%) ...".format(
                i, len(filenames), percentage))
        img = cv2.imread(path)
        kp, des = descriptors.sift(img)
        if class_des == None:
            class_des = np.array(des, dtype=np.uint16)
        else:
            class_des = np.vstack((class_des, np.array(des, dtype=np.uint16)))
    print("descriptors of shape = {0}".format(class_des.shape))
    filename = "train/des_" + class_name + ".mat"
    data = {"stored": class_des}
    sio.savemat(filename, data)
Example #15
0
def store_train_des(filenames, class_name):
	class_des = None
	step = (len(filenames) * 5) / 100
	for i in range(len(filenames)):
		path = filenames[i]
		if i % step == 0:
			percentage = (i * 100) / len(filenames)
			print("Getting SIFT from image {0} of {1}({2}%) ...".format(
					i, len(filenames), percentage
				)
			)
		img = cv2.imread(path)
		kp, des = descriptors.sift(img)
		if class_des == None:
			class_des = np.array(des, dtype=np.uint16)
		else:
			class_des = np.vstack((class_des, np.array(des, dtype=np.uint16)))
	print("descriptors of shape = {0}".format(class_des.shape))
	filename = "train/des_" + class_name + ".mat"
	data = {"stored": class_des}
	sio.savemat(filename, data)
Example #16
0
def store_test_des(dataset):
	''' Stores the descriptors for the testing set in one file per object.
		Each file has a matrix where a row represent a descriptor.
	'''
	# e.g. test = [[bear/img1.jpg, bear/img2.jpg], [cat/img1.jpg, ...], ...]
	test = dataset.get_test_set()
	counter = 0
	n_files = 30
	folders = glob.glob("test/*")
	for files in test:
		s = "Getting descriptors in class {0}.".format(counter)
		print(s)
		for i in range(n_files):
			# iteration over files inside a class
			# if i % step == 0:
			percentage = (i * 100) / n_files
			print(
				"Getting SIFT from file {0} of {1} ({2}%) ...".format(
					i, n_files, percentage
				)
			)
			fname = files[i]
			test_img = cv2.imread(fname)
			kp, current_des = descriptors.sift(test_img)
			
			# Store current descriptors
			if "/" in fname:
				obj_name = fname.split("/")[-1]
				obj_name = obj_name.split(".")[0]
				storage_name = folders[counter] + "/" + obj_name + ".mat"
			else:
				obj_name = fname.split("\\")[-1]
				obj_name = obj_name.split(".")[0]
				storage_name = folders[counter] + "\\" + obj_name + ".mat"
			data = {"stored": current_des}
			sio.savemat(storage_name, data)
		counter += 1
Example #17
0
def knn_kdtree(dataset):
    # n_classes = len(dataset.get_classes())
    n_classes = 5
    des_files = glob.glob("train/*.mat")
    predictions = []
    counter = 0

    # Get KDTrees with the descriptors of the training set
    print("Getting sample of the descriptors for classes in their KD-trees")
    classes_trees = []
    start = time.time()
    for c in range(n_classes):
        fname = des_files[c]
        print("fname = {0}".format(fname))
        data = sio.loadmat(fname)
        class_des = data["stored"]

        sample_size = 1000
        current_sample = utils.random_sample(class_des, sample_size)
        tree = spatial.KDTree(current_sample)
        classes_trees.append(tree)
        # cleaning this variable
        class_des = None
    end = time.time()
    elapsed_time = utils.humanize_time(end - start)
    print("Elapsed time building KDTrees {0}".format(elapsed_time))

    for test_files in dataset.get_test_set():
        # Using only the first n_files objects of the test set for each class
        n_files = 15
        # TESTING! #
        if counter == n_classes:
            break
        ############
        print("Starting to predict the test set of class {0}.".format(counter))
        # iteration over classes
        predictions.append([])
        # The i-th element of this list has the descriptors for the i-th image;
        # all the images are in the same class-folder
        test_des_list = []
        # step = (len(test_files) * 5) / 100
        # for i in range(len(test_files)):
        for i in range(n_files):
            # iteration over files inside a class
            # if i % step == 0:
            # 	percentage = (i * 100) / len(test_files)
            # 	print(
            # 		"Getting SIFT from file {0} of {1} ({2}%) ...".format(
            # 			i, len(test_files), percentage
            # 		)
            # 	)
            fname = test_files[i]
            test_img = cv2.imread(fname)
            kp, current_des = descriptors.sift(test_img)
            test_des_list.append(current_des)
        # We can't add more descriptors because it would be too expensive in RAM

        # distances = np.zeros((len(test_files), n_classes))
        distances = np.zeros((n_files, n_classes))
        for c in range(n_classes):
            # for img_index in range(len(test_files)):
            for img_index in range(n_files):
                des = test_des_list[img_index]
                print("Getting dist for img index = {0}".format(img_index))
                start = time.time()
                current_dists, ids = classes_trees[c].query(des)
                distances[img_index][c] = sum(current_dists)
                end = time.time()
                s = "Seconds getting distances {0} for {1} descriptors".format(
                    (end - start), len(des))
                print(s)
        # for img_index in range(len(test_files)):
        for img_index in range(n_files):
            predictions[-1].append(np.argmin(distances[img_index]))
        counter += 1
    return predictions