# default_exp bbox_annotator

Bounding Box Annotator

Bounding Box Annotator allows users to freely draw on top of images.

State

View

Controller

We have annotation saved in dictionary lile: {'path/to/imagename.jpg': {'x':0, 'y': 0, 'width': 100, 'heigth': 100}}

Navi widget has index and prev/next buttons to iterate over max_im_number of images (todo: change name as we can iterate of any object).

BBoxAnnotator has coupled index (with Navi one), and onchange event to update the current image path and label.

On image_path change event BBoxCanvas rerenders new image and label

#export

class BBoxAnnotator(Annotator):
    """
    Represents bounding box annotator.

    Gives an ability to itarate through image dataset,
    draw 2D bounding box annotations for object detection and localization,
    export final annotations in json format

    """

    def __init__(
        self,
        project_path: Path,
        input_item: InputImage,
        output_item: OutputImageBbox,
        annotation_file_path: Path,
        has_border: bool = False,
        *args, **kwargs
    ):
        app_state = AppWidgetState(
            uuid=str(id(self)),
            **{
                'size': (input_item.width, input_item.height),
            }
        )

        super().__init__(app_state)

        self._input_item = input_item
        self._output_item = output_item

        self.bbox_state = BBoxState(
            uuid=str(id(self)),
            classes=output_item.classes,
            drawing_enabled=self._output_item.drawing_enabled
        )

        self.storage = JsonCaptureStorage(
            im_dir=project_path / input_item.dir,
            annotation_file_path=annotation_file_path
        )

        self.controller = BBoxAnnotatorController(
            app_state=self.app_state,
            bbox_state=self.bbox_state,
            storage=self.storage,
            **kwargs
        )

        self.view = BBoxAnnotatorGUI(
            app_state=self.app_state,
            bbox_state=self.bbox_state,
            fit_canvas=self._input_item.fit_canvas,
            on_save_btn_clicked=self.controller.save_current_annotations,
            has_border=has_border
        )

        self.view.on_client_ready(self.controller.handle_client_ready)

    def __repr__(self):
        display(self.view)
        return ""

    def to_dict(self, only_annotated=True):
        return self.storage.to_dict(only_annotated)
bb.view._image_box.debug_output
bb.view._image_box._controller.debug_output
@pytest.fixture
def bbox_fixture():
    bb = BBoxAnnotator(
        project_path=Path(project_path),
        input_item=in_p,
        output_item=out_p,
        annotation_file_path=anno_file_path
    )

    bbox_sample = [
        {'x': 10, 'y': 10, 'width': 20, 'height': 30},
        {'x': 10, 'y': 20, 'width': 20, 'height': 30},
        {'x': 10, 'y': 30, 'width': 20, 'height': 30},
    ]

    bb.view._image_box._state.bbox_coords = [BboxCoordinate(**bbox) for bbox in bbox_sample]

    return bb