def get_catalog_data(self, compute_descriptors=True):
        iterator = self.catalog_images_paths
        if self.verbose: iterator = tqdm(iterator, desc="Get catalog data")

        self.catalog_data = {
            "keypoints": [],
            "descriptors": [],
            "labels": [],
            "shapes": [],
        }
        for catalog_path in iterator:
            for width in self.catalog_image_widths:
                img = utils.read_image(catalog_path, width=width)
                label = catalog_path.split("/")[-1][:-4]
                keypoints = utils.get_keypoints(img,
                                                self.catalog_keypoint_stride,
                                                self.keypoint_sizes)
                self.catalog_data["keypoints"] += list(keypoints)
                self.catalog_data["labels"] += [label] * len(keypoints)
                self.catalog_data["shapes"] += [img.shape[:2]] * len(keypoints)
                if compute_descriptors:
                    descriptors = utils.get_descriptors(
                        img, keypoints, self.feature_extractor)
                    self.catalog_data["descriptors"] += list(descriptors)

        self.catalog_data["descriptors"] = np.array(
            self.catalog_data["descriptors"])
Ejemplo n.º 2
0
 def compute_image_features(self, image):
     keypoints = utils.get_keypoints(image, self.keypoint_stride, self.keypoint_sizes)
     descriptors = utils.get_descriptors(image, keypoints, self.feature_extractor)
     distances = sklearn_pairwise.pairwise_distances(descriptors, self.vocab["features"], metric="cosine")
     softmax_distances = np.exp(1. - distances) / np.sum(np.exp(1. - distances), axis=1, keepdims=True)
     features = 1. * np.sum(softmax_distances, axis=0) / len(softmax_distances) * self.vocab["idf"]
     return features
    def predict_query(self, query_path, classifier=None, apply_threshold=True):
        # Read img
        query_img = utils.read_image(query_path, width=self.query_image_width)
        query_original_h, query_original_w = cv2.imread(query_path).shape[:2]

        # Get keypoints
        query_keypoints = utils.get_keypoints(query_img,
                                              self.query_keypoint_stride,
                                              self.keypoint_sizes)
        query_kpts_data = np.array(
            [utils.keypoint2data(kpt) for kpt in query_keypoints])

        # Get descriptors
        if self.verbose: print("Query description...")
        query_descriptors = utils.get_descriptors(query_img, query_keypoints,
                                                  self.feature_extractor)

        # Matching
        self.get_matches_results(query_kpts_data, query_descriptors,
                                 query_img.shape)

        # Get bboxes
        bboxes = self.get_raw_bboxes(query_kpts_data)
        bboxes = self.filter_bboxes(bboxes, query_img.shape)
        bboxes = self.merge_bboxes(bboxes, query_img.shape)
        if classifier is not None:
            bboxes = self.add_classifier_score(bboxes, query_img, classifier)
        if apply_threshold:
            bboxes = self.filter_bboxes_with_threshold(bboxes)
        bboxes = self.reshape_bboxes_original_size(
            bboxes, (query_original_h, query_original_w), query_img.shape[:2])

        return bboxes
 def predict_query(self, query, score_threshold=None):
     if type(query) in [str, np.string_]:
         query_img = utils.read_image(query, size=self.image_size)
     else:
         query_img = cv2.resize(query, (self.image_size, self.image_size))
     query_keypoints = utils.get_keypoints(query_img, self.keypoint_stride,
                                           self.keypoint_sizes)
     query_descriptors = utils.get_descriptors(query_img, query_keypoints,
                                               self.feature_extractor)
     scores = self.get_query_scores(query_descriptors)
     return scores
    def get_catalog_descriptors(self):
        iterator = self.catalog_images_paths
        if self.verbose: iterator = tqdm(iterator, desc="Catalog description")

        self.catalog_descriptors = []
        for path in iterator:
            img = utils.read_image(path, size=self.image_size)
            keypoints = utils.get_keypoints(img, self.keypoint_stride,
                                            self.keypoint_sizes)
            descriptors = utils.get_descriptors(img, keypoints,
                                                self.feature_extractor)
            self.catalog_descriptors.append(descriptors)

        self.catalog_descriptors = np.array(self.catalog_descriptors)
        self.catalog_descriptors = self.catalog_descriptors.reshape(
            -1, self.catalog_descriptors.shape[-1])
