Skip to content

Instance

Instance

Instance(
    project_path: Optional[StrOrPathLike] = None,
    dist_path: Optional[StrOrPathLike] = None,
    port: Optional[int] = None,
    host: str = "127.0.0.1",
    *,
    already_running: bool = False,
    disable_code: bool = False,
    tutorial_only: bool = False,
    context_in_body: bool = False,
    wait_for_init_model: bool = False,
    ping: bool = True,
    gpu: int = 0
)

A class for starting or connecting to a PEKAT VISION project.

Parameters:

Name Type Description Default
project_path Optional[StrOrPathLike]

Path to an existing project. Ignored if already_running is True.

None
dist_path Optional[StrOrPathLike]

Path to PEKAT VISION installation folder. Ignored if already_running is True.

None
port Optional[int]

Port of the project.

None
host str

IP address of the project. Set to "0.0.0.0" to listen on all interfaces.

'127.0.0.1'
already_running bool

Whether the project is already running. If the project is not already running, project will be started.

False
disable_code bool

Disable code module. Ignored if already_running is True.

False
tutorial_only bool

Only allow Anomaly module and force tutorial. Ignored if already_running is True.

False
context_in_body bool

Whether to send the context dict in the response body. Set to True if the resulting context is larger than 4KB.

False
wait_for_init_model bool

Whether to wait for all models to be initialized before returning the Instance object. Ignored if already_running is True.

False
ping bool

Whether to send a ping request to HOST:PORT/ping upon creating the instance.

True
gpu int

Which GPU to start project on. Ignored if already_running is True.

0

Raises:

Type Description
DistNotExistsError

If dist_path does not exist.

DistNotFoundError

If no PEKAT VISION was found in the default installation directory.

PortIsAllocatedError

If the specified port is already used by another process.

ValueError

If not already_running and project path is None.

RuntimeError

If the created process has no stdout.

ProjectNotFoundError

If the project was not found in the specified location.

PekatNotStartedError

If PEKAT VISION couldn't start a project.

analyze

analyze(
    image: Union[NDArray[np.uint8], bytes, StrOrPathLike],
    response_type: ResponseType = "context",
    data: Optional[str] = None,
    timeout: float = 20,
) -> Result

Send an image to the running project and get the results.

response_type will affect the image of the returned Result:

  • "context": context only, Result.image_bytes will be None.
  • "image": Result.image_bytes will contain the processed image.
  • "annotated_image": Result.image_bytes will contain the processed image with drawn detected rectangles.
  • "heatmap": Result.image_bytes will contain the layered heatmaps of the processed image.

In order to decode the image in Result.image_bytes, use get_decoded_image.

Parameters:

Name Type Description Default
image Union[NDArray[uint8], bytes, StrOrPathLike]

Path to the image, encoded image bytes or numpy image.

required
response_type ResponseType

Type of response data.

'context'
data Optional[str]

Data to be added to the query. Project will be able to access this under the "data" key in context.

None
timeout float

Timeout in seconds for the analyze request.

20

Raises:

Type Description
InvalidResponseTypeError

If response_type is not any of the allowed response types.

InvalidDataTypeError

If image is not any of the allowed data types.

Timeout

Upon timeout.

Returns:

Type Description
Result

A tuple of image and context. If response_type is "context", then the returned image is None.

send_random

send_random(
    shape: Tuple[int, ...] = (512, 512, 3),
    response_type: ResponseType = "context",
    data: Optional[str] = None,
    timeout: float = 20,
) -> Result

Send random data for analysis.

Parameters:

Name Type Description Default
shape Tuple[int, ...]

Shape of the image to be sent.

(512, 512, 3)
response_type ResponseType

Type of response data.

'context'
data Optional[str]

Data to be added to the query. Project will be able to access this under the "data" key in context.

None
timeout float

Timeout in seconds for the analyze request.

20

Raises:

Type Description
Exception

Returns:

Type Description
Result

A tuple of image and context. If response_type is "context", then the returned image is None.

stop

stop(timeout: float = 5) -> None

Stop the project if it's running and not stopping already.

It can only stop the project if already_running was set to True.

Parameters:

Name Type Description Default
timeout float

Timeout in seconds to kill process.

5

Raises:

Type Description
NoConnectionError

Upon timeout.

ping

ping(timeout: float = 5) -> requests.Response

Ping the project to check if it's running.

Parameters:

Name Type Description Default
timeout float

Timeout in seconds to ping.

5

Raises:

Type Description
NoConnectionError

Upon timeout.

Returns:

Type Description
Response

Ping response.