Skip to content

Camera

This module provides a Camera class for handling camera feed

CLASS DESCRIPTION
Camera

Camera class for handling camera feed

Documentation last updated: 2025-02-22

Camera

Camera(
    *,
    connection_details: dict | None = None,
    init_timeout: int = 1,
    buffer_size: int = 2000,
    simulation: bool = False,
    verbose: bool = False,
    **kwargs,
)

Camera class for handling camera feed

ATTRIBUTE DESCRIPTION
placeholder

Placeholder image

TYPE: ndarray

transforms

List of transformations

TYPE: list[tuple[Callable[[ndarray, Any], ndarray], Iterable | None, Mapping | None]]

callbacks

List of callbacks

TYPE: list[tuple[Callable[[ndarray, Any], ndarray], Iterable | None, Mapping | None]]

connection

Connection to the device

TYPE: Any | None

connection_details

Connection details for the device

TYPE: dict

flags

Flags for the device

TYPE: SimpleNamespace

init_timeout

Timeout for initialization

TYPE: int

buffer

Buffer for storing frames

TYPE: deque

data_queue

Queue for storing data

TYPE: Queue

show_event

Event for showing the stream

TYPE: Event

stream_event

Event for streaming

TYPE: Event

threads

Threads for streaming and processing data

TYPE: dict

verbose

Verbosity of class

TYPE: bool

feed

Video feed

TYPE: VideoCapture

is_connected

Whether the device is connected

TYPE: bool

verbose

Verbosity of class

TYPE: bool

frame_rate

Frame rate of camera feed

TYPE: int | float

frame_size

Frame size of camera feed

TYPE: tuple[int, int]

METHOD DESCRIPTION
checkDeviceConnection

Check the connection to the device

connect

Connect to the device

connectFeed

Connect to the camera feed

disconnect

Disconnect from the device

disconnectFeed

Disconnect from the camera feed

setFrameRate

Set the frame rate of camera feed

setFrameSize

Set the resolution of camera feed

decodeBytesToFrame

Decode byte array of image

encodeFrameToBytes

Encode image into byte array

loadImageFile

Load an image from file

saveFrame

Save image to file

saveFramesToVideo

Save frames to video file

transformFrame

Transform the frame

processFrame

Process the frame

getFrame

Get image from camera feed

show

Show image in window

checkDeviceBuffer

Check the connection buffer

clear

Clear the input and output buffers

read

Read data from the device

showStream

Show the stream

startStream

Start the stream

stopStream

Stop the stream

stream

Toggle the stream

Initialize the camera object

PARAMETER DESCRIPTION
connection_details

connection details for the device. Defaults to None.

TYPE: dict DEFAULT: None

init_timeout

timeout for initialization. Defaults to 1.

TYPE: int DEFAULT: 1

buffer_size

size of the buffer. Defaults to 2000.

TYPE: int DEFAULT: 2000

simulation

whether to simulate the camera feed. Defaults to False.

TYPE: bool DEFAULT: False

verbose

verbosity of the class. Defaults to False.

TYPE: bool DEFAULT: False

feed property writable

feed: VideoCapture

Video feed

frame_rate property

frame_rate: int | float

Frame rate of camera feed

frame_size property

frame_size: tuple[int, int]

Frame size of camera feed

is_connected property

is_connected: bool

Whether the device is connected

verbose property writable

verbose: bool

Verbosity of class

__enter__

__enter__()

Context manager enter method

__exit__

__exit__(exc_type, exc_value, traceback)

Context manager exit method

checkDeviceBuffer

checkDeviceBuffer() -> bool

Check the connection buffer

RETURNS DESCRIPTION
bool

whether the device buffer is available

TYPE: bool

checkDeviceConnection

checkDeviceConnection() -> bool

Check the connection to the device

RETURNS DESCRIPTION
bool

whether the device is connected

TYPE: bool

clear

clear()

Clear the input and output buffers

connect

connect()

Connect to the device

connectFeed

connectFeed()

Connect to the camera feed

decodeBytesToFrame staticmethod

decodeBytesToFrame(bytearray: bytes) -> ndarray

Decode byte array of image

PARAMETER DESCRIPTION
bytearray

byte array of image

TYPE: bytes

RETURNS DESCRIPTION
ndarray

np.ndarray: image array of decoded byte array

disconnect

disconnect()

Disconnect from the device

disconnectFeed

disconnectFeed()

Disconnect from the camera feed

encodeFrameToBytes staticmethod

encodeFrameToBytes(
    frame: ndarray, extension: str = ".png"
) -> bytes

Encode image into byte array

PARAMETER DESCRIPTION
frame

image array to be encoded

TYPE: ndarray

extension

image format to encode to. Defaults to '.png'.

TYPE: str DEFAULT: '.png'

RETURNS DESCRIPTION
bytes

byte array of image

