anntoolkit - API Reference

Main API: anntoolkit.App

class anntoolkit.App(width=600, height=600, title='Hello')[source]

Bases: object

A base class for the application.

Note

You need to implement your own application class that derives from this class.

Warning

You will need to call set_image before you can run it

Parameters:
  • width (int) – width of the window. Default: 600.
  • height (int) – height of the window. Default: 600.
  • title (str) – title of the window. Default: “Hello”.

Example

>>> import anntoolkit
>>> import imageio
>>>
>>> class App(anntoolkit.App):
>>>     def __init__(self):
>>>         # Calling constructor of base class
>>>         super(App, self).__init__(title='Test')
>>>
>>>         # set image to view
>>>         im = imageio.imread('test_image.jpg')
>>>         self.set_image(im)
>>>
>>> # Run app
>>> app = App()
>>> app.run()
box(box, color_stroke, color_fill)[source]

Draw a box

Parameters:
  • box (list[tuple[float, float], tuple[float, float]]) – coordinates of the box. List should be size of two contain x,y coordinats of two opposite corners of the box
  • color_stroke (tuple[int, int, int, int]) – color of the stroke
  • color_fill (tuple[int, int, int, int]) – color of the fill
Returns:

tuple[float, float] - x, y coordinates in window space

height

Height of the window

loc_2_win(x, y)[source]

Convert image space to window space

Parameters:
  • x (float) – x coordinate of the text in image space
  • y (float) – y coordinate of the text in image space
Returns:

tuple[float, float] - x, y coordinates in window space

on_keyboard(key, down, mods)[source]

Is called on keybord key event from event loop that is run in run() method

Empty method, you need to overwrite it

Warning

  • Don’t call it, this is callback
Parameters:
  • key (int) – The key that generated even. Can be anntoolkit.KeyEscape, anntoolkit.KeyTab, anntoolkit.KeyBackspace, anntoolkit.KeyInsert, anntoolkit.KeyDelete, anntoolkit.KeyRight, anntoolkit.KeyLeft, anntoolkit.KeyDown, anntoolkit.KeyUp or any char from ‘A’ to ‘Z’
  • down (bool) – True if key is pressed. False if key is released.
  • mods (int) – Indicates additional modifing keys, such as Ctrl, Shift, Alt

Example

>>> def on_keyboard(self, key, down, mods):
>>>     if down:
>>>         if key == anntoolkit.KeyLeft:
>>>             ...
>>>         if key == anntoolkit.KeyRight:
>>>             ...
>>>         if key == anntoolkit.KeyUp:
>>>             ...
>>>         if key == anntoolkit.KeyDown:
>>>             ...
>>>         if key == anntoolkit.KeyDelete:
>>>             ...
>>>         if key == anntoolkit.KeyBackspace:
>>>             ...
>>>         if key == 'R':
>>>             ...
>>>         if key == 'A':
>>>             ...
>>>         if key == 'Q':
>>>             ...
on_mouse_button(down, x, y, lx, ly)[source]

Is called on left mouse button event from event loop that is run in run() method If the user presses left button on the mouse, this method is called

Empty method, you need to overwrite it

Warning

  • Don’t call it, this is callback
Parameters:
  • down (bool) – True if left mouse button is pressed. False if left mouse button is released.
  • x (float) – x coordinate of mouse cursor in window coordinate system
  • y (float) – y coordinate of mouse cursor in window coordinate system
  • lx (float) – x coordinate of mouse cursor in image coordinate system
  • ly (float) – y coordinate of mouse cursor in image coordinate system
on_mouse_position(x, y, lx, ly)[source]

Is called on mouse move event from event loop that is run in run() method If the user moves mouse, this method is called

Empty method, you need to overwrite it

Warning

  • Don’t call it, this is callback
Parameters:
  • x (float) – x coordinate of mouse cursor in window coordinate system
  • y (float) – y coordinate of mouse cursor in window coordinate system
  • lx (float) – x coordinate of mouse cursor in image coordinate system
  • ly (float) – y coordinate of mouse cursor in image coordinate system
on_update()[source]

Is called each frame from the event loop that is run in run() method

Empty method, you need to overwrite it. Do all drawing from here.

Note

  • Calls to draw API are valid only from within this method. Draw API: point(), box(), text(), etc.

Warning

  • Don’t call it, this is callback
point(x, y, color, radius=5.0)[source]

Draw point in image space

Parameters:
  • x (int) – x coordinate of the text in image space
  • y (int) – y coordinate of the text in image space
  • color (tuple[int, int, int, int]) – RGBA color of text
  • radius (float) – radius of the point. Default 5.0.
recenter()[source]

Resets zoom and recenters the image to fit in the window

run()[source]

Runs the application.

Note

This is a blocking call. It won’t return until the window is closed

Runs an event loop, where it process user input and updates window. Call callback method on_update().

Example

>>> app = App()
>>> app.run()
scale

Returns scale - change of length when transform from image space to window space

If scale is 2, that means any line segment in window space will be twice longer than corresponding in image space

Returns:float - scale
set_image(image, recenter=True)[source]

Sets the image to annotate

Note

Must be numpy ndarray of type numpy.uint8

Should have 2 dims (grayscale) or 3 dims (colored) with the last dim of size 3 for RGB case or 4 for RGBA case.

Example

>>> im = imageio.imread('test_image.jpg')
>>> app.set_image(im)
set_roi(roi, scale=0)[source]

Zooms and moves the image to view the requested roi

