scistag.mediastag.camera_cv2.CameraCv2

class CameraCv2(source)[source]

Bases: VideoSourceCamera

A camera source wrapping OpenCV’s camera access capabilities

Initializer

Parameters

source (int | str) – The camera source. When a number is passed it will be interpreted as “web cam” index, e.g. 0,

otherwise it will be handled as gstreamer pipeline definition.

Methods

add_redirect

Redirects the camera's data into a local or remote DataStag storage

continue_video

Pauses the video

get_camera_name

Returns the global name of a local camera

get_image

Returns the current image as np array

get_local_camera

Returns a unique name for a local camera at given source IF the camera is also registered and is usable

get_local_camera_exists

Returns if a camera with given source was registered

get_local_camera_name

Returns a unique name for a local camera at given source

handle_fetch

Tries to fetch data from the camera

handle_initialize_camera

Initializes the camera. Could be executed in another thread, lock data

handle_redirections

Forwards the new camera image into callback functions and/or database targets

pause

Pauses the video

redirect_to_vault

Redirects the camera's stream into the vault :type index: int :param index: The internal index

seek

Seeks to given video position :type position_s: float :param position_s: The new desired position :rtype: float :return: The real new position

set_filter

Assigns a filter which is assigned to every image received from the original video source.

set_image

Updates the current image

start

Starts the camera

stop

Stops the camera thread

update_progress

Tries to update the videos' progress

Attributes

LOCAL_CAMERA_STREAM_IDENTIFIER

Local camera, e.g.

LOCAL_CAMERA_STREAM_IDENTIFIER_TYPE

Name definition of the local camera's type

__dict__

__doc__

__module__

__weakref__

list of weak references to the object (if defined)

source

The camera's source as unique identifier

_get_image_int(timestamp=None)

Returns the most recent image and it’s time stamp

Parameters

timestamp (float | None) – If provided and the image was not modified no image will be returned.

Return type

tuple[float, Image | None]

Returns

The newest image if one is available and timestamp does not match the previous time stamp.

add_redirect(target, timeout_s=5.0)

Redirects the camera’s data into a local or remote DataStag storage

Parameters
  • target (str | Callable[[float, Image], None]) – If a string this is interpreted as a DataStag target identifier, otherwise a function to be

  • timeout_s – The timeout in seconds for how long the local camera streams shall be kept in the vault

called.

continue_video()

Pauses the video

classmethod get_camera_name(index)

Returns the global name of a local camera

Parameters

index (int) – The camera’s index

Return type

str

Returns

The identifier under which the camera can be stored in the local DataStag vault

get_image(timestamp=None, wait=False, timeout_s=2.0)

Returns the current image as np array

Parameters
  • timestamp (float | None) – The timestamp of the last image received

  • wait (bool) – If set the function will wait for the next valid image

  • timeout_s (float) – The timeout in seconds before a TimeoutError exception will be raised. 2 seconds by default. If set to -1 the function will wait forever.

Return type

tuple[float, Image | None]

Returns

Updated timestamp, the image

classmethod get_local_camera(source)

Returns a unique name for a local camera at given source IF the camera is also registered and is usable

Parameters

source (int | str) – The camera’s index or other unique identifier

Return type

str | None

Returns

The unique identifier if the camera does exist. Otherwise None

classmethod get_local_camera_exists(source)

Returns if a camera with given source was registered

Parameters

source (int | str) – The camera’s source name, class specific. Should be globally unique

Return type

bool

Returns

True if the camera was found

classmethod get_local_camera_name(source)

Returns a unique name for a local camera at given source

Parameters

source (int | str) – The camera’s index or other unique identifier

Return type

str

Returns

The unique identifier

handle_fetch()[source]

Tries to fetch data from the camera

Return type

tuple[float, Image | None]

Returns

The new image on success. None if no new image was available.

handle_initialize_camera()[source]
Initializes the camera. Could be executed in another thread, lock data

accordingly.

Overwrite this with your initialization code.

handle_redirections()

Forwards the new camera image into callback functions and/or database targets

pause()

Pauses the video

redirect_to_vault(index)

Redirects the camera’s stream into the vault :type index: int :param index: The internal index

seek(position_s)

Seeks to given video position :type position_s: float :param position_s: The new desired position :rtype: float :return: The real new position

set_filter(image_filter)

Assigns a filter which is assigned to every image received from the original video source. For example call set_filter(Grayscale()) to convert the camera or video to grayscale. You can also assign an ImageFilterPipeline to combine multiple filters. :type image_filter: ImageFilter | None :param image_filter: The filter to assign or None to disable it

Return type

None

set_image(timestamp, image)

Updates the current image

Parameters
  • timestamp (float) – The timestamp

  • image (Image) – The newest image

Return type

None

start()

Starts the camera

Return type

VideoSourceCamera

Returns

The camera object

stop()

Stops the camera thread

Return type

VideoSourceCamera

Returns

The camera object

update_progress()

Tries to update the videos’ progress

Return type

bool

Returns

True if the progress was updated

LOCAL_CAMERA_STREAM_IDENTIFIER = 'streams.cameras.camera_'

Local camera, e.g. a webcam connected to the hosting PC directly

LOCAL_CAMERA_STREAM_IDENTIFIER_TYPE = '.type'

Name definition of the local camera’s type

_lock

Access lock

_remote_thread

The remote thread

auto_progress

If defined the get_image function is also allowed to update the video’s progress. Otherwise the managing control, e.g. the VideoPlayer is responsible to do so

duration

The video’s overall duration in seconds. 0 if a stream

fps

The videos frame per second count

image_filter: ImageFilter | None

A filter which shall be applied to every image received from the video before it’s returned

is_stream

Defines if the source is a continuous stream

last_raw_image: Image | None

The last raw image received internal

last_raw_image_timestamp: float

The last raw image’s time stamp

last_returned_image: Image | None

The last returned image (after applying filters etc.)

last_update_timestamp

Timestamp of the last update

position

The current position (in seconds)

recent_image: Image | None

The most recent image

recent_timestamp: float

The most recent image’s time stamp

redirections

Targets the camera image shall be redirected to

repeat

The movie’s repeat mode

source: int | str

The camera’s source as unique identifier

speed

The movie’s playback speed. 1.0 = 100%

start_timestamp

Timestamp when the playback started or continued

started

Defines if the remote thread was started

time_per_frame

The amount of seconds per frame (1/fps)

valid

Defines if there is a valid source

video_resolution

The video’s size in pixels