Ejemplo n.º 6
0
def calculate_descriptors(img_files):
	''' Gets the descriptors for every image in the input. Stores them in files.

	Args:
		img_files (list of string): The path for each image file in the dataset.

	Returns:
		int: number of descriptors.
	'''
	descriptors = None
	files_count = len(img_files)
	step = (5 * files_count) / 100
	max_size = 250
	descriptors_count = 0
	min_desc_ind = 0
	max_desc_ind = 0
	resize_to = 640
	for i in range(files_count):
		# Get the descriptors for each grayscale image 
		filename = img_files[i]
		gray_img = cv2.imread(filename, cv2.CV_LOAD_IMAGE_GRAYSCALE)
		current_img_des = utils.get_descriptors(gray_img, resize=resize_to)
		descriptors_count += len(current_img_des)
		if descriptors is None:
			descriptors = current_img_des
		else:
			descriptors = np.vstack((descriptors, current_img_des))
		if i % step == 0:
			percentage = (i * 100) / files_count
			print(
				"Getting descriptors in image number {0} of {1} ({2}%)".format(
					i, files_count, percentage
				)
			)
		# Stores the descriptors in a file to avoid memory errors
		if i % max_size == 0 and i > 0:
			max_desc_ind = descriptors_count - 1
			storage_count = i / max_size
			storage_name = "des_{0}_{1}_{2}.np".format(
				storage_count, min_desc_ind, max_desc_ind
			)
			pickle.dump(descriptors, open(storage_name, "wb"), protocol=2)
			min_desc_ind += len(descriptors)
			descriptors = None
	return descriptors_count
Ejemplo n.º 7
0
    def build_vocab(self):
        if not self.force_vocab_compute and os.path.exists(self.vocab_path):
            if self.verbose: print("Loading vocab...")
            with open(self.vocab_path, "rb") as f:
                self.vocab = pickle.load(f)
            if self.verbose: print("Vocab loaded !")
        else:
            iterator = self.catalog_images_paths
            if self.verbose: iterator = tqdm(iterator, desc="Vocab construction")
            descriptors = []
            image_ids = []
            for i, image_path in enumerate(iterator):
                image = utils.read_image(image_path, size=self.image_size)
                keypoints = utils.get_keypoints(image, self.keypoint_stride, self.keypoint_sizes)
                desc = utils.get_descriptors(image, keypoints, self.feature_extractor)
                descriptors += list(desc)
                image_ids += [i for _ in range(len(keypoints))]
            descriptors = np.array(descriptors)
            image_ids = np.array(image_ids)

            if self.verbose: print("KMeans step...")
            kmeans = MiniBatchKMeans(n_clusters=self.vocab_size, init_size=3 * self.vocab_size)
            clusters = kmeans.fit_predict(descriptors)
            
            if self.verbose: print("Computing Idfs...")
            self.vocab = {}
            self.vocab["features"] = kmeans.cluster_centers_
            self.vocab["idf"] = np.zeros((self.vocab["features"].shape[0],))
            nb_documents = len(self.catalog_images_paths)
            for cluster in set(clusters):
                nb_documents_containing_cluster = len(set(image_ids[clusters == cluster]))
                self.vocab["idf"][cluster] = np.log(1. * nb_documents / nb_documents_containing_cluster)

            if self.verbose: print("Saving vocal...")
            with open(self.vocab_path, "wb") as f:
                pickle.dump(self.vocab, f) 
            if self.verbose: print("Vocab saved !")
Ejemplo n.º 8
0
def get_descriptor_from_image_path(path):
    resize_to = 640
    gray_img = cv2.imread(path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    return utils.get_descriptors(gray_img, resize=resize_to)