Cameras

Bispectral

crappy.camera.bispectral.calc_string(st: str, crc: int) int[source]

Given a binary string and starting CRC, Calc a final CRC-16.

Meta Camera

class crappy.camera.camera.Cam_bool_setting(name: str, getter: Callable[[], bool] | None = None, setter: Callable[[bool], None] | None = None, default: bool = True)[source]

Camera setting that can only be True or False.

__init__(name: str, getter: Callable[[], bool] | None = None, setter: Callable[[bool], None] | None = None, default: bool = True) None[source]

Sets the attributes.

Parameters:
  • name – The name of the setting, that will be displayed in the GUI.

  • getter – The method for getting the current value of the setting.

  • setter – The method for setting the current value of the setting.

  • default – The default value to assign to the setting.

class crappy.camera.camera.Cam_choice_setting(name: str, choices: Tuple[str, ...], getter: Callable[[], str] | None = None, setter: Callable[[str], None] | None = None, default: str | None = None)[source]

Camera setting that can take any value from a predefined list of values.

__init__(name: str, choices: Tuple[str, ...], getter: Callable[[], str] | None = None, setter: Callable[[str], None] | None = None, default: str | None = None) None[source]

Sets the attributes.

Parameters:
  • name – The name of the setting, that will be displayed in the GUI.

  • choices – A tuple listing the possible values for the setting.

  • getter – The method for getting the current value of the setting.

  • setter – The method for setting the current value of the setting.

  • default – The default value to assign to the setting.

class crappy.camera.camera.Cam_scale_setting(name: str, lowest: int | float, highest: int | float, getter: Callable[[], int | float] | None = None, setter: Callable[[int | float], None] | None = None, default: int | float | None = None)[source]

Camera setting that can take any value between a lower and an upper boundary.

This class can handle settings that should only take int values as well as settings that can take float value.

__init__(name: str, lowest: int | float, highest: int | float, getter: Callable[[], int | float] | None = None, setter: Callable[[int | float], None] | None = None, default: int | float | None = None) None[source]

Sets the attributes.

Parameters:
  • name – The name of the setting, that will be displayed in the GUI.

  • lowest – The lower boundary for the setting values.

  • highest – The upper boundary for the setting values.

  • getter – The method for getting the current value of the setting.

  • setter – The method for setting the current value of the setting.

  • default – The default value to assign to the setting.

property value: int | float

Returns the current value of the setting, by calling the getter if one was provided or else by returning the stored value.

class crappy.camera.camera.Cam_setting(name: str, getter: Callable[[], Any], setter: Callable[[Any], None], default: Any)[source]

Base class for each camera setting.

It is meant to be subclassed and should not be used as is.

__init__(name: str, getter: Callable[[], Any], setter: Callable[[Any], None], default: Any) None[source]

Sets the attributes.

Parameters:
  • name – The name of the setting, that will be displayed in the GUI.

  • getter – The method for getting the current value of the setting.

  • setter – The method for setting the current value of the setting.

  • default – The default value to assign to the setting.

property value: Any

Returns the current value of the setting, by calling the getter if one was provided or else by returning the stored value.

class crappy.camera.camera.Camera[source]

Base class for every camera object.

It contains all the methods shared by these classes and sets MetaCam as their metaclass.

__getattr__(item: str) Any[source]

Method for getting the value of a setting directly by calling self.<setting name>.

It is called in case __getattribute__ doesn’t work properly, and tries to return the corresponding setting value.

__init__() None[source]

Simply sets the dict containing the settings.

__setattr__(key: str, val: Any) None[source]

Method for setting the value of a setting directly by calling self.<setting name> = <value>.

add_bool_setting(name: str, getter: Callable[[], bool] | None = None, setter: Callable[[bool], None] | None = None, default: bool = True) None[source]

Adds a boolean setting, whose value is either True or False.

Parameters:
  • name – The name of the setting, that will be displayed in the GUI and can be used to directly get the value of the setting by calling self.<name>

  • getter – The method for getting the current value of the setting. If not given, the returned value is simply the last one that was set.

  • setter – The method for setting the current value of the setting. If not given, the value to be set is simply stored.

  • default – The default value to assign to the setting.

