Coding Reference

Sample custom block script provided in AugeLab:

Example_Block code
## Bootstrap phase
from studio.custom_block import *
# Importing of community modules 
import cv2
import numpy as np

## Class Definition 
class Example_Block(Block):
	op_code = 'Example_Block' # DO NOT CHANGE !!!, must be same as class name

	def init(self):
		self.width = 200
		self.height = 120
		self.tooltip = 'Increment image values by one.'

		self.input_sockets = [SocketTypes.ImageAny('in1')]
		self.output_sockets = [SocketTypes.ImageAny('out1')]

		self.param['text1'] = TextInput(text= '5')

	def run(self):
		in1 = self.input['in1'].data
		self.output['out1'].data = in1 + 1


add_block(Example_Block.op_code, Example_Block)

Bootstrap Phase

Mandatory Imports

Sample script always starts with imports of certain modules. from studio.custom_block import *imports necessary super classes, widgets and methods for you to utilize in your block. This line is mandatory for all class definitions.

Importing Community Modules

Here, most used two libraries are already set to be imported for you. You can use OpenCV and numpy libraries in your custom blocks.

You can also import custom libraries by installing them with Import Package Window and set-read custom static variables.

All blocks provided in AugeLab Studio is cross platform compatible. However, using community modules may compromise that.

Class Definition

Name of your class is determined by whatever title you've provided in Block Name text edit. You can introduce attributes or methods however you like in this section.

Class Attributes-Methods

`Block.op_code: str`

op_code defines a unique identifier string for your custom block. To upload your custom block, you must choose a unique block name for your custom block.

`Block.tooltip: str`

tooltip allows you to show tooltips on your custom blocks when users hover over your custom block.

`Block.input_sockets: list[SocketType]`

input_sockets attribute stores socket types. Each socket type is automatically generated for you in Input Socket or Output Socket section.

SocketType takes two arguments:

...
SomeSocketClass(SocketTypes.BaseSocketClass):
    def __init__(self, name: str = '', multiple: bool = False):
        ''' 
            name: Text shown beside socket graphics
            multiple: Draw a horizontal line to indicate a list of type.
        '''
        ...

List of SocketTypes can be seen below:

  • [ImageAny]: ImageRGB | ImageGray

  • [ImageRGB]: npt.NDArray[np.uint8] # [N, N, 3]

  • [ImageGray]: npt.NDArray[np.uint8] # [N, N]

  • [Integer]: int

  • [Float]: int

  • [Number]: float | int

  • [Boolean]: bool

  • [String]: str

  • [Generic]: object

  • [Shape]: list[list[int, int], ...]

  • [Pixel]: tuple[int] | tuple[int, int, int]

  • [Point]: tuple[int, int]

`Block.output_sockets: list[SocketType]`

Same rules on Input_sockets applies to this section as well.

`Block.param: dict[str, Component]`

This property holds your block components, and interactable widgets by their unique names. There are several components you can utilize:

`Block.input: dict[str, object]`

input property is a dynamic dict that transfer data into your custom block. You can retrieve data from your input sockets by their unique socket names.

...
class Example_Block(Block):
    ...
    def init(self):
        ...
        self.input_sockets = [SocketTypes.ImageAny('Image'),
                              SocketTypes.Number('Constant')]
        ...
        
    def run(self):
        image = self.input['Image'].data
        constant = self.input['Image']
        ...

`Block.output: dict[str, object]`

output property is a dynamic dict that transfer data out of your custom block. You can place data into your output sockets by their unique socket names.

...
class Example_Block(Block):
    ...
    def init(self):
        ...
        self.output_sockets = [SocketTypes.ImageAny('Result'),
                              SocketTypes.Number('Detections')]
        ...
        
    def run(self):
        ...
        self.output['Result'].data = img_detections_drawn
        self.output['Detections'].data = n_detections
        

`Block.register_resource(name: str = '', path: str = '') -> str`

Registers paths and automatically saves them in the scenario file.

Normally, using raw paths may create problems during transferring scenario files across different computers. However, using automatic resource management allows you to use generic or relative paths. Simply storing a resource in a path lower than the scenario file will allow your script to receive the correct path no matter the where the root path is.

Arguments:

name: str : Unique name provided to distinguish a path.

path: str: Path to be registered.

Returns:

path: str: Registered path.

To receive the path provided, use get_resource method.

`Block.get_resource(name: str = '') -> str`

Allows you the receive the resource you have provided into resource manager of a custom block.

Arguments:

name: str : Unique name provided to distinguish a path.

Returns:

path: str: Registered path.

To register a path, use register_resource method.

`Block.init(self)`

This part is executed every time when a custom block is dragged-dropped, copy-pasted, or loaded in a new scenario. It's mostly advised to use this section to:

  • Load resources for your custom block

  • Define components to be used (text inputs, buttons etc.)

  • Inner block states that change with runtime.

`Block.run(self)`

Run method is executed at every step of an execution of a scenario in AugeLab Studio. You can utilize your custom blocks inner states, custom modules, constants etc.

You can receive inputs by utilizing dynamic attributes provided by super Block class and send outputs as well, change configurations of your components and read component state to choose the right execution logic.

`QAFileDialog`

QAFileDialog is a static class that offers file path choosing dialog for users:

`QAFileDialog.getOpenFileName(**kwargs)`

Arguments

caption: str = '': Title of file dialog header

directory: str = '': Starting directory of the dialog

filter: str = '': Filenames to be filtered, for example -> 'Image Files (*.png *.jpg *.bmp)'

Returns

path:str: Chosen path by user.

`QAFileDialog.getExistingDirectory`

Arguments

caption: str = '': Title of file dialog header

directory: str = '': Starting directory of the dialog

Returns

path:str: Chosen path by user.

`add_block(My_Block.op_code, My_Block)`

This part is automatically generated by the Designer Window and should not be modified. If to be modified, make sure the block name is the same as class name and op_code.

Last updated