text(s, x, y, color=None, color_bg=None, alignment=Alignment.Left)[source]

Draw text in window space

Parameters:
  • s (str) – Text
  • x (int) – x coordinate of the text in window space
  • y (int) – y coordinate of the text in window space
  • color (tuple[int, int, int, int]) – RGBA color of text. Default None, results in white
  • color_bg (tuple[int, int, int, int]) – RGBA color of background. Default None, results in black
  • alignment (anntoolkit.Alignment) – Alignment. Can be one of: anntoolkit.Alignment.Left, anntoolkit.Alignment.Center, anntoolkit.Alignment.Right. Default anntoolkit.Alignment.Left.
text_loc(s, lx, ly, color=None, color_bg=None, alignment=Alignment.Left)[source]

Draw text in image space

Parameters:
  • s (str) – Text
  • x (int) – x coordinate of the text in image space
  • y (int) – y coordinate of the text in image space
  • color (tuple[int, int, int, int]) – RGBA color of text
  • color_bg (tuple[int, int, int, int]) – RGBA color of background
  • alignment (anntoolkit.Alignment) – Alignment. Can be one of: anntoolkit.Alignment.Left, anntoolkit.Alignment.Center, anntoolkit.Alignment.Right. Default anntoolkit.Alignment.Left.
width

Width of the window

win_2_loc(x, y)[source]

Convert window space to image space

Parameters:
  • x (float) – x coordinate of the text in window space
  • y (float) – y coordinate of the text in window space
Returns:

tuple[float, float] - x, y coordinates in image space

Enumerations

class anntoolkit.Alignment

Bases: pybind11_builtins.pybind11_object

Members:

Left

Center

Right

Center = Alignment.Center
Left = Alignment.Left
Right = Alignment.Right
name

handle) -> str

Type:(self
class anntoolkit.SpecialKeys

Bases: pybind11_builtins.pybind11_object

Members:

KeyEscape

KeyEnter

KeyTab

KeyBackspace

KeyInsert

KeyDelete

KeyRight

KeyLeft

KeyDown

KeyUp

KeyBackspace = SpecialKeys.KeyBackspace
KeyDelete = SpecialKeys.KeyDelete
KeyDown = SpecialKeys.KeyDown
KeyEnter = SpecialKeys.KeyEnter
KeyEscape = SpecialKeys.KeyEscape
KeyInsert = SpecialKeys.KeyInsert
KeyLeft = SpecialKeys.KeyLeft
KeyRight = SpecialKeys.KeyRight
KeyTab = SpecialKeys.KeyTab
KeyUp = SpecialKeys.KeyUp
name

handle) -> str

Type:(self

Not public, internal API:

class anntoolkit.Context

Bases: pybind11_builtins.pybind11_object

box(self: _anntoolkit.Context, arg0: float, arg1: float, arg2: float, arg3: float, arg4: Tuple[int, int, int, int], arg5: Tuple[int, int, int, int]) → None
get_mouse_position(self: _anntoolkit.Context) → Tuple[float, float, float, float]
get_scale(self: _anntoolkit.Context) → float
height(self: _anntoolkit.Context) → int
init(self: _anntoolkit.Context, arg0: int, arg1: int, arg2: str) → None

Initializes context and creates window

loc_2_win(self: _anntoolkit.Context, arg0: float, arg1: float) → Tuple[float, float]
new_frame(self: _anntoolkit.Context) → None

Starts a new frame. NewFrame must be called before any imgui functions

point(self: _anntoolkit.Context, x: float, y: float, color: Tuple[int, int, int, int], radius: float = 5) → None
recenter(self: _anntoolkit.Context) → None
render(self: _anntoolkit.Context) → None

Finilizes the frame and draws all UI. Render must be called after all imgui functions

set(self: _anntoolkit.Context, arg0: Image) → None
set_keyboard_callback(self: _anntoolkit.Context, arg0: function) → None
set_mouse_button_callback(*args, **kwargs)

Overloaded function.

  1. set_mouse_button_callback(self: _anntoolkit.Context, arg0: function) -> None
  2. set_mouse_button_callback(self: _anntoolkit.Context, arg0: function) -> None
set_mouse_position_callback(self: _anntoolkit.Context, arg0: function) → None
set_roi(self: _anntoolkit.Context, arg0: float, arg1: float, arg2: float, arg3: float) → None
set_without_recenter(self: _anntoolkit.Context, arg0: Image) → None
should_close(self: _anntoolkit.Context) → bool
text(*args, **kwargs)

Overloaded function.

  1. text(self: _anntoolkit.Context, arg0: str, arg1: int, arg2: int, arg3: SimpleText::Alignment) -> None
  2. text(self: _anntoolkit.Context, arg0: str, arg1: int, arg2: int, arg3: Tuple[int, int, int, int], arg4: Tuple[int, int, int, int], arg5: SimpleText::Alignment) -> None
text_loc(*args, **kwargs)

Overloaded function.

  1. text_loc(self: _anntoolkit.Context, arg0: str, arg1: float, arg2: float, arg3: SimpleText::Alignment) -> None
  2. text_loc(self: _anntoolkit.Context, arg0: str, arg1: float, arg2: float, arg3: Tuple[int, int, int, int], arg4: Tuple[int, int, int, int], arg5: SimpleText::Alignment) -> None
width(self: _anntoolkit.Context) → int
win_2_loc(self: _anntoolkit.Context, arg0: float, arg1: float) → Tuple[float, float]