add_choice_setting(name: str, choices: Tuple[str, ...], getter: Callable[[], str] | None = None, setter: Callable[[str], None] | None = None, default: str | None = None) None[source]

Adds a choice setting, that can take a limited number of predefined str values.

Parameters:
  • name – The name of the setting, that will be displayed in the GUI and can be used to directly get the value of the setting by calling self.<name>

  • choices – A tuple containing the possible values for the setting.

  • getter – The method for getting the current value of the setting. If not given, the returned value is simply the last one that was set.

  • setter – The method for setting the current value of the setting. If not given, the value to be set is simply stored.

  • default – The default value to assign to the setting. If not given, will be the fist item in choices.

add_scale_setting(name: str, lowest: int | float, highest: int | float, getter: Callable[[], int | float] | None = None, setter: Callable[[int | float], None] | None = None, default: int | float | None = None) None[source]

Adds a scale setting, whose value is an int or a float clamped between two boundaries.

Note

If any of lowest or highest is a float, then the setting is considered to be of type float and can take float values. Otherwise, it is considered of type int and can only take integer values.

Parameters:
  • name – The name of the setting, that will be displayed in the GUI and can be used to directly get the value of the setting by calling self.<name>

  • lowest – The lowest possible value for the setting.

  • highest – The highest possible value for the setting.

  • getter – The method for getting the current value of the setting. If not given, the returned value is simply the last one that was set.

  • setter – The method for setting the current value of the setting. If not given, the value to be set is simply stored.

  • default – The default value to assign to the setting. If not given, will be the average of lowest and highest.

close() None[source]

This method should perform any action required for properly stopping the image acquisition and/or closing the connection to the camera.

This step is usually extremely important in order for the camera resources to be released. Otherwise, it might be impossible to re-open the camera from Crappy without resetting the hardware connection with it.

get_image() Tuple[Dict[str, Any], float] | ndarray | None[source]

Acquires an image and returns it along with its metadata or timestamp.

It is also fine for this method to return None. The image should be returned as a numpy array, and the metadata as a dict or the timestamp as a float. The keys of the metadata dictionary should preferably be valid Exif tags, so that the metadata can be embedded into the image file when saving.

In order for the recording of images to run, the metadata dict must contain at least the 't(s)' and ''ImageUniqueID'' keys, whose values should be the timestamp when the frame was acquired (as returned by time.time()) and the frame number as an int.

open(**kwargs) None[source]

This method should initialize the connection to the camera, configure the camera, and start the image acquisition.

This method also takes as arguments all the kwargs that were passed to the Camera block but not used by it. Some may be used directly, e.g. for choosing which camera to open out of several possible ones, and the others should indicate values to set for available settings. It is fine not to provide any values for the settings here, as each setting has a default.

To effectively set the setting values, the method set_all() has to be called at the end of open (e.g. self.set_all(**kwargs)). This is true even if no value to set was given in the kwargs. If it is not called, the settings won’t actually be set on the camera.

If some camera settings require values from the camera for their instantiation (e.g. self.add_setting(..., highest=self.cam.max_width(), ...)), they should be instantiated here. And of course before calling set_all().

set_all(**kwargs) None[source]

Checks if the kwargs are valid, sets them, and for settings that are not in kwargs sets them to their default value.

class crappy.camera.camera.MetaCam(name: str, bases: tuple, dct: dict)[source]

Metaclass ensuring that two cameras don’t have the same name, and that all cameras define the required methods. Also keeps track of all the Camera classes, including the custom user-defined ones.

Camera GStreamer

class crappy.camera.gstreamer.Camera_gstreamer[source]

A class for reading images from a video device using Gstreamer.

It can read images from the default video source, or a video device can be specified. In this case, the user has access to a range of parameters for tuning the image. Alternatively, it is possible to give a custom GStreamer pipeline to the block. In this case no settings are available, and it is up to the user to ensure the validity of the pipeline.

This class uses less resources and is compatible with more cameras than the Camera OpenCV camera, that relies on OpenCV. The installation of GStreamer of however less straightforward than the one of OpenCV.

Note

For a better performance of this class in Linux, it is recommended to have v4l-utils installed.

__init__() None[source]

Simply initializes the instance attributes.

close() None[source]

Simply stops the image acquisition.

get_image() Tuple[float, ndarray][source]

