forensicfit.core.analyzer module#

analyzer.py

This module contains the Analyzer class, which is responsible for handling and analyzing images in the context of the ForensicFit application. It uses computer vision techniques for image analysis and provides utilities for plotting the results and converting images to and from byte buffers.

The module includes the following classes: - Analyzer: An abstract base class that defines the necessary interface for image analysis in the ForensicFit application.

Author: Pedram Tavadze Email: petavazohi@gmail.com

class forensicfit.core.analyzer.Analyzer(**kwargs)[source]#

Bases: object

Abstract base class that represents an analyzer in the system.

The Analyzer class is designed to be subclassed by concrete analyzer classes. Each subclass should implement the methods that make sense for that specific type of analyzer.

This class uses the Abstract Base Classes (ABC) module which enables the creation of a blueprint for other classes. This means you can’t create an instance of this class, it is intended to be subclassed. All methods marked with @abstractmethod must be implemented in any concrete (i.e., non-abstract) subclass.

image#

The image to be analyzed. This attribute is expected to be a numpy array representing the image, but it’s initially set to None.

Type:

np.ndarray

values#

A dictionary that contains the results of the analysis. The keys are strings describing what each value represents.

Type:

dict

metadata#

An instance of the Metadata class, containing metadata related to the analysis.

Type:

Metadata

Notes

This class is part of a module called “analyzer.py”. It serves as the parent class for all future analyzers in the system.

Attributes:
shape

A property that provides the shape of the image contained in the Analyzer instance.

Methods

apply_filter(mode, **kwargs)

exposure_control([mode])

from_buffer(buffer, metadata[, ext])

Receives an io byte buffer with the corresponding metadata and creates an instance of the class.

from_dict()

Abstract method for setting the state of an object from a dictionary.

load_dict()

Abstract method for loading a dictionary.

plot(which[, cmap, zoom, savefig, ax, show, ...])

Plots different kinds of data based on the given parameters.

plot_boundary([savefig, color, ax, show])

Plots the detected boundary of the image.

resize([size, dpi])

Resize the image associated with this analyzer.

to_buffer([ext])

Converts the current instance of the Analyzer class to a byte buffer, which can be useful for serialization or for writing to a file.

__init__(**kwargs)[source]#
exposure_control(mode='equalize_hist', **kwargs)[source]#
apply_filter(mode, **kwargs)[source]#
resize(size=None, dpi=None)[source]#

Resize the image associated with this analyzer.

The method allows to resize the image either by providing the desired output size, or by providing the dots per inch (dpi). If both parameters are None, the image will not be modified.

Parameters:
  • size (tuple, optional) – Desired output size in pixels as (height, width). If provided, this value will be used to resize the image. Default is None.

  • dpi (tuple, optional) – Desired dots per inch (dpi) as (horizontal, vertical). If provided and ‘dpi’ key exists in the metadata, this value will be used to compute the new size and then resize the image. Default is None.

Return type:

None

Raises:

ValueError – If both ‘size’ and ‘dpi’ are None.

plot_boundary(savefig=None, color='red', ax=None, show=False)[source]#

Plots the detected boundary of the image.

Parameters:
  • savefig (Union[str, Path], optional) – Path to save the plot. If provided, the plot will be saved at the specified location. If None, the plot will not be saved. Default is None.

  • color (str, optional) – Color of the boundary line in the plot. Default is ‘red’.

  • ax (matplotlib.axes.Axes, optional) – An instance of Axes in which to draw the plot. If None, a new Axes instance will be created. Default is None.

  • show (bool, optional) – Controls whether to show the image using matplotlib.pyplot.show() after it is drawn. Default is False.

Returns:

ax – The Axes instance in which the plot was drawn.

Return type:

matplotlib.axes.Axes

plot(which, cmap='gray', zoom=4, savefig=None, ax=None, show=False, mode=None, **kwargs)[source]#

Plots different kinds of data based on the given parameters.

Parameters:
  • which (str) – Determines the kind of plot to be created. Possible values include “coordinate_based”, “boundary”, “bin_based+coordinate_based”, “coordinate_based+bin_based”, “bin_based”, “bin_based+max_contrast”, “max_contrast+bin_based” and others.

  • cmap (str, optional) – The Colormap instance or registered colormap name. Default is ‘gray’.

  • zoom (int, optional) – The zoom factor for the plot. Default is 4.

  • savefig (str, optional) – Path and name to save the image. If None, the plot will not be saved. Default is None.

  • ax (matplotlib.axes.Axes or List[matplotlib.axes.Axes], optional) – An instance of Axes or list of Axes in which to draw the plot. If None, a new Axes instance will be created. Default is None.

  • show (bool, optional) – If True, displays the image. Default is False.

  • mode (str, optional) – Determines the mode of operation, which affects how the plot is generated. The effect depends on the value of which.

  • **kwargs – Arbitrary keyword arguments.

Returns:

ax – The Axes instance(s) in which the plot was drawn.

Return type:

matplotlib.axes.Axes or List[matplotlib.axes.Axes]

abstract load_dict()[source]#

Abstract method for loading a dictionary.

This method should be implemented by any non-abstract subclass of Analyzer. The implementation should handle the loading of some kind of dictionary data specific to that subclass.

Returns:

  • Typically, this method would return the loaded dictionary, but the

  • exact return type and value will depend on the specific implementation

  • in the subclass.

abstract from_dict()[source]#

Abstract method for setting the state of an object from a dictionary.

This method should be implemented by any non-abstract subclass of Analyzer. The implementation should set the state of an object based on data provided in a dictionary.

Parameters:
  • implementation (This will vary depending on the subclass) –

  • typically (but) –

  • argument (this method would accept a single) –

  • state. (the data to use when setting the object's) –

Returns:

  • Typically, this method would not return a value, but this will depend

  • on the specific implementation in the subclass.

classmethod from_buffer(buffer, metadata, ext='.png')[source]#

Receives an io byte buffer with the corresponding metadata and creates an instance of the class. This class method is helpful in situations where you have raw image data along with associated metadata and need to create an Analyzer object.

Parameters:
  • buffer (bytes) – A buffer containing raw image data, typically in the form of bytes.

  • metadata (dict) – A dictionary containing metadata related to the image. The specific contents will depend on your application, but might include things like the image’s origin, resolution, or creation date.

  • ext (str, optional) – The file extension of the image being loaded. Used to determine the decoding method. Default is ‘.png’. If the ‘ext’ key is present in the metadata dict, it will override this parameter.

Returns:

  • An instance of the Analyzer class, initialized with the image and

  • metadata provided.

to_buffer(ext='.png')[source]#

Converts the current instance of the Analyzer class to a byte buffer, which can be useful for serialization or for writing to a file. This method supports various image formats determined by the extension provided.

Return type:

bytes

Parameters:

ext (str, optional) – The file extension for the output buffer. This will determine the format of the output image. Default is ‘.png’. This method supports any image format that is recognized by the OpenCV library.

Returns:

A byte string representing the image data in the format specified by ‘ext’. This can be directly written to a file or transmitted over a network, among other things.

Return type:

bytes

Raises:

ValueError – If the provided extension is not supported, a ValueError will be raised.

property shape: tuple#

A property that provides the shape of the image contained in the Analyzer instance.

Returns:

A tuple representing the shape of the image. For grayscale images, this will be a 2-tuple (height, width). For color images, this will be a 3-tuple (height, width, channels), where ‘channels’ is typically 3 for an RGB image or 4 for an RGBA image.

Return type:

tuple