TYPE: bytes

getFrame

getFrame(latest: bool = False) -> tuple[bool, ndarray]

Get image from camera feed

PARAMETER DESCRIPTION
latest

whether to get the latest image. Default to False.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
tuple[bool, ndarray]

tuple[bool, np.ndarray]: (whether an image is obtained, image array)

loadImageFile staticmethod

loadImageFile(filename: str) -> ndarray

Load an image from file

PARAMETER DESCRIPTION
filename

image filename

TYPE: str

RETURNS DESCRIPTION
ndarray

np.ndarray: image array from file

processFrame staticmethod

processFrame(
    frame: ndarray,
    callbacks: Iterable[
        tuple[
            Callable[[ndarray, Any], ndarray],
            Iterable | None,
            Mapping | None,
        ]
    ]
    | None = None,
) -> ndarray

Process the frame

PARAMETER DESCRIPTION
frame

image array to be processed

TYPE: ndarray

callbacks

list of callbacks. Defaults to None.

TYPE: list[tuple[Callable[[np.ndarray,Any], np.ndarray], Iterable|None, Mapping|None] DEFAULT: None

RETURNS DESCRIPTION
ndarray

np.ndarray: processed image array

read

read() -> tuple[bool, ndarray]

Read data from the device

RETURNS DESCRIPTION
tuple[bool, ndarray]

tuple[bool, np.ndarray]: (whether data is received, data)

saveFrame staticmethod

saveFrame(
    frame: ndarray, filename: str | None = None
) -> bool

Save image to file

PARAMETER DESCRIPTION
frame

frame array to be saved

TYPE: ndarray

filename

filename to save to. Defaults to 'image.png'.

TYPE: str DEFAULT: None

RETURNS DESCRIPTION
bool

whether the image array is successfully saved

TYPE: bool

saveFramesToVideo staticmethod

saveFramesToVideo(
    frames: Iterable[ndarray],
    fps: int | float,
    filename: str | None = None,
) -> bool

Save frames to video file

PARAMETER DESCRIPTION
frames

list of frames to be saved

TYPE: list[ndarray]

fps

frame rate of video

TYPE: int | float

filename

filename to save to. Defaults to 'video.mp4'.

TYPE: str DEFAULT: None

RETURNS DESCRIPTION
bool

whether the video is successfully saved

TYPE: bool

setFrameRate

setFrameRate(fps: int | float = 30.0)

Set the frame rate of camera feed

PARAMETER DESCRIPTION
fps

frame rate in frames per second. Defaults to 30.0.

TYPE: int | float DEFAULT: 30.0

setFrameSize

setFrameSize(size: Iterable[int] = (10000, 10000))

Set the resolution of camera feed

PARAMETER DESCRIPTION
size

width and height of feed in pixels. Defaults to (10000,10000).

TYPE: tuple[int] DEFAULT: (10000, 10000)

show

show(
    transforms: list[Callable[[ndarray], ndarray]]
    | None = None,
)

Show image in window

PARAMETER DESCRIPTION
transforms

list of transformations. Defaults to None.

TYPE: list[Callable[[ndarray], ndarray]] DEFAULT: None

showStream

showStream(on: bool)

Show the stream

PARAMETER DESCRIPTION
on

whether to show the stream

TYPE: bool

startStream

startStream(
    buffer: deque | None = None,
    *,
    show: bool = False,
    sync_start: Barrier | None = None,
)

Start the stream

PARAMETER DESCRIPTION
buffer

buffer to store frames. Defaults to None.

TYPE: deque DEFAULT: None

show

whether to show the stream. Defaults to False.

TYPE: bool DEFAULT: False

sync_start

synchronization barrier. Defaults to

TYPE: Barrier DEFAULT: None

stopStream

stopStream()

Stop the stream

stream

stream(
    on: bool,
    buffer: deque | None = None,
    *,
    sync_start: Barrier | None = None,
    **kwargs,
)

Toggle the stream

PARAMETER DESCRIPTION
on

whether to start the stream

TYPE: bool

buffer

buffer to store frames. Defaults to None.

TYPE: deque DEFAULT: None

sync_start

synchronization barrier. Defaults to None.

TYPE: Barrier DEFAULT: None

transformFrame staticmethod

transformFrame(
    frame: ndarray,
    transforms: Iterable[
        tuple[
            Callable[[ndarray, Any], ndarray],
            Iterable | None,
            Mapping | None,
        ]
    ]
    | None = None,
) -> ndarray

Transform the frame

PARAMETER DESCRIPTION
frame

image array to be transformed

TYPE: ndarray

transforms

list of transformations. Defaults to None.

TYPE: list[tuple[Callable[[ndarray, Any], ndarray], Iterable | None, Mapping | None]] DEFAULT: None

RETURNS DESCRIPTION
ndarray

np.ndarray: transformed image array