Reads the last image acquired from the camera.

Returns:

The acquired image, along with a timestamp.

open(device: str | int | None = None, user_pipeline: str | None = None, nb_channels: int | None = None, img_depth: int | None = None, **kwargs) None[source]

Opens the pipeline, sets the settings and starts the acquisition of images.

A custom pipeline can be specified using the user_pipeline argument. Simply copy-paste the pipeline as it is in the terminal, and the class will take care of adjusting it to make it compatible with the Python binding of GStreamer.

Note

For custom pipelines, it may be necessary to set a format directly in the pipeline (which isn’t the case in the terminal). For instance, this line

gst-launch-1.0 autovideosrc ! videoconvert ! autovideosink

May need to become

gst-launch-1.0 autovideosrc ! videoconvert ! video/x-raw,format=BGR ! autovideosink

Note

Pipelines built using a pipe for redirecting the stream from another application are also accepted. For example, the following user pipeline is valid :

gphoto2 --stdout --capture-movie | gst-launch-1.0 fdsrc fd=0 ! videoconvert ! autovideosink
Parameters:
  • device – The video device to open, if the device opened by default isn’t the right one. In Linux, should be a path like /dev/video0. In Windows and Mac, should be the index of the video device. This argument is ignored if a user_pipeline is given.

  • user_pipeline (str, optional) – A custom pipeline that can optionally be given. If given, the device argument is ignored. The pipeline should be given as it would be in a terminal.

  • nb_channels (int, optional) – The number of channels expected in the acquired images, in case a custom pipeline is given. Otherwise, this argument is ignored. For now, Crappy only manages 1- and 3-channel images.

  • img_depth (int, optional) – The bit depth of each channel of the acquired images, in case a custom pipeline is given. Otherwise, this argument is ignored. For now, Crappy only manages 8- and 16-bits deep images.

  • **kwargs – Allows specifying values for the settings even before displaying the graphical interface.

Camera OpenCV

class crappy.camera.opencv.Camera_opencv[source]

A class for reading images from any camera able to interface with OpenCv.

The number of the video device to read images from can be specified. It is then also possible to tune the encoding format and the size.

This camera class is less performant than the Camera GStreamer one that relies on GStreamer, but the installation of OpenCv is way easier than the one of GStreamer.

Note

For a better performance of this class in Linux, it is recommended to have v4l-utils installed.

__init__() None[source]

Sets variables and adds the channels setting.

close() None[source]

Releases the videocapture object.

get_image() Tuple[float, ndarray][source]

Grabs a frame from the videocapture object and returns it along with a timestamp.

open(device_num: int = 0, **kwargs) None[source]

Opens the video stream and sets any user-specified settings.

Parameters:
  • device_num (int, optional) – The number of the device to open.

  • **kwargs – Any additional setting to set before opening the graphical interface.

Fake Camera

class crappy.camera.fakeCamera.Fake_camera[source]

This camera class generates images without requiring any actual camera or existing image file.

The generated images are just a gradient of grey levels, with a line moving as a function of time. It is possible to tune the dimension of the image, the frame rate and the speed of the line.

__init__() None[source]

Initializes the parent class and instantiates the settings.

get_image() Tuple[Dict[str, Any], ndarray][source]

Returns the updated image, depending only on the current timestamp.

Also includes a waiting loop in order to achieve the right frame rate.

open(**kwargs) None[source]

Sets the settings, generates the first image and initializes the time counter.

File Reader

class crappy.camera.file_reader.File_reader[source]

This Camera class reads existing images from a given folder, in the same order in which they were acquired.

The name of the images to read must follow the following pattern : <frame_nr> <frame_seconds>.<frame_subseconds>.<file_extension>. This pattern is the same as used for recording images with Crappy, so images recorded via Crappy are readily readable an don’t need to be re-named.

This class tries to read the images at the same framerate as they were recorded, although the control of the framerate is not so precise. It might be that the images cannot be read fast enough to match the original framerate, in which case the images are read as fast as possible and the delay keeps growing.

__init__() None[source]

Initializes the parent class and sets a few attributes.

get_image() Tuple[float, ndarray][source]

Reads the next image in the image folder, and returns it at the right time so that the achieved framerate matches the original framerate.

