def match_features(self, kapture_data): image_list = [ filename for _, _, filename in kapture.flatten(kapture_data.records_camera) ] descriptors = [] descriptor_type = kapture_data.descriptors.dtype descriptor_size = kapture_data.descriptors.dsize for image_path in image_list: descriptors_full_path = get_descriptors_fullpath( kapture_data.kapture_path, image_path) descriptors.append( image_descriptors_from_file(descriptors_full_path, descriptor_type, descriptor_size)) kapture_data.matches = kapture.Matches() if self._sequential_length is None: self._sequential_length = len(image_list) for i in tqdm(range(len(image_list))): for j in range(i + 1, min(len(image_list), i + self._sequential_length)): matches = self._matcher.match_descriptors( descriptors[i], descriptors[j]) if self._minimal_score is not None: mask = matches[:, 2] > self._minimal_score matches = matches[mask] kapture_data.matches.add(image_list[i], image_list[j]) matches_full_path = get_matches_fullpath( (image_list[i], image_list[j]), kapture_data.kapture_path) image_matches_to_file(matches_full_path, matches)
def compute_matches(input_path: str, pairsfile_path: str): """ compute matches from descriptors. images to match are selected from a pairsfile (csv with name1, name2, score) :param input_path: input path to kapture input root directory :type input_path: str :param pairsfile_path: path to pairs file (csv with 3 fields, name1, name2, score) :type pairsfile_path: str """ logger.info(f'compute_matches. loading input: {input_path}') kdata = kapture_from_dir(input_path) assert kdata.sensors is not None assert kdata.records_camera is not None assert kdata.descriptors is not None image_pairs = get_pairs_from_file(pairsfile_path) matcher = MatchPairNnTorch(use_cuda=torch.cuda.is_available()) new_matches = kapture.Matches() logger.info('compute_matches. entering main loop...') hide_progress_bar = logger.getEffectiveLevel() > logging.INFO for image_path1, image_path2 in tqdm(image_pairs, disable=hide_progress_bar): if image_path1 == image_path2: continue if image_path1 > image_path2: image_path1, image_path2 = image_path2, image_path1 if image_path1 not in kdata.descriptors or image_path2 not in kdata.descriptors: logger.warning('unable to find descriptors for image pair : ' '\n\t{} \n\t{}'.format(image_path1, image_path2)) continue descriptor1 = load_descriptors(input_path, image_path1, kdata.descriptors.dtype, kdata.descriptors.dsize) descriptor2 = load_descriptors(input_path, image_path2, kdata.descriptors.dtype, kdata.descriptors.dsize) matches = matcher.match_descriptors(descriptor1, descriptor2) matches_path = get_matches_fullpath((image_path1, image_path2), input_path) image_matches_to_file(matches_path, matches) new_matches.add(image_path1, image_path2) if not matches_check_dir(new_matches, input_path): logger.critical( 'matching ended successfully but not all files were saved') logger.info('all done')
def compute_matches_from_loaded_data(input_path: str, kdata: kapture.Kapture, image_pairs: list, overwrite_existing: bool = False): assert kdata.sensors is not None assert kdata.records_camera is not None assert kdata.descriptors is not None matcher = MatchPairNnTorch(use_cuda=torch.cuda.is_available()) new_matches = kapture.Matches() logger.info('compute_matches. entering main loop...') hide_progress_bar = logger.getEffectiveLevel() > logging.INFO skip_count = 0 for image_path1, image_path2 in tqdm(image_pairs, disable=hide_progress_bar): if image_path1 == image_path2: continue if image_path1 > image_path2: image_path1, image_path2 = image_path2, image_path1 # skip existing matches if (not overwrite_existing) and (kdata.matches is not None) and ((image_path1, image_path2) in kdata.matches): new_matches.add(image_path1, image_path2) skip_count += 1 continue if image_path1 not in kdata.descriptors or image_path2 not in kdata.descriptors: logger.warning('unable to find descriptors for image pair : ' '\n\t{} \n\t{}'.format(image_path1, image_path2)) continue descriptor1 = load_descriptors(input_path, image_path1, kdata.descriptors.dtype, kdata.descriptors.dsize) descriptor2 = load_descriptors(input_path, image_path2, kdata.descriptors.dtype, kdata.descriptors.dsize) matches = matcher.match_descriptors(descriptor1, descriptor2) matches_path = get_matches_fullpath((image_path1, image_path2), input_path) image_matches_to_file(matches_path, matches) new_matches.add(image_path1, image_path2) if not overwrite_existing: logger.debug(f'{skip_count} pairs were skipped because the match file already existed') if not matches_check_dir(new_matches, input_path): logger.critical('matching ended successfully but not all files were saved') logger.info('all done')
def compute_matches_from_loaded_data(input_path: str, tar_handlers: Optional[TarCollection], kdata: kapture.Kapture, descriptors_type: Optional[str], image_pairs: list, overwrite_existing: bool = False): assert kdata.sensors is not None assert kdata.records_camera is not None assert kdata.descriptors is not None os.umask(0o002) if descriptors_type is None: descriptors_type = try_get_only_key_from_collection(kdata.descriptors) assert descriptors_type is not None assert descriptors_type in kdata.descriptors keypoints_type = kdata.descriptors[descriptors_type].keypoints_type # assert kdata.descriptors[descriptors_type].metric_type == "L2" matcher = MatchPairNnTorch(use_cuda=torch.cuda.is_available()) new_matches = kapture.Matches() logger.info('compute_matches. entering main loop...') hide_progress_bar = logger.getEffectiveLevel() > logging.INFO skip_count = 0 for image_path1, image_path2 in tqdm(image_pairs, disable=hide_progress_bar): if image_path1 == image_path2: continue if image_path1 > image_path2: image_path1, image_path2 = image_path2, image_path1 # skip existing matches if (not overwrite_existing) \ and (kdata.matches is not None) \ and keypoints_type in kdata.matches \ and ((image_path1, image_path2) in kdata.matches[keypoints_type]): new_matches.add(image_path1, image_path2) skip_count += 1 continue if image_path1 not in kdata.descriptors[descriptors_type] \ or image_path2 not in kdata.descriptors[descriptors_type]: logger.warning('unable to find descriptors for image pair : ' '\n\t{} \n\t{}'.format(image_path1, image_path2)) continue descriptor1 = load_descriptors( descriptors_type, input_path, tar_handlers, image_path1, kdata.descriptors[descriptors_type].dtype, kdata.descriptors[descriptors_type].dsize) descriptor2 = load_descriptors( descriptors_type, input_path, tar_handlers, image_path2, kdata.descriptors[descriptors_type].dtype, kdata.descriptors[descriptors_type].dsize) matches = matcher.match_descriptors(descriptor1, descriptor2) matches_path = get_matches_fullpath((image_path1, image_path2), keypoints_type, input_path, tar_handlers) image_matches_to_file(matches_path, matches) new_matches.add(image_path1, image_path2) if not overwrite_existing: logger.debug( f'{skip_count} pairs were skipped because the match file already existed' ) if not matches_check_dir(new_matches, keypoints_type, input_path, tar_handlers): logger.critical( 'matching ended successfully but not all files were saved') # update kapture matches if kdata.matches is None: kdata.matches = {} if keypoints_type not in kdata.matches: kdata.matches[keypoints_type] = kapture.Matches() kdata.matches[keypoints_type].update(new_matches) logger.info('all done')