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 adjust_center_to_src_img(self): h, w = self.src_img.shape[:2] if self.center is None: self.center_to_cener_of(self.src_img) cx, cy = self.center.to_list() cx = cx if cx < w else w - 1 cy = cy if cy < h else h - 1 self.update_center(Point2D(x=cx, y=cy))
def update_src_coco(self, src_coco_img: COCO_Image, src_coco_ann: COCO_Annotation): self.update_src_coco_img(src_coco_img=src_coco_img) self.update_src_img(src_img=cv2.imread(self.src_coco_img.coco_url)) self.update_src_coco_ann(src_coco_ann=src_coco_ann) self.update_center( center=Point2D.from_list(src_coco_ann.bbox.center())) self.adjust_center_to_src_img() self.check_ids_are_valid() self.update_zoom()
def from_dict(cls, item_dict: dict) -> Linemod_Annotation: return Linemod_Annotation( data_root=item_dict['data_root'], mask_path=item_dict['mask_path'], depth_path=item_dict['depth_path'] if 'depth_path' in item_dict else None, type=item_dict['type'], class_name=item_dict['cls'], corner_2d=Point2D_List.from_list(item_dict['corner_2d'], demarcation=True), corner_3d=Point3D_List.from_list(item_dict['corner_3d'], demarcation=True), center_2d=Point2D.from_list(item_dict['center_2d']), center_3d=Point3D.from_list(item_dict['center_3d']), fps_2d=Point2D_List.from_list(item_dict['fps_2d'], demarcation=True), fps_3d=Point3D_List.from_list(item_dict['fps_3d'], demarcation=True), K=LinemodCamera.from_matrix(np.array(item_dict['K'])), pose=QuaternionList.from_list(item_dict['pose']), image_id=item_dict['image_id'], category_id=item_dict['category_id'], id=item_dict['id'])
def src_coco_img(self, src_coco_img: COCO_Image): self.update_src_coco_img(src_coco_img=src_coco_img) self.update_src_img(src_img=cv2.imread(self.src_coco_img.coco_url)) self.update_center(center=Point2D(x=int(src_coco_img.width / 2), y=int(src_coco_img.height / 2))) self.adjust_center_to_src_img()
def center_to_cener_of(self, img: np.ndarray): img_h, img_w = img.shape[:2] self.update_center(Point2D(x=int(img_w / 2), y=int(img_h / 2)))
def from_dict(cls, item_dict: dict) -> COCO_Zoom: return COCO_Zoom(magnitude=item_dict['magnitude'], center=Point2D.from_list(item_dict['center']) if 'center' in item_dict else None)
) cropped_mark_dataset.images.append(cropped_coco_image) mark_ann_found = False mark_ann_list = cast(List[COCO_Annotation], []) for i in list(range(len(mark_anns)))[::-1]: if roi_ann.bbox.contains(mark_anns[i].bbox): mark_ann_found = True # mark_ann = mark_anns[i].copy() mark_ann_list.append( mark_anns[i].copy()) del mark_anns[i] if not mark_ann_found: raise Exception for mark_ann in mark_ann_list: mark_ann.segmentation = mark_ann.segmentation - Point2D(x=roi_ann.bbox.xmin, y=roi_ann.bbox.ymin) mark_ann.bbox = mark_ann.segmentation.to_bbox() mark_ann.keypoints = mark_ann.keypoints - Point2D(x=roi_ann.bbox.xmin, y=roi_ann.bbox.ymin) cropped_coco_ann = COCO_Annotation( id=len(cropped_mark_dataset.annotations), category_id=mark_ann.category_id, image_id=cropped_coco_image.id, segmentation=mark_ann.segmentation, bbox=mark_ann.bbox, area=mark_ann.bbox.area(), keypoints=mark_ann.keypoints, num_keypoints=len(mark_ann.keypoints), keypoints_3d=mark_ann.keypoints_3d ) cropped_mark_dataset.annotations.append(cropped_coco_ann) crop_pbar.update()
def _process_organized_part(self, organized_part: dict, frame_img: np.ndarray, whole_number_coco_image: COCO_Image, measure_ann: COCO_Annotation, whole_number_count: int, digit_dir: str): whole_number_cat = self.whole_number_dataset.categories.get_unique_category_from_name( 'whole_number') whole_number_seg = None whole_number_abs_bbox = None seg_len_list = [ len(part_ann.segmentation) for part_ann in organized_part['anns'] ] if all([seg_len > 0 for seg_len in seg_len_list]): target = 'seg' elif all([seg_len == 0 for seg_len in seg_len_list]): target = 'bbox' else: logger.warning( f"There are empty segmentations among organized_part['anns'].") logger.warning( f"Cannot determine whether to merge segmentations or bounding boxes." ) logger.warning( f"[len(part_ann.segmentation) for part_ann in organized_part['anns']]: {[len(part_ann.segmentation) for part_ann in organized_part['anns']]}" ) logger.warning( f"organized_part['whole_names']: {organized_part['whole_names']}" ) logger.warning( f"organized_part['part_names']: {organized_part['part_names']}" ) logger.warning(f"Assuming target = 'bbox'") target = 'bbox' if target == 'seg': for part_ann in organized_part['anns']: whole_number_seg = whole_number_seg + part_ann.segmentation if whole_number_seg is not None else part_ann.segmentation whole_number_abs_bbox = whole_number_seg.to_bbox().to_int() elif target == 'bbox': whole_number_seg = Segmentation() for part_ann in organized_part['anns']: whole_number_abs_bbox = whole_number_abs_bbox + part_ann.bbox if whole_number_abs_bbox is not None else part_ann.bbox whole_number_abs_bbox = whole_number_abs_bbox.to_int() else: raise Exception whole_number_bbox_region = frame_img[ whole_number_abs_bbox.ymin:whole_number_abs_bbox.ymax, whole_number_abs_bbox.xmin:whole_number_abs_bbox.xmax, :] whole_number_img_save_path = f'{digit_dir}/{get_rootname_from_filename(whole_number_coco_image.file_name)}_{whole_number_count}.{get_extension_from_filename(whole_number_coco_image.file_name)}' self._save_image(img=whole_number_bbox_region, save_path=whole_number_img_save_path) digit_coco_image = COCO_Image.from_img_path( img_path=whole_number_img_save_path, license_id=0, image_id=len(self.digit_dataset.images)) self.digit_dataset.images.append(digit_coco_image) measure_bbox_ref_point = Point2D(x=measure_ann.bbox.xmin, y=measure_ann.bbox.ymin) if target == 'seg': whole_number_seg = whole_number_seg - measure_bbox_ref_point # relative to measure bbox whole_number_bbox = whole_number_seg.to_bbox( ) # relative to measure bbox elif target == 'bbox': whole_number_seg = Segmentation() # Empty whole_number_bbox = whole_number_abs_bbox - measure_bbox_ref_point # relative to measure bbox else: raise Exception whole_number_coco_ann = COCO_Annotation( id=len(self.whole_number_dataset.annotations), category_id=whole_number_cat.id, image_id=whole_number_coco_image.id, segmentation=whole_number_seg, bbox=whole_number_bbox, area=whole_number_bbox.area()) self.whole_number_dataset.annotations.append(whole_number_coco_ann) for part_ann in organized_part['anns']: part_ann = COCO_Annotation.buffer(part_ann) coco_cat = self.categories.get_obj_from_id(part_ann.category_id) digit_cat = self.digit_dataset.categories.get_unique_category_from_name( coco_cat.name.split('part')[-1]) whole_number_bbox_ref_point = measure_bbox_ref_point + Point2D( x=whole_number_bbox.xmin, y=whole_number_bbox.ymin) if target == 'seg': digit_seg = part_ann.segmentation - whole_number_bbox_ref_point digit_bbox = digit_seg.to_bbox() elif target == 'bbox': digit_seg = Segmentation() digit_bbox = part_ann.bbox - whole_number_bbox_ref_point else: raise Exception digit_coco_ann = COCO_Annotation(id=len( self.digit_dataset.annotations), category_id=digit_cat.id, image_id=digit_coco_image.id, segmentation=digit_seg, bbox=digit_bbox, area=digit_bbox.area()) self.digit_dataset.annotations.append(digit_coco_ann)
def _process_single_digit_ann(self, frame_img: np.ndarray, whole_number_coco_image: COCO_Image, whole_ann: COCO_Annotation, measure_ann: COCO_Annotation, whole_number_count: int, digit_dir: str): coco_cat = self.categories.get_obj_from_id(whole_ann.category_id) whole_number_cat = self.whole_number_dataset.categories.get_unique_category_from_name( 'whole_number') # Update Digit Image Handler orig_bbox = whole_ann.bbox.to_rounded_int(special=True) if orig_bbox.area() == 0: logger.error(f'Encountered orig_bbox.area() == 0') logger.error(f'orig_bbox: {orig_bbox}') logger.error(f'whole_ann.bbox: {whole_ann.bbox}') raise Exception whole_number_bbox_region = frame_img[orig_bbox.ymin:orig_bbox.ymax, orig_bbox.xmin:orig_bbox.xmax, :] whole_number_img_save_path = f'{digit_dir}/{get_rootname_from_filename(whole_number_coco_image.file_name)}_{whole_number_count}.{get_extension_from_filename(whole_number_coco_image.file_name)}' self._save_image(img=whole_number_bbox_region, save_path=whole_number_img_save_path) digit_coco_image = COCO_Image.from_img_path( img_path=whole_number_img_save_path, license_id=0, image_id=len(self.digit_dataset.images)) self.digit_dataset.images.append(digit_coco_image) # Update Whole Number Annotation Handler measure_bbox = measure_ann.bbox measure_bbox_ref_point = Point2D(x=measure_bbox.xmin, y=measure_bbox.ymin) if len(whole_ann.segmentation) != 0: # Segmentation Based whole_number_seg = whole_ann.segmentation - measure_bbox_ref_point whole_number_bbox = whole_number_seg.to_bbox() else: # BBox Based (No Segmentation) whole_number_seg = whole_ann.segmentation.copy() whole_number_bbox = whole_ann.bbox - measure_bbox_ref_point whole_number_coco_ann = COCO_Annotation( id=len(self.whole_number_dataset.annotations), category_id=whole_number_cat.id, image_id=whole_number_coco_image.id, segmentation=whole_number_seg, bbox=whole_number_bbox, area=whole_number_bbox.area()) self.whole_number_dataset.annotations.append(whole_number_coco_ann) # Update Digit Annotation Handler digit_cat = self.digit_dataset.categories.get_unique_category_from_name( coco_cat.name) whole_number_bbox_ref_point = measure_bbox_ref_point + Point2D( x=whole_number_bbox.xmin, y=whole_number_bbox.ymin) if len(whole_ann.segmentation) != 0: # Segmentation Based digit_seg = whole_ann.segmentation - whole_number_bbox_ref_point digit_bbox = digit_seg.to_bbox() else: # BBox Based (No Segmentation) digit_seg = whole_ann.segmentation.copy() digit_bbox = whole_ann.bbox - whole_number_bbox_ref_point digit_coco_ann = COCO_Annotation(id=len( self.digit_dataset.annotations), category_id=digit_cat.id, image_id=digit_coco_image.id, segmentation=digit_seg, bbox=digit_bbox, area=digit_bbox.area()) self.digit_dataset.annotations.append(digit_coco_ann)
from logger import logger from common_utils.common_types.point import Point2D, Point3D, Point2D_List, Point3D_List from common_utils.common_types.keypoint import Keypoint2D, Keypoint3D, Keypoint2D_List, Keypoint3D_List from common_utils.common_types.bbox import BBox from common_utils.common_types.segmentation import Polygon, Segmentation const_int = 10 const_float = 20.0 pt2d_0 = Point2D(x=1.0, y=2.0) pt2d_1 = Point2D(x=3.0, y=4.0) assert pt2d_0 + pt2d_1 == Point2D(x=pt2d_0.x + pt2d_1.x, y=pt2d_0.y + pt2d_1.y) assert pt2d_0 + const_int == Point2D(x=pt2d_0.x + const_int, y=pt2d_0.y + const_int) assert pt2d_0 + const_float == Point2D(x=pt2d_0.x + const_float, y=pt2d_0.y + const_float) assert pt2d_0 - pt2d_1 == Point2D(x=pt2d_0.x - pt2d_1.x, y=pt2d_0.y - pt2d_1.y) assert pt2d_0 - const_int == Point2D(x=pt2d_0.x - const_int, y=pt2d_0.y - const_int) assert pt2d_0 - const_float == Point2D(x=pt2d_0.x - const_float, y=pt2d_0.y - const_float) assert pt2d_0 * const_int == Point2D(x=pt2d_0.x * const_int, y=pt2d_0.y * const_int) assert pt2d_0 * const_float == Point2D(x=pt2d_0.x * const_float, y=pt2d_0.y * const_float) assert pt2d_0 / const_int == Point2D(x=pt2d_0.x / const_int, y=pt2d_0.y / const_int) assert pt2d_0 / const_float == Point2D(x=pt2d_0.x / const_float,