Context Managers

Context managers can be used in order to avoid writing verbose boilerplate code like the following:

try:
    api.initialize()
    # User code.
except NxLibException as e:
    print(f"An NxLibException occured: Error Text: {e.get_error_text()}")
except Exception as e:
    print("Something bad happenend, that has been out of our control.\n")
    raise e

This can simplified with a context manager, who performs the initialization and takes care of the exception handling as follows:

with NxLib() as api:
    # User code.

See Examples for more on how the following classes can be used to simplify and beautify your code.

NxLib

class nxlib.context.NxLib

This class offers a simple to use interface for interacting with a normal NxLib. It implements the context manager protocol and thus can be used in a with statement, which automatically initializes the NxLib and takes care of the exception handling.

Parameters

path (str, optional) – The path to the NxLib shared library. Defaults to None.

NxLibRemote

class nxlib.context.NxLibRemote(hostname, port)

This class offers a simple to use interface for interacting with a remote NxLib. It implements the context manager protocol and thus can be used in a with statement, which automatically loads the remote NxLib, connects to the given hostname (and port) when entering the scope and automatically disconnects when exiting the scope. It also takes care of the exception handling.

Parameters
  • hostname (str) – The hostname of the remote NxLib.

  • port (int) – The port number of the remote NxLib.

MonoCamera

class nxlib.context.MonoCamera(serial, open_params={})

This class implements the context manager protocol and simplifies the handling of a mono camera.

It provides the camera tree node by calling get_node() or lets you directly access an NxLibItem within the camera node by using the [] operator.

Parameters
  • serial (str) – The serial number of the target camera.

  • open_params (dict) – Optional parameters for opening the target camera.

Raises
capture()

Capture the image(s).

classmethod from_serial(serial, expected_types=None, open_params={})
get_node()

Get the camera tree node of the stereo camera the context object opens and represents.

Returns

The camera node of the stereo camera.

Return type

NxLibItem

rectify()

Rectify the captured images (requires capture() to be called first). Use this method only if you want to have the rectified raw images and no further data.

StereoCamera

class nxlib.context.StereoCamera(serial, open_params={})

This class implements the context manager protocol and simplifies the handling of an Ensenso stereo camera.

It provides the camera tree node by calling get_node() or lets you directly access an NxLibItem within the camera node by using the [] operator.

Parameters
  • serial (str) – The serial number of the target camera.

  • open_params (dict) – Optional parameters for opening the target camera.

Raises
capture()

Capture the image(s).

compute_disparity_map()

Compute the disparity map (requires capture() to be called first).

Returns

The disparity map node.

Return type

NxLibItem

compute_point_map()

Compute the point map (requires compute_disparity_map() to be called first).

Returns

The point map node.

Return type

NxLibItem

compute_texture()

Compute the texture (requires compute_point_map() to be called first).

Returns

The texture node.

Return type

NxLibItem

classmethod from_serial(serial, expected_types=None, open_params={})
get_disparity_map()

The computed disparity map (requires compute_disparity_map()).

Returns

A byte buffer containing the disparity map.

Return type

Object

get_node()

Get the camera tree node of the stereo camera the context object opens and represents.

Returns

The camera node of the stereo camera.

Return type

NxLibItem

get_point_map()

The computed point map (requires compute_point_map()).

Returns

A byte buffer containing the point map.

Return type

Object

get_texture()

The computed texture (requires compute_texture()).

Returns

A byte buffer containing the texture.

Return type

Object

rectify()

Rectify the captured images (requires capture() to be called first). Use this method only if you want to have the rectified raw images and no further data.

StructuredLightCamera

class nxlib.context.StructuredLightCamera(serial, open_params={})

This class implements the context manager protocol and simplifies the handling of an Ensenso structured light camera and has the same functionality as a stereo camera except that it does not have a disparity map.

It provides the camera tree node by calling get_node() or lets you directly access an NxLibItem within the camera node by using the [] operator.

Parameters
  • serial (str) – The serial number of the target camera.

  • open_params (dict) – Optional parameters for opening the target camera.

Raises
capture()

Capture the image(s).

compute_disparity_map()

Does nothing, because a structured light camera does not have a disparity map. Existing for compatibility reasons.

compute_point_map()

Compute the point map (requires capture() to be called first).

Returns

The point map node.

Return type

NxLibItem

compute_texture()

Compute the texture (requires compute_point_map() to be called first).

Returns

The texture node.

Return type

NxLibItem

get_node()

Get the camera tree node of the stereo camera the context object opens and represents.

Returns

The camera node of the stereo camera.

Return type

NxLibItem

get_point_map()

The computed point map (requires compute_point_map()).

Returns

A byte buffer containing the point map.

Return type

Object

get_texture()

The computed texture (requires compute_texture()).

Returns

A byte buffer containing the texture.

Return type

Object

rectify()

Rectify the captured images (requires capture() to be called first). Use this method only if you want to have the rectified raw images and no further data.

Exceptions

exception nxlib.context.CameraTypeError

Raised if camera has the wrong type (Mono/Stereo/StructuredLight).

exception nxlib.context.CameraNotFoundError

Raised if no camera is found for a given serial number.

exception nxlib.context.CameraOpenError

Raised if a camera cannot be opened.

exception nxlib.context.CameraDisparityMapError

Raised if a non-existing disparity map is requested.