def from_json(self, json: Json) -> ROCOObject: t: ObjectType = ObjectType(json["name"]) x1, y1, x2, y2 = ( int(float(json["bndbox"]["xmin"])), int(float(json["bndbox"]["ymin"])), int(float(json["bndbox"]["xmax"])), int(float(json["bndbox"]["ymax"])), ) x1, y1 = max(0, x1), max(0, y1) x2, y2 = min(x2, self.image_w - 1), min(y2, self.image_h - 1) box = Box.from_positions(x1, y1, x2, y2) if t is not ObjectType.ARMOR: return ROCOObject(type=t, box=box) armor_number = int(json["armor_class"]) if json["armor_class"] != "none" else 0 return Armor( type=t, box=box, number=armor_number, digit=ArmorDigit.from_number(armor_number), color=ArmorColor(json["armor_color"]), )
def _scale_annotation(annotation: ROCOAnnotation, height: int, width: int): vertical_ratio, horizontal_ratio = height / annotation.h, width / annotation.w for obj in annotation.objects: obj.box = Box.from_positions( x1=int(obj.box.x1 * horizontal_ratio), y1=int(obj.box.y1 * vertical_ratio), x2=int(obj.box.x2 * horizontal_ratio), y2=int(obj.box.y2 * vertical_ratio), ) annotation.w, annotation.h = width, height
def crop_image_annotation( image: Image, annotation: ROCOAnnotation, box: Box, min_coverage: float, name: str ) -> Tuple[Image, ROCOAnnotation, str]: objects = InBoxObjectFilter(box, min_coverage).filter(annotation.objects) objects = [copy(o) for o in objects] for obj in objects: obj.box = Box.from_positions( x1=max(0, obj.box.x1 - box.x1), y1=max(0, obj.box.y1 - box.y1), x2=min(box.x2, obj.box.x2 - box.x1), y2=min(box.y2, obj.box.y2 - box.y1), ) return ( image[box.y1 : box.y2, box.x1 : box.x2], ROCOAnnotation(w=box.w, h=box.h, objects=objects, has_rune=False), name, )
def make_robots_and_armors( self, objects_params: List[ObjectParams], image: Image ) -> Tuple[List[DetectedRobot], List[DetectedArmor]]: image_height, image_width, *_ = image.shape robots, armors = [], [] for object_params in objects_params: object_type = ObjectType(self.label_map.name_of(object_params.object_class_id)) box = Box.from_positions( # TODO what about using relative coordinates ? x1=int(object_params.xmin * image_width), y1=int(object_params.ymin * image_height), x2=int(object_params.xmax * image_width), y2=int(object_params.ymax * image_height), ) if object_type is ObjectType.ARMOR: armors.append(DetectedArmor(object_type, box, object_params.score)) else: robots.append(DetectedRobot(object_type, box, object_params.score)) return robots, armors