def from_dict(self, object_dict: dict) -> NDDS_Annotation_Object: check_required_keys(object_dict, required_keys=[ 'class', 'instance_id', 'visibility', 'location', 'quaternion_xyzw', 'pose_transform', 'cuboid_centroid', 'projected_cuboid_centroid', 'bounding_box', 'cuboid', 'projected_cuboid' ]) check_required_keys(object_dict['bounding_box'], required_keys=['top_left', 'bottom_right']) return NDDS_Annotation_Object( class_name=object_dict['class'], instance_id=object_dict['instance_id'], visibility=object_dict['visibility'], location=Point3D.from_list(object_dict['location']), quaternion_xyzw=Quaternion.from_list( object_dict['quaternion_xyzw']), pose_transform=np.array(object_dict['pose_transform']), cuboid_centroid=Point3D.from_list(object_dict['cuboid_centroid']), projected_cuboid_centroid=Point2D.from_list( object_dict['projected_cuboid_centroid']), bounding_box=BBox.from_list( object_dict['bounding_box']['top_left'] + object_dict['bounding_box']['bottom_right'], input_format='pminpmax'), cuboid=Cuboid3D.from_list(object_dict['cuboid'], demarcation=True), projected_cuboid=Cuboid2D.from_list( object_dict['projected_cuboid'], demarcation=True))
def from_dict(cls, category_dict: dict, strict: bool = True) -> COCO_Category: if strict: check_required_keys(category_dict, required_keys=[ 'supercategory', 'id', 'name', 'keypoints', 'skeleton' ]) return COCO_Category( supercategory=category_dict['supercategory'], id=category_dict['id'], name=category_dict['name'], keypoints=category_dict['keypoints'], skeleton=category_dict['skeleton'], ) else: check_required_keys(category_dict, required_keys=['id']) return COCO_Category(id=category_dict['id'], supercategory=category_dict['supercategory'] if 'supercategory' in category_dict else None, name=category_dict['name'] if 'name' in category_dict else None, keypoints=category_dict['keypoints'] if 'keypoints' in category_dict else None, skeleton=category_dict['skeleton'] if 'skeleton' in category_dict else None)
def from_dict(cls, ann_dict: dict) -> Detectron2_Annotation: check_required_keys( ann_dict, required_keys=['iscrowd', 'bbox', 'category_id', 'bbox_mode']) if ann_dict['bbox_mode'] == BoxMode.XYWH_ABS: bbox = BBox.from_list(ann_dict['bbox'], input_format='pminsize') elif ann_dict['bbox_mode'] == BoxMode.XYXY_ABS: bbox = BBox.from_list(ann_dict['bbox'], input_format='pminpmax') else: raise NotImplementedError if 'keypoints' in ann_dict: keypoints = Keypoint2D_List.from_list( value_list=ann_dict['keypoints'], demarcation=False) else: keypoints = Keypoint2D_List() return Detectron2_Annotation(iscrowd=ann_dict['iscrowd'], bbox=bbox, keypoints=keypoints, category_id=ann_dict['category_id'], segmentation=Segmentation.from_list( points_list=ann_dict['segmentation'] if 'segmentation' in ann_dict else [], demarcation=False), bbox_mode=ann_dict['bbox_mode'])
def from_dict(cls, item_dict: dict) -> NDDS_Dataset: check_required_keys( item_dict, required_keys=['camera_config', 'obj_config', 'frames']) return NDDS_Dataset( camera_config=CameraConfig.from_dict(item_dict['camera_config']), obj_config=ObjectSettings.from_dict(item_dict['obj_config']), frames=NDDS_Frame_Handler.from_dict_list(item_dict['frames']))
def from_dict(cls, item_dict: dict) -> ObjectSettings: check_required_keys( item_dict, required_keys=['exported_object_classes', 'exported_objects']) return ObjectSettings( exported_object_classes=item_dict['exported_object_classes'], exported_objects=ExportedObjectHandler.from_dict_list( item_dict['exported_objects']))
def from_dict(self, item_dict: dict) -> BBoxResult: check_required_keys( item_dict, required_keys=['image_id', 'category_id', 'bbox', 'score']) return BBoxResult(image_id=item_dict['image_id'], category_id=item_dict['category_id'], bbox=BBox.from_list(bbox=item_dict['bbox'], input_format='pminsize'), score=item_dict['score'])
def from_dict(cls, shape_dict: dict) -> LabelmeShape: check_required_keys(shape_dict, required_keys=['label', 'points', 'group_id', 'shape_type', 'flags']) return LabelmeShape( label=shape_dict['label'], points=Point2D_List.from_list(value_list=shape_dict['points'], demarcation=True), shape_type=shape_dict['shape_type'], group_id=shape_dict['group_id'], flags=shape_dict['flags'] )
def from_dict(cls, item_dict: dict) -> CameraData: check_required_keys(item_dict, required_keys=[ 'location_worldframe', 'quaternion_xyzw_worldframe' ]) return CameraData(location_worldframe=Point3D.from_list( coords=item_dict['location_worldframe']), quaternion_xyzw_worldframe=Quaternion.from_list( coords=item_dict['quaternion_xyzw_worldframe']))
def from_dict(cls, labelme_dict: dict) -> LabelmeAnnotation: check_required_keys(labelme_dict, required_keys=['version', 'flags', 'shapes', 'imagePath', 'imageData', 'imageHeight', 'imageWidth']) return LabelmeAnnotation( version=labelme_dict['version'], flags=labelme_dict['flags'], shapes=LabelmeShapeHandler.from_dict_list(labelme_dict['shapes']), img_path=labelme_dict['imagePath'], img_data=labelme_dict['imageData'], img_h=labelme_dict['imageHeight'], img_w=labelme_dict['imageWidth'] )
def from_dict(cls, item_dict: dict) -> NDDS_Frame: check_required_keys(item_dict, required_keys=['img_path', 'ndds_ann']) return NDDS_Frame(img_path=item_dict['img_path'], ndds_ann=NDDS_Annotation.from_dict( item_dict['ndds_ann']), cs_img_path=item_dict['cs_img_path'] if 'cs_img_path' in item_dict else None, depth_img_path=item_dict['depth_img_path'] if 'depth_img_path' in item_dict else None, is_img_path=item_dict['is_img_path'] if 'is_img_path' in item_dict else None)
def from_dict(cls, item_dict: dict) -> CameraSettings: check_required_keys( item_dict, required_keys=['name', 'horizontal_fov', 'intrinsic_settings', 'captured_image_size'] ) return CameraSettings( name=item_dict['name'], horizontal_fov=item_dict['horizontal_fov'], intrinsic_settings=IntrinsicSettings.from_dict(item_dict['intrinsic_settings']), captured_image_size=CapturedImageSize.from_dict(item_dict['captured_image_size']) )
def from_dict(self, ann_dict: dict) -> NDDS_Annotation: check_required_keys( ann_dict, required_keys=[ 'camera_data', 'objects' ] ) return NDDS_Annotation( camera_data=CameraData.from_dict(ann_dict['camera_data']), objects=NDDS_Annotation_Object_Handler.from_dict_list(ann_dict['objects']) )
def from_dict(cls, intrinsic_param_dict: dict) -> Camera: check_type(intrinsic_param_dict, valid_type_list=[dict]) if len(intrinsic_param_dict) > 0: check_required_keys(intrinsic_param_dict, required_keys=['f', 'c', 'T']) check_type_from_list(list(intrinsic_param_dict.values()), valid_type_list=[list]) return Camera(f=intrinsic_param_dict['f'], c=intrinsic_param_dict['c'], T=intrinsic_param_dict['T']) else: return None
def from_dict(cls, info_dict: dict) -> COCO_Info: check_required_keys(info_dict, required_keys=[ 'description', 'url', 'version', 'year', 'contributor', 'date_created' ]) return COCO_Info(description=info_dict['description'], url=info_dict['url'], version=info_dict['version'], year=info_dict['year'], contributor=info_dict['contributor'], date_created=info_dict['date_created'])
def from_dict(self, item_dict: dict) -> ExportedObject: check_required_keys(item_dict, required_keys=[ 'class', 'segmentation_class_id', 'segmentation_instance_id', 'fixed_model_transform', 'cuboid_dimensions' ]) return ExportedObject( class_name=item_dict['class'], segmentation_class_id=item_dict['segmentation_class_id'], segmentation_instance_id=item_dict['segmentation_instance_id'], fixed_model_transform=np.array(item_dict['fixed_model_transform']), cuboid_dimensions=item_dict['cuboid_dimensions'])
def from_dict(cls, ann_dict: dict) -> Detectron2_Annotation_Dict: check_required_keys(ann_dict, required_keys=[ 'file_name', 'height', 'width', 'image_id', 'annotations' ]) return Detectron2_Annotation_Dict( file_name=ann_dict['file_name'], height=ann_dict['height'], width=ann_dict['width'], image_id=ann_dict['image_id'], annotations=Detectron2_Annotation_List.from_dict_list( dict_list=ann_dict['annotations']))
def from_dict(self, item_dict: dict) -> KeypointResult: check_required_keys( item_dict, required_keys=['image_id', 'category_id', 'keypoints']) if 'score' not in item_dict and 'score_list' not in item_dict: logger.error( f"Need to provide a dictionary with either a 'score' key or a 'score_list' key." ) raise Exception return KeypointResult(image_id=item_dict['image_id'], category_id=item_dict['category_id'], keypoints=Keypoint2D_List.from_list( item_dict['keypoints'], demarcation=False), score=item_dict['score'])
def from_dict(cls, ann_dict: dict, strict: bool = True) -> COCO_Annotation: if strict: check_required_keys(ann_dict, required_keys=[ 'segmentation', 'num_keypoints', 'area', 'iscrowd', 'keypoints', 'image_id', 'bbox', 'category_id', 'id' ]) return COCO_Annotation( segmentation=Segmentation.from_list(ann_dict['segmentation'], demarcation=False), num_keypoints=ann_dict['num_keypoints'], area=ann_dict['area'], iscrowd=ann_dict['iscrowd'], keypoints=Keypoint2D_List.from_list(ann_dict['keypoints'], demarcation=False), image_id=ann_dict['image_id'], bbox=BBox.from_list(ann_dict['bbox'], input_format='pminsize'), category_id=ann_dict['category_id'], id=ann_dict['id'], keypoints_3d=Keypoint3D_List.from_list( ann_dict['keypoints_3d'], demarcation=False) if 'keypoints_3d' in ann_dict else None, camera=Camera.from_dict(ann_dict['camera_params']) if 'camera_params' in ann_dict else None) else: check_required_keys( ann_dict, required_keys=['id', 'category_id', 'image_id']) return COCO_Annotation( segmentation=Segmentation.from_list(ann_dict['segmentation'], demarcation=False) if 'segmentation' in ann_dict else None, num_keypoints=ann_dict['num_keypoints'] if 'num_keypoints' in ann_dict else None, area=ann_dict['area'] if 'area' in ann_dict else None, iscrowd=ann_dict['iscrowd'] if 'iscrowd' in ann_dict else None, keypoints=Keypoint2D_List.from_list(ann_dict['keypoints'], demarcation=False) if 'keypoints' in ann_dict else None, image_id=ann_dict['image_id'], bbox=BBox.from_list(ann_dict['bbox'], input_format='pminsize') if 'bbox' in ann_dict else None, category_id=ann_dict['category_id'], id=ann_dict['id'], keypoints_3d=Keypoint3D_List.from_list( ann_dict['keypoints_3d'], demarcation=False) if 'keypoints_3d' in ann_dict else None, camera=Camera.from_dict(ann_dict['camera_params']) if 'camera_params' in ann_dict else None)
def from_dict(cls, image_dict: dict) -> COCO_Image: check_required_keys(image_dict, required_keys=[ 'license', 'file_name', 'coco_url', 'height', 'width', 'date_captured', 'id' ]) return COCO_Image( license_id=image_dict['license'], file_name=image_dict['file_name'], coco_url=image_dict['coco_url'], height=image_dict['height'], width=image_dict['width'], date_captured=image_dict['date_captured'], flickr_url=image_dict['flickr_url'] if 'flickr_url' in image_dict else None, id=image_dict['id'], )
def from_dict(cls: T, item_dict: dict) -> T: constructor_dict = {} constructor_params = cls.get_constructor_params() unnecessary_params = [] for key, value in item_dict.items(): if key in constructor_params: constructor_dict[key] = value else: unnecessary_params.append(key) if len(unnecessary_params) > 0: logger.warning( f'Received unnecessary parameters in {cls.__name__}.from_dict') logger.warning(f'Received: {list(item_dict.keys())}') logger.warning(f'Expected: {constructor_params}') logger.warning(f'Extra: {unnecessary_params}') check_required_keys(constructor_dict, required_keys=constructor_params) return cls(*constructor_dict.values())
def from_dict(cls: T, item_dict: dict) -> T: """ Note: It is required that all class constructor parameters be of a JSON serializable datatype. If not, it is necessary to override this classmethod. """ constructor_dict = {} constructor_params = cls.get_constructor_params() unnecessary_params = [] for key, value in item_dict.items(): if key in constructor_params: constructor_dict[key] = value else: unnecessary_params.append(key) if len(unnecessary_params) > 0: logger.warning(f'Received unnecessary parameters in {cls.__name__}.from_dict') logger.warning(f'Received: {list(item_dict.keys())}') logger.warning(f'Expected: {constructor_params}') logger.warning(f'Extra: {unnecessary_params}') check_required_keys(constructor_dict, required_keys=constructor_params) return cls(*constructor_dict.values())
def from_dict(cls, ann_dict: dict, strict: bool = True) -> COCO_Annotation: if strict: check_required_keys(ann_dict, required_keys=[ 'segmentation', 'num_keypoints', 'area', 'iscrowd', 'keypoints', 'image_id', 'bbox', 'category_id', 'id' ]) return COCO_Annotation( segmentation=Segmentation.from_list(ann_dict['segmentation'], demarcation=False), num_keypoints=ann_dict['num_keypoints'], area=ann_dict['area'], iscrowd=ann_dict['iscrowd'], keypoints=Keypoint2D_List.from_list(ann_dict['keypoints'], demarcation=False), image_id=ann_dict['image_id'], bbox=BBox.from_list(ann_dict['bbox'], input_format='pminsize'), category_id=ann_dict['category_id'], id=ann_dict['id'], keypoints_3d=Keypoint3D_List.from_list( ann_dict['keypoints_3d'], demarcation=False) if 'keypoints_3d' in ann_dict else None, camera=Camera.from_dict(ann_dict['camera_params']) if 'camera_params' in ann_dict else None) else: check_required_keys( ann_dict, required_keys=['id', 'category_id', 'image_id']) if 'segmentation' not in ann_dict: seg = None elif ann_dict['iscrowd'] == 1 and type( ann_dict['segmentation']) == dict: compressed_rle = mask.frPyObjects( ann_dict['segmentation'], ann_dict['segmentation']['size'][0], ann_dict['segmentation']['size'][1]) seg_mask = mask.decode(compressed_rle) contours, _ = cv2.findContours(seg_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) seg = Segmentation.from_contour(contour_list=contours) else: seg = Segmentation.from_list(ann_dict['segmentation'], demarcation=False) return COCO_Annotation( segmentation=seg, num_keypoints=ann_dict['num_keypoints'] if 'num_keypoints' in ann_dict else None, area=ann_dict['area'] if 'area' in ann_dict else None, iscrowd=ann_dict['iscrowd'] if 'iscrowd' in ann_dict else None, keypoints=Keypoint2D_List.from_list(ann_dict['keypoints'], demarcation=False) if 'keypoints' in ann_dict else None, image_id=ann_dict['image_id'], bbox=BBox.from_list(ann_dict['bbox'], input_format='pminsize') if 'bbox' in ann_dict else None, category_id=ann_dict['category_id'], id=ann_dict['id'], keypoints_3d=Keypoint3D_List.from_list( ann_dict['keypoints_3d'], demarcation=False) if 'keypoints_3d' in ann_dict else None, camera=Camera.from_dict(ann_dict['camera_params']) if 'camera_params' in ann_dict else None)
def from_dict(cls, collection_dict: dict, check_paths: bool = True) -> DatasetConfigCollection: check_required_keys(collection_dict, required_keys=[ 'collection_dir', 'dataset_names', 'dataset_specific' ]) collection_dir = collection_dict['collection_dir'] check_type(collection_dir, valid_type_list=[str]) dataset_names = collection_dict['dataset_names'] check_type(dataset_names, valid_type_list=[list]) check_type_from_list(dataset_names, valid_type_list=[str]) dataset_specific = collection_dict['dataset_specific'] check_type(dataset_specific, valid_type_list=[dict]) collection_tag = None if 'tag' not in collection_dict else collection_dict[ 'tag'] check_type(collection_tag, valid_type_list=[type(None), str]) check_required_keys( dataset_specific, required_keys=['img_dir', 'ann_path', 'ann_format']) img_dir = dataset_specific['img_dir'] check_type(img_dir, valid_type_list=[str, list]) if type(img_dir) is list: check_type_from_list(img_dir, valid_type_list=[str]) check_list_length(img_dir, correct_length=len(dataset_names)) ann_path = dataset_specific['ann_path'] check_type(ann_path, valid_type_list=[str, list]) if type(ann_path) is list: check_type_from_list(ann_path, valid_type_list=[str]) check_list_length(ann_path, correct_length=len(dataset_names)) ann_format = dataset_specific['ann_format'] check_type(ann_format, valid_type_list=[str, list]) if type(ann_format) is list: check_type_from_list(ann_format, valid_type_list=[str]) check_list_length(ann_format, correct_length=len(dataset_names)) dataset_tag = None if 'tag' not in dataset_specific else dataset_specific[ 'tag'] check_type(dataset_tag, valid_type_list=[type(None), str, list]) if type(dataset_tag) is list: check_type_from_list(dataset_tag, valid_type_list=[type(None), str]) check_list_length(dataset_tag, correct_length=len(dataset_names)) dataset_config_list = [] for i in range(len(dataset_names)): if type(img_dir) is str: img_dir0 = img_dir elif type(img_dir) is list: if i >= len(img_dir): raise IndexError img_dir0 = img_dir[i] else: raise Exception if type(ann_path) is str: ann_path0 = ann_path elif type(ann_path) is list: if i >= len(ann_path): raise IndexError ann_path0 = ann_path[i] else: raise Exception if type(ann_format) is str: ann_format0 = ann_format elif type(ann_format) is list: if i >= len(ann_format): raise IndexError ann_format0 = ann_format[i] else: raise Exception if type(dataset_tag) is str or dataset_tag is None: dataset_tag0 = dataset_tag elif type(dataset_tag) is list: if i >= len(dataset_tag): raise IndexError dataset_tag0 = dataset_tag[i] else: raise Exception img_dir1 = rel_to_abs_path( f'{collection_dir}/{dataset_names[i]}/{img_dir0}') ann_path1 = rel_to_abs_path( f'{collection_dir}/{dataset_names[i]}/{ann_path0}') if check_paths: check_dir_exists(img_dir1) check_file_exists(ann_path1) config = DatasetConfig(img_dir=img_dir1, ann_path=ann_path1, ann_format=ann_format0, tag=dataset_tag0) dataset_config_list.append(config) return DatasetConfigCollection(dataset_config_list=dataset_config_list, tag=collection_tag)
def from_dict(cls, item_dict: dict) -> CameraConfig: check_required_keys(item_dict, required_keys=['camera_settings']) return CameraConfig( camera_settings=CameraSettingsHandler.from_dict_list(item_dict['camera_settings']) )
def from_dict(cls, license_dict: dict) -> COCO_License: check_required_keys(license_dict, required_keys=['url', 'id', 'name']) return COCO_License(url=license_dict['url'], id=license_dict['id'], name=license_dict['name'])