Skip to content

Vision Reference

kevinbotlib.vision

kevinbotlib.vision.SingleFrameSendable

Bases: BinarySendable

Sendable for a single frame of video or an image

encoding instance-attribute

Frame encoding format

Supported encodings: * JPG * PNG

data_id = 'kevinbotlib.vision.dtype.frame' class-attribute instance-attribute

Internally used to differentiate sendable types

struct = {'dashboard': [{'element': 'value', 'format': 'limit:1024'}, {'element': 'resolution', 'format': 'raw'}, {'element': 'quality', 'format': 'raw'}, {'element': 'encoding', 'format': 'raw'}]} class-attribute

Data structure suggestion for use in dashboard applications

get_dict()

Return the sendable in dictionary form

Returns:

Name Type Description
dict dict

The sendable data

kevinbotlib.vision.MjpegStreamSendable

Bases: SingleFrameSendable

Sendable for a single frame of an MJPG stream

Contains all required information for decoding a video stream

data_id = 'kevinbotlib.vision.dtype.mjpeg' class-attribute instance-attribute

Internally used to differentiate sendable types

quality instance-attribute

The current JPEG compression level out of 100 - lower means more compression

resolution instance-attribute

A two integer list containing the video resolution (WIDTH x HEIGHT)

encoding = 'JPG' class-attribute instance-attribute

Frame encoding format

struct = {'dashboard': [{'element': 'value', 'format': 'limit:1024'}, {'element': 'resolution', 'format': 'raw'}, {'element': 'quality', 'format': 'raw'}, {'element': 'encoding', 'format': 'raw'}]} class-attribute

Data structure suggestion for use in dashboard applications

get_dict()

Return the sendable in dictionary form

Returns:

Name Type Description
dict dict

The sendable data

kevinbotlib.vision.FrameEncoders

Encoders from OpenCV Mats into raw bytes or network sendables

encode_sendable_jpg(frame, quality=80) staticmethod

Encode an OpenCV Mat to a SingleFrameSendable using JPEG encoding

Parameters:

Name Type Description Default
frame MatLike

The Mat to encode

required
quality int

The JPEG quality level. Defaults to 80.

80

Returns:

Name Type Description
SingleFrameSendable SingleFrameSendable

A sendable to be sent over the network

encode_sendable_png(frame, compression=3) staticmethod

Encode an OpenCV Mat to a SingleFrameSendable using PNG encoding

Parameters:

Name Type Description Default
frame MatLike

The Mat to encode

required
compression int

The PNG compression level. Defaults to 3.

3

Returns:

Name Type Description
SingleFrameSendable SingleFrameSendable

A sendable to be sent over the network

encode_jpg(frame, quality=80) staticmethod

Encode an OpenCV Mat to raw bytes using JPEG encoding

Parameters:

Name Type Description Default
frame MatLike

The Mat to encode

required
quality int

The JPEG quality level. Defaults to 80.

80

Returns:

Name Type Description
bytes bytes

Raw data

encode_png(frame, compression=3) staticmethod

Encode an OpenCV Mat to raw bytes using PNG encoding

Parameters:

Name Type Description Default
frame MatLike

The Mat to encode

required
compression int

The PNG compression level. Defaults to 3.

3

Returns:

Name Type Description
bytes bytes

Raw data

kevinbotlib.vision.FrameDecoders

Decoders from Base64 or network sendables to OpenCV Mats

decode_sendable(sendable) staticmethod

Decode a SingleFrameSendable into an OpenCV Mat

Parameters:

Name Type Description Default
sendable SingleFrameSendable

The sendable to decode

required

Raises:

Type Description
ValueError

If the encoding type isn't recognized

Returns:

Name Type Description
MatLike MatLike

An OpenCV Mat

decode_base64(data, encoding) staticmethod

Decode a base64 string into an OpenCV Mat

Parameters:

Name Type Description Default
data str

The base64 data to decode

required
encoding str

The encoding format. Can be JPG or "PNG"

required

Raises:

Type Description
ValueError

If the encoding type isn't recognized

Returns:

Name Type Description
MatLike MatLike

An OpenCV Mat

kevinbotlib.vision.VisionCommUtils

Various utilities to integrate vision data with networking

init_comms_types(client) staticmethod

Allows the use of frame data over the communication client

Parameters:

Name Type Description Default
client RedisCommClient

The communication client to integrate with

required

kevinbotlib.vision.BaseCamera

Bases: ABC

Abstract class for creating Vision Cameras

get_frame() abstractmethod

Get the current frame from the camera

Returns:

Type Description
tuple[bool, MatLike]

tuple[bool, MatLike]: Frame retrieval success and an OpenCV Mat

set_resolution(width, height) abstractmethod

Attempt to set the current camera resolution

Parameters:

Name Type Description Default
width int

Frame width in px

required
height int

Frame height in px

required

kevinbotlib.vision.CameraByIndex

Bases: BaseCamera

Create an OpenCV camera from a device index

Not recommended if you have more than one camera on a system

__init__(index)

Initialize the camera

Parameters:

Name Type Description Default
index int

Index of the camera

required

kevinbotlib.vision.CameraByDevicePath

Bases: BaseCamera

Create an OpenCV camera from a device path

__init__(path)

Initialize the camera

Parameters:

Name Type Description Default
path str

Device path of the camera ex: /dev/video0

required

kevinbotlib.vision.VisionPipeline

Bases: ABC

An abstract vision processing pipeline

__init__(source)

Pipeline initialization

Parameters:

Name Type Description Default
source Callable[[], tuple[bool, MatLike]]

Getter for the frame to process

required

run(*args, **kwargs) abstractmethod

Runs the vision pipeline

Returns:

Type Description
tuple[bool, MatLike | None]

tuple[bool, MatLike | None]: An OpenCV Mat for pipeline visualization purposes. Can be ignored depending on the use case.

return_values()

Retrieves the calculations from the latest pipeline iteration

Returns:

Name Type Description
Any Any

Pipeline calculations

kevinbotlib.vision.EmptyPipeline

Bases: VisionPipeline

A fake vision pipeline returning the original frame

run()

Fake pipeline. Return the inputs.

Returns: Source values