def get_pathlists(self) -> (list, list, list, list): img_src_pathlist = get_all_files_of_extension(dir_path=self.img_src, extension='png') ann_src_pathlist = get_all_files_of_extension( dir_path=self.ann_src, extension=self.ann_extension) img_dst_pathlist = get_all_files_of_extension(dir_path=self.img_dst, extension='png') ann_dst_pathlist = get_all_files_of_extension( dir_path=self.ann_dst, extension=self.ann_extension) return img_src_pathlist, ann_src_pathlist, img_dst_pathlist, ann_dst_pathlist
def run(self): img_dirs = get_all_files_of_extension(dir_path=self.img_dir, extension='png') annotation_paths = get_all_files_of_extension(dir_path=self.annotation_dir, extension='xml') for i, annotation_path in zip(range(len(annotation_paths)), annotation_paths): rootname = get_rootname_from_path(path=annotation_path) img_path = f"{self.img_dir}/{rootname}.png" xml_path = f"{self.annotation_dir}/{rootname}.xml" self.handler.add(key=len(self.handler.annotations), annotation_path=xml_path, img_path=img_path) self.handler.load_remaining()
def get_camera_settings(self): json_path_list = get_all_files_of_extension(dir_path=self.data_dir, extension='json') camera_settings_path = [ json_path for json_path in json_path_list if '_camera' in json_path ][0] camera_settings_json = json.load(open(camera_settings_path, 'r'))["camera_settings"] return camera_settings_json
def load_annotation_paths(self): annotation_paths = get_all_files_of_extension( dir_path=self.labelimg_annotation_dir, extension='xml') for annotation_path in annotation_paths: rootname = get_rootname_from_path(path=annotation_path) img_path = f"{self.img_dir}/{rootname}.png" xml_path = f"{self.labelimg_annotation_dir}/{rootname}.xml" self.labelimg_annotation_handler.add(key=len( self.labelimg_annotation_handler.annotations), annotation_path=xml_path, img_path=img_path)
def load_from_dir(cls, img_dir: str, json_dir: str, show_pbar: bool = True) -> NDDS_Frame_Handler: check_dir_exists(json_dir) check_dir_exists(img_dir) img_pathlist = get_valid_image_paths(img_dir) json_path_list = [ path for path in get_all_files_of_extension(dir_path=json_dir, extension='json') if not get_filename(path).startswith('_') ] json_path_list.sort() handler = NDDS_Frame_Handler() if show_pbar: pbar = tqdm(total=len(json_path_list), unit='ann(s)', leave=True) pbar.set_description(f'Loading {cls.__name__}') for json_path in json_path_list: check_file_exists(json_path) json_rootname = get_rootname_from_path(json_path) matching_img_path = None matching_cs_img_path = None matching_depth_img_path = None matching_is_img_path = None for img_path in img_pathlist: img_rootname = '.'.join(get_filename(img_path).split('.')[:-1]) if img_rootname == json_rootname: matching_img_path = img_path elif img_rootname == f'{json_rootname}.cs': matching_cs_img_path = img_path elif img_rootname == f'{json_rootname}.depth': matching_depth_img_path = img_path elif img_rootname == f'{json_rootname}.is': matching_is_img_path = img_path if matching_img_path and matching_cs_img_path and matching_depth_img_path and matching_is_img_path: break if matching_img_path is None: logger.error( f"Couldn't find image file that matches rootname of {get_filename(json_path)} in {img_dir}" ) raise FileNotFoundError frame = NDDS_Frame( img_path=matching_img_path, ndds_ann=NDDS_Annotation.load_from_path(json_path), cs_img_path=matching_cs_img_path, depth_img_path=matching_depth_img_path, is_img_path=matching_is_img_path) handler.append(frame) if show_pbar: pbar.update() return handler
def get_all_object_json_files(self): json_path_list = get_all_files_of_extension(dir_path=self.data_dir, extension='json') json_path_list = [ json_path for json_path in json_path_list if '_camera' not in json_path ] json_path_list = [ json_path for json_path in json_path_list if '_object' not in json_path ] json_path_list.sort() return json_path_list
# pbar = tqdm(total=len(frame_result_list), unit='frame(s)') # for result in frame_result_list: # for pred in result.pred_list: # pred.recalculate( # gt_kpt_3d=kpt_3d, # corner_3d=corner_3d, # K=K, # distortion=np.array([0.001647, -0.105636, -0.002094, -0.006446, 0.000000]), # units_per_meter=1.0 # ) # pbar.update() # pbar.close() # frame_result_list.save_to_path(f'{infer_data_dump}/{model_name}_infer0.json', overwrite=True) infer_data_dump_dir = 'infer_data_dump0' infer_data_dump_paths = get_all_files_of_extension(infer_data_dump_dir, extension='json') infer_data_dump_paths.sort() fixed_infer_data_dump_dir = 'infer_data_dump' make_dir_if_not_exists(fixed_infer_data_dump_dir) pbar = tqdm(total=len(infer_data_dump_paths), unit='dump(s)', leave=True) pbar.set_description('Fixing Inference Dumps') for infer_data_dump_path in infer_data_dump_paths: results = PVNetFrameResultList.load_from_path(infer_data_dump_path) for result in results: for pred in result.pred_list: pred.recalculate(gt_kpt_3d=kpt_3d, corner_3d=corner_3d, K=K, distortion=distortion, units_per_meter=1.0) results.save_to_path(
def load_from_dir(cls, load_dir: str) -> LabelmeAnnotationHandler: check_dir_exists(load_dir) json_path_list = get_all_files_of_extension(dir_path=load_dir, extension='json') return cls.load_from_pathlist(json_path_list)
coco_dataset = COCO_Dataset.load_from_path( json_path= '/home/clayton/workspace/prj/data_keep/data/toyota/from_toyota/20201017/20201017_robot_camera/combined/output.json', img_dir= '/home/clayton/workspace/prj/data_keep/data/toyota/from_toyota/20201017/20201017_robot_camera/combined' ) linemod_ann_sample = linemod_dataset.annotations[0] kpt_3d = linemod_ann_sample.fps_3d.copy() kpt_3d.append(linemod_ann_sample.center_3d) corner_3d = linemod_ann_sample.corner_3d K = linemod_ann_sample.K linemod_image_sample = linemod_dataset.images[0] dsize = (linemod_image_sample.width, linemod_image_sample.height) weights_dir = '/home/clayton/workspace/git/clean-pvnet/data/model/pvnet/custom' weight_path_list = get_all_files_of_extension(weights_dir, 'pth') weight_path_list.sort() infer_data_dump_dir = '/home/clayton/workspace/prj/data_keep/data/toyota/from_toyota/20201017/20201017_robot_camera/infer_dump' make_dir_if_not_exists(infer_data_dump_dir) # delete_all_files_in_dir(infer_data_dump_dir, ask_permission=True) weights_pbar = tqdm(total=len(weight_path_list), unit='weight(s)') for weight_path in weight_path_list: rootname = get_rootname_from_path(weight_path) weights_pbar.set_description(rootname) pred_dump_path = f'{infer_data_dump_dir}/{rootname}.json' if file_exists(pred_dump_path): weights_pbar.update() continue inferer = PVNetInferer(weight_path=weight_path) inferer.infer_coco_dataset(dataset=coco_dataset, kpt_3d=kpt_3d,