from logger import logger from annotation_utils.ndds.structs import NDDS_Dataset from annotation_utils.coco.structs import COCO_Dataset, COCO_Category_Handler from common_utils.file_utils import make_dir_if_not_exists, delete_all_files_in_dir src_dir = '/home/clayton/workspace/prj/data_keep/data/ndds/20200924_yagura5_bright_1-colored' dst_dir = 'nihonbashi_debug' make_dir_if_not_exists(dst_dir) delete_all_files_in_dir(dst_dir, ask_permission=False) hsr_categories = COCO_Category_Handler.load_from_path( '/home/clayton/workspace/prj/data_keep/data/toyota/dataset/config/categories/hsr_categories.json' ) # Load NDDS Dataset ndds_dataset = NDDS_Dataset.load_from_dir(json_dir=src_dir, show_pbar=True) # Fix NDDS Dataset naming so that it follows convention. (This is not necessary if the NDDS dataset already follows the naming convention.) for frame in ndds_dataset.frames: # Fix Naming Convention for ann_obj in frame.ndds_ann.objects: if ann_obj.class_name.lower() == 'nihonbashi': obj_type, obj_name = 'seg', 'hsr' instance_name = '0' ann_obj.class_name = f'{obj_type}_{obj_name}_{instance_name}' elif ann_obj.class_name.lower() in list('abcdefghijkl'): obj_type, obj_name = 'kpt', 'hsr' instance_name, contained_name = '0', ann_obj.class_name ann_obj.class_name = f'{obj_type}_{obj_name}_{instance_name}_{contained_name}' else: logger.error(f'Unknown ann_obj.class_name: {ann_obj.class_name}')
from common_utils.file_utils import file_exists from annotation_utils.coco.structs import COCO_Dataset, COCO_Category_Handler, COCO_Category from annotation_utils.labelme.structs import LabelmeAnnotationHandler # Define Labelme Directory Paths img_dir = '/path/to/labelme/img/dir' json_dir = '/path/to/labelme/json/dir' # Load Labelme Handler labelme_handler = LabelmeAnnotationHandler.load_from_dir(load_dir=json_dir) # Define COCO Categories Before Conversion if not file_exists('categories_example.json'): # Save a new categories json if it doesn't already exist. categories = COCO_Category_Handler() categories.append( # Standard Keypoint Example COCO_Category( id=len(categories), supercategory='pet', name='dog', keypoints=[ # The keypoint labels are defined here 'left_eye', 'right_eye', # 0, 1 'mouth_left', 'mouth_center', 'mouth_right' # 2, 3, 4 ], skeleton=[ # The connections between keypoints are defined with indecies here [0, 1], [2, 3], [3,4] ] ) ) categories.append( # Simple Keypoint Example COCO_Category.from_label_skeleton(
dst_dir = 'crescent_dataset_val' # Load NDDS Dataset ndds_dataset = NDDS_Dataset.load_from_dir(json_dir=src_dir, show_pbar=True) # Fix NDDS Dataset naming so that it follows convention. (This is not necessary if the NDDS dataset already follows the naming convention.) for frame in ndds_dataset.frames: # Fix Naming Convention for ann_obj in frame.ndds_ann.objects: if ann_obj.class_name.startswith('crescent'): obj_type, obj_name = 'seg', 'crescent' instance_name = '0' ann_obj.class_name = f'{obj_type}_{obj_name}_{instance_name}' # Convert To COCO Dataset crescent_categories = COCO_Category_Handler() crescent_categories.append( COCO_Category(id=len(crescent_categories), name='crescent')) dataset = COCO_Dataset.from_ndds(ndds_dataset=ndds_dataset, categories=crescent_categories, naming_rule='type_object_instance_contained', show_pbar=True, bbox_area_threshold=-1, allow_unfound_seg=True) dataset.move_images(dst_img_dir=dst_dir, preserve_filenames=True, overwrite_duplicates=False, update_img_paths=True, overwrite=True, show_pbar=True) dataset.save_to_path(f'{dst_dir}/output.json', overwrite=True)
from annotation_utils.coco.structs import COCO_Category_Handler, COCO_Category handler = COCO_Category_Handler() handler.append(COCO_Category(id=len(handler), name='garbage')) output_dir = '/home/clayton/workspace/prj/data_keep/data/ndds/categories' handler.save_to_path(f'{output_dir}/garbage.json', overwrite=True)
from annotation_utils.coco.structs import COCO_Category_Handler, COCO_Category # Simple Non-Keypoint Example handler = COCO_Category_Handler([ COCO_Category(id=0, supercategory='bird', name='duck'), COCO_Category(id=1, supercategory='bird', name='sparrow'), COCO_Category(id=1, supercategory='bird', name='pigeon') ]) handler.save_to_path('birds0.json') # Simple Non-Keypoint Example using for loop handler = COCO_Category_Handler() for name in ['duck', 'sparrow', 'pigeon']: handler.append( COCO_Category(id=len(handler), supercategory='bird', name=name)) handler.save_to_path('birds1.json') # Keypoint Example handler = COCO_Category_Handler([ COCO_Category( id=0, supercategory='pet', name='dog', keypoints=[ # The keypoint labels are defined here 'left_eye', 'right_eye', # 0, 1 'mouth_left', 'mouth_center', 'mouth_right' # 2, 3, 4 ],
temp_parts = temp.split('_') if len(temp_parts) == 2: obj_type, obj_name, instance_name = target_obj_type, temp_parts[ 1], temp_parts[0] ann_obj.class_name = f'{obj_type}_{obj_name}_{instance_name}' elif len(temp_parts) == 3: obj_type, obj_name, instance_name = target_obj_type, f'{temp_parts[1]}part{temp_parts[2]}', '0' ann_obj.class_name = f'{obj_type}_{obj_name}_{instance_name}' else: raise Exception # Convert To COCO Dataset dataset = Measure_COCO_Dataset.from_ndds( ndds_dataset=ndds_dataset, categories=COCO_Category_Handler.load_from_path( '/home/clayton/workspace/prj/data_keep/data/ndds/categories/measure_all.json' ), naming_rule='type_object_instance_contained', delimiter='_', ignore_unspecified_categories=True, show_pbar=True, bbox_area_threshold=1, default_visibility_threshold=0.10, visibility_threshold_dict={'measure': 0.01}, allow_unfound_seg=True, class_merge_map={ 'mark_10th_place': 'seg_measure', 'marking_bottom': 'seg_measure', 'marking_top': 'seg_measure', 'hook': 'seg_measure' })
# printj.yellow( ann_obj.class_name) else: logger.error(f'ann_obj.class_name: {ann_obj.class_name}') # raise Exception # Delete Duplicate Objects frame.ndds_ann.objects.delete_duplicates(verbose=True, verbose_ref=frame.img_path) # ndds_dataset.save_to_path(save_path=f'{coco_data_dir}/hook_fixed_ndds.json', overwrite=True) # Convert To COCO Dataset dataset = COCO_Dataset.from_ndds( ndds_dataset=ndds_dataset, # categories=COCO_Category_Handler.load_from_path(f'/home/jitesh/3d/data/categories/hook_7ckpt.json'), categories=COCO_Category_Handler.load_from_path( f'/home/jitesh/3d/data/categories/tropicana.json'), naming_rule='type_object_instance_contained', ignore_unspecified_categories=True, show_pbar=True, bbox_area_threshold=1, default_visibility_threshold=-1, allow_unfound_seg=True, ) make_dir_if_not_exists(coco_data_dir) img_path = f'{coco_data_dir}/img' make_dir_if_not_exists(coco_data_dir) ann_dir = f'{coco_data_dir}/json' make_dir_if_not_exists(ann_dir) dataset.move_images(dst_img_dir=img_path, preserve_filenames=False, update_img_paths=True,
elif ann_obj.class_name.startswith('kpt-ob'): obj_type, obj_name = 'kpt', 'mark-outer' contained_name = 'ob' instance_name = str(0) ann_obj.class_name = f'{obj_type}_{obj_name}_{instance_name}_{contained_name}' for idx in delete_idx_list[::-1]: del ndds_dataset.frames[idx] print(f"ID deleted {idx}") # Bolt ROI Dataset Creation logger.info('Creating Bolt ROI Dataset') bolt_roi_categories = COCO_Category_Handler() print(f"Bolt_roi_categories :{bolt_roi_categories}") bolt_roi_categories.append( COCO_Category( id=len(bolt_roi_categories), name='bolt-roi' ) ) print(f"Bolt_roi_categories :{bolt_roi_categories}") bolt_roi_dataset = COCO_Dataset.from_ndds( ndds_dataset=ndds_dataset, categories=bolt_roi_categories, naming_rule='type_object_instance_contained', delimiter='_', ignore_unspecified_categories=True, show_pbar=True,
from annotation_utils.labelme.structs import LabelmeAnnotationHandler from annotation_utils.coco.structs import COCO_Dataset, COCO_Category_Handler # Path Variables img_dir = '' json_dir = '' categories_conf_path = '' # Load Labelme Annotation Handler labelme_handler = LabelmeAnnotationHandler.load_from_dir(load_dir=json_dir) # Load COCO Dataset from Labelme Annotation Handler coco_dataset = COCO_Dataset.from_labelme( labelme_handler=labelme_handler, categories=COCO_Category_Handler.load_from_path(categories_conf_path), img_dir=img_dir ) # Save COCO Dataset to a file coco_dataset.save_to_path(save_path='', overwrite=True) # Preview COCO Dataset coco_dataset.display_preview(show_details=True) coco_dataset.save_visualization(show_details=True) # coco_dataset.save_video(show_details=True)
dst_dir = 'cup_dataset' # Load NDDS Dataset ndds_dataset = NDDS_Dataset.load_from_dir(json_dir=src_dir, show_pbar=True) # Fix NDDS Dataset naming so that it follows convention. (This is not necessary if the NDDS dataset already follows the naming convention.) for frame in ndds_dataset.frames: # Fix Naming Convention for ann_obj in frame.ndds_ann.objects: if ann_obj.class_name.startswith('cup'): obj_type, obj_name = 'seg', 'cup' instance_name = '0' ann_obj.class_name = f'{obj_type}_{obj_name}_{instance_name}' # Convert To COCO Dataset cup_categories = COCO_Category_Handler() cup_categories.append(COCO_Category(id=len(cup_categories), name='cup')) dataset = COCO_Dataset.from_ndds(ndds_dataset=ndds_dataset, categories=cup_categories, naming_rule='type_object_instance_contained', show_pbar=True, bbox_area_threshold=-1, allow_unfound_seg=True) dataset.move_images(dst_img_dir=dst_dir, preserve_filenames=True, overwrite_duplicates=False, update_img_paths=True, overwrite=True, show_pbar=True) dataset.save_to_path(f'{dst_dir}/output.json', overwrite=True) # dataset.display_preview(show_details=True)
from annotation_utils.coco.structs import COCO_Dataset # Load NDDS Dataset ndds_dataset = NDDS_Dataset.load_from_dir( json_dir='/home/clayton/workspace/prj/data_keep/data/ndds/type1', show_pbar=True) # Fix NDDS Dataset naming so that it follows convention. (This is not necessary if the NDDS dataset already follows the naming convention.) for frame in ndds_dataset.frames: for ann_obj in frame.ndds_ann.objects: if ann_obj.class_name == 'bolt': obj_type, obj_name = 'seg', 'bolt-roi' ann_obj.class_name = f'{obj_type}_{obj_name}' # Define COCO Categories categories = COCO_Category_Handler() categories.append(COCO_Category(id=len(categories), name='bolt-roi')) # Convert To COCO Dataset dataset = COCO_Dataset.from_ndds(ndds_dataset=ndds_dataset, categories=categories, naming_rule='type_object_instance_contained', delimiter='_', ignore_unspecified_categories=True, show_pbar=True, bbox_area_threshold=1, default_visibility_threshold=0.10, visibility_threshold_dict={'bolt-roi': 0.01}, allow_unfound_seg=True, class_merge_map={ 'mark1': 'seg_bolt-roi',
from annotation_utils.coco.structs import COCO_Category_Handler, COCO_Category handler = COCO_Category_Handler() handler.append( COCO_Category(supercategory='screw', name='screw', keypoints=[], skeleton=[], id=len(handler))) handler.append( COCO_Category(supercategory='hole', name='hole', keypoints=[], skeleton=[], id=len(handler))) handler.save_to_path('interphone_ng_object_categories.json')
from annotation_utils.coco.structs import COCO_Category_Handler, COCO_Category handler = COCO_Category_Handler() handler.append( COCO_Category.from_label_skeleton( supercategory='', # Supercategory Name name='bracket_light', # Object Name id=len(handler), label_skeleton='' ) ) handler.save_to_path(save_path='hien_all_2_symbol_categories.json', overwrite=True) # Save Categorie