If the original framerate cannot be achieved, just reads the image as fast as possible.

By default, stops the test when there’s no image left to read. If specified otherwise, just does nothing until the test ends.

open(reader_folder: Path | str, reader_backend: str | None = None, stop_at_end: bool = True) None[source]

Sets the reader backend and retrieves the images to read, sorted by their timestamp.

Parameters:
  • reader_folder – The path to the folder containing the images to read.

  • reader_backend

    The backend to use for reding the images. Should be one of :

    'sitk' or 'cv2'
    

    If not given, SimpleITK is preferred over OpenCV if available.

  • stop_at_end – If True (the default), stops the Crappy test once the available images are all exhausted. Otherwise, simply does nothing while waiting for the test to end.

JAI

class crappy.camera.jai.Jai[source]

This class allows the use of 10 and 12 bits mode for the Jai Cameras. Obviously, the framerate will be slower than the 8 bits version.

class crappy.camera.jai.Jai8[source]

This class supports Jai GO-5000-PMCL gray cameras.

This one uses FullAreaGray8 module for maximum framerate.

PiCamera

class crappy.camera.pi_camera.Picamera[source]

Class for reading images from a PiCamera.

The Picamera Camera block is meant for reading images from a Picamera. It uses the picamera module for capturing images, and cv2 for converting bgr images to black and white.

Warning

Only works on Raspberry Pi, with the picamera API. On the latest OS release “Bullseye”, it has to be specifically activated in the configuration menu.

__init__() None[source]

Instantiates the available settings.

close() None[source]

Joins the thread and closes the stream and the picamera.PiCamera object.

get_image() Tuple[float, ndarray][source]

Simply returns the last image in the buffer.

The captured image is in bgr format, and converted into black and white if needed.

Returns:

The timeframe and the image.

open(**kwargs: Any) None[source]

Sets the settings to their default values and starts the thread.

Seek Thermal Pro

class crappy.camera.seek_thermal_pro.Seek_thermal_pro[source]

Class for reading the Seek Thermal Pro infrared camera.

The Seek_thermal_pro Camera block is meant for reading images from a Seek Thermal Pro infrared camera. It communicates over USB, and gets images by converting the received bytearrays into numpy arrays.

Important

Only for Linux users: In order to drive the Seek Thermal Pro, the appropriate udev rule should be set. This can be done using the udev_rule_setter utility in crappy’s util folder. It is also possible to add it manually by running:

$ echo "SUBSYSTEM==\"usb\", ATTR{idVendor}==\"289d\", MODE=\"0777\"" | sudo tee seek_thermal.rules > /dev/null 2>&1

in a shell opened in /etc/udev/rules.d.

__init__() None[source]

Selects the right USB device.

close() None[source]

Resets the camera and releases the USB resources.

get_image() Tuple[float, ndarray][source]

Reads a single image from the camera.

Returns:

The dict containing the metadata, and the captured image

open() None[source]

Sets the USB communication and device.

Webcam

class crappy.camera.webcam.Webcam[source]

A basic class for reading images from a USB camera (including webcams).

It relies on the OpenCv library. Note that it was purposely kept extremely simple as it is mainly used as a demo. See Camera OpenCV and Camera GStreamer for classes giving a finer control over the device.

__init__() None[source]

Sets variables and adds the channels setting.

close() None[source]

Releases the videocapture object.

get_image() Tuple[float, ndarray][source]

Grabs a frame from the videocapture object and returns it along with a timestamp.

open(device_num: int | None = 0, **kwargs) None[source]

Opens the video stream and sets any user-specified settings.

Parameters:
  • device_num (int, optional) – The number of the device to open.

  • **kwargs – Any additional setting to set before opening the graphical interface.

Xi API

class crappy.camera.xiapi.Xiapi[source]

Camera class for ximeas using official XiAPI.

close() None[source]

This method closes properly the camera.

Returns:

void return function.

get_image() Tuple[float, ndarray][source]

This method get a frame on the selected camera and return a ndarray.

Returns:

frame from ximea device (ndarray height*width).

open(sn: str | None = None, **kwargs) None[source]

Will actually open the camera, args will be set to default unless specified otherwise in kwargs.

If sn is given, it will open the camera with the corresponding serial number.

Else, it will open any camera.