def __init__(self, description: str = "Dataset Created Using annotation_utils", url: str = "https://github.com/cm107/annotation_utils", version: str = "1.0", year: str = get_present_year(), contributor: str = get_username(), date_created: str = get_present_time_Ymd()): super().__init__() self.description = description self.url = url self.version = version self.year = year self.contributor = contributor self.date_created = date_created
def from_scratch(self, description: str, url: str, version: str = '1.0') -> COCO_Field_Buffer: info = COCO_Info(description=description, url=url, version=version, year=get_present_year(), contributor=get_username(), date_created=get_present_time_Ymd()) licenses = COCO_License_Handler() images = COCO_Image_Handler() annotations = COCO_Annotation_Handler() categories = COCO_Category_Handler() return COCO_Field_Buffer(info=info, licenses=licenses, images=images, annotations=annotations, categories=categories)
def process(self, save_image_to_dir: bool = True): object_name = np.array([item.name for item in self.coco_category_list]) json_path_list = self.get_all_object_json_files() for i, json_path in enumerate(json_path_list): ann_dict = json.load(open(json_path, 'r')) camera_params = self.camera_params_from_dict(ann_dict=ann_dict) ann_object_list = ann_dict['objects'] image_location = os.path.abspath(json_path[:-5] + '.png') coco_image = CocoImage(license=1, file_name=os.path.basename(image_location), coco_url=image_location, height=camera_params.resx, width=camera_params.resy, date_captured=get_present_time_Ymd(), flickr_url=None, id=i) self.coco_image_list.append(coco_image) ndds_ann_object_list = [] for ann_object in ann_object_list: ndds_ann_object = NDDS_Annotation_Object.from_dict(ann_object) if any(ele in ndds_ann_object.class_name for ele in object_name): mask = np.array([ ele in ndds_ann_object.class_name for ele in object_name ]) object_name_single = object_name[mask][0] category_id = [ category.id for category in self.coco_category_list if category.name == object_name_single ][0] object_index = ndds_ann_object.class_name.replace( object_name_single, '') color_instance_rgb = self.segmentation_id_to_color( ndds_ann_object) # get segmentation and bbox instance_img = cv2.imread( coco_image.coco_url.replace('.png', '.is.png')) seg = self.get_instance_segmentation( img=instance_img, lower_thres=(int(color_instance_rgb[0] - 1), int(color_instance_rgb[1] - 1), int(color_instance_rgb[2] - 1)), upper_thresh=(int(color_instance_rgb[0] + 1), int(color_instance_rgb[1] + 1), int(color_instance_rgb[2] + 1))) if len(seg) == 0: print( "image segmentation not found, please check color code" ) continue outer_bbox = seg.to_bbox() if outer_bbox.area() < 10: print("object too small to be considered") continue keypoints, keypoints_3d = self.get_keypoints( ann_object_list=ann_object_list, object_name_single=object_name_single, object_index=object_index) coco_annotation = CocoAnnotation( bbox=outer_bbox.to_list(output_format='pminsize'), image_id=i, category_id=category_id, is_crowd=0, id=len(self.coco_annotation_list) + 1, keypoints=keypoints, keypoints_3d=keypoints_3d, segmentation=seg.to_list(), area=outer_bbox.area(), orientation_xyzw=ndds_ann_object.quaternion_xyzw. to_dict(), camera_params=camera_params.to_dict_fct(), num_keypoints=int(len(keypoints) / 3)) self.coco_annotation_list.append(coco_annotation) if save_image_to_dir: self.save_images() return CocoDataset(info=self.coco_info, licenses=self.coco_license_list, images=self.coco_image_list, annotations=self.coco_annotation_list, categories=self.coco_category_list), self.save_path