leaf.visualization

Module Contents

class leaf.visualization.BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'name_placeholder'
ACCEPTED_ID = None
ACCEPTED_COLOR = [None, None, None, None]
REJECTED_ID = None
REJECTED_COLOR = [None, None, None, None]
class leaf.visualization.VisualizationMapping
class Symptoms_det
class Pycnidia

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'pycnidia'
ACCEPTED_ID = 1
ACCEPTED_COLOR = [255, 246, 0]
REJECTED_ID = 254
REJECTED_COLOR = [0, 4, 255]
class Rust

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'rust'
ACCEPTED_ID = 2
ACCEPTED_COLOR = [255, 132, 0]
REJECTED_ID = 253
REJECTED_COLOR = [0, 112, 255]
class Symptoms_seg
class LeafDamage

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'necrosis'
ACCEPTED_ID = 2
ACCEPTED_COLOR = [255, 0, 88]
REJECTED_ID = 253
REJECTED_COLOR = [0, 255, 173]
class InsectDamage

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'insect_damage'
ACCEPTED_ID = 3
ACCEPTED_COLOR = [255, 0, 0]
REJECTED_ID = 252
REJECTED_COLOR = [0, 255, 250]
class PowderyMildew

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'powdery_mildew'
ACCEPTED_ID = 4
ACCEPTED_COLOR = [151, 255, 0]
REJECTED_ID = 251
REJECTED_COLOR = [113, 0, 255]
class Background

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'background'
ACCEPTED_ID = 0
ACCEPTED_COLOR = [60, 20, 200]
REJECTED_ID = 255
REJECTED_COLOR = [80, 127, 255]
class Focus
class Sharp

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'sharp'
ACCEPTED_ID = 1
ACCEPTED_COLOR = None
class Blurry

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'blurry'
ACCEPTED_ID = 0
ACCEPTED_COLOR = [105, 105, 105]
class Organs
class Head

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'wheat_head'
ACCEPTED_ID = 1
ACCEPTED_COLOR = [154, 255, 0]
REJECTED_ID = 254
REJECTED_COLOR = [60, 20, 220]
class Stem

Bases: BaseVisMap

Base class for visualization mapping. Defines placeholders for name, accepted/rejected IDs and their associated colors.

NAME = 'stem'
ACCEPTED_ID = 2
ACCEPTED_COLOR = [255, 191, 0]
REJECTED_ID = 253
REJECTED_COLOR = [96, 164, 244]
class leaf.visualization.Visualizer(vis_all: bool = True, vis_symptoms: bool = True, visualize_acceptance: bool = True, vis_organs: bool = True, vis_focus: bool = True, vis_symptoms_det: bool = True, vis_symptoms_seg: bool = True, vis_background: bool = False, src_root: str = 'export', rgb_root: str = 'images', export_root: str = 'export/visualizations', organs_subfolder: str = 'organs/pred', focus_subfolder: str = 'focus/pred', symptoms_det_subfolder: str = 'symptoms_det/pred', symptoms_seg_subfolder: str = 'symptoms_seg/pred')

General Visualization class capable of collecting and visualizing various predictions

Initializes the visualizer with paths and flags to control what types of data are visualized.

Parameters:
  • vis_all (bool) – If True, visualize all available predictions.

  • vis_symptoms (bool) – If True, symptom segmentation and symptom detection will be combined.

  • visualize_acceptance (bool) – Whether to distinguish between accepted and rejected predictions.

  • vis_organs (bool) – Visualize organ segmentation predictions.

  • vis_focus (bool) – Visualize image focus predictions.

  • vis_symptoms_det (bool) – Visualize detected symptoms.

  • vis_symptoms_seg (bool) – Visualize segmented symptoms.

  • vis_background (bool) – Visualize segmented symptom background.

  • src_root (str) – Root directory containing prediction outputs.

  • rgb_root (str) – Directory containing original RGB images.

  • export_root (str) – Directory where visualizations will be saved.

  • organs_subfolder (str) – Path to organ predictions.

  • focus_subfolder (str) – Path to focus predictions.

  • symptoms_det_subfolder (str) – Path to symptom detection predictions.

  • symptoms_seg_subfolder (str) – Path to symptom segmentation predictions.

vis_all = True
vis_symptoms = True
visualize_acceptance = True
vis_organs = True
vis_focus = True
vis_symptoms_det = True
vis_symptoms_seg = True
visualize_background = False
src_root = 'export'
rgb_root = 'images'
export_root = 'export/visualizations'
organs_subfolder = 'organs/pred'
focus_subfolder = 'focus/pred'
symptoms_det_subfolder = 'symptoms_det/pred'
symptoms_seg_subfolder = 'symptoms_seg/pred'
map_data() list[dict]

Maps prediction files and RGB images to a structured dictionary.

Returns:

A list of dictionaries where each dictionary contains paths to relevant data types for one image.

Return type:

list[dict]

Raises:

Exception – If there’s a mismatch in file counts or insufficient inputs.

find_images(search_root: str, img_extensions: list = ['*.jpg', '*.jpeg', '*.JPG', '*.png']) list[pathlib.Path]

Recursively finds images under the specified root directory.

Parameters:
  • search_root (str) – Directory to search.

  • img_extensions (list) – List of file extensions to include.

Returns:

Sorted list of found image paths.

Return type:

list[pathlib.Path]

visualize() None

Main method that triggers visualization of all enabled types.

visualize_all(data_set: dict) None

Creates a composite visualization with all predictions.

Parameters:

data_set (dict) – A dictionary of file paths to predictions and RGB image.

visualize_symptoms(data_set: dict) None

Creates a composite visualization of symptoms detections and symptoms segmentation.

Parameters:

data_set (dict) – A dictionary of file paths to predictions and RGB image.

read_image(path: str, grayscale: bool = False, bgr: bool = True) numpy.array

Loads an image from the given path.

Parameters:
  • path (str) – Path to the image file.

  • grayscale (bool) – Load in grayscale if True.

  • bgr (bool) – Return in BGR (default OpenCV) if True, otherwise convert to RGB.

Returns:

Loaded image.

Return type:

np.array

Raises:

FileNotFoundError – If the image could not be loaded.

combine_predictions(data: dict) dict

Combines predictions with logic to mark accepted/rejected predictions.

Parameters:

data (dict) – Dictionary containing segmentation and detection arrays.

Returns:

Modified dictionary with adjusted labels based on acceptance rules.

Return type:

dict

visualize_focus(predictions: numpy.array, img_bgr: numpy.array, reference_img: numpy.array = None) numpy.array

Overlays focus predictions on the RGB image.

Parameters:
  • predictions (np.array) – Focus prediction mask.

  • img_bgr (np.array) – BGR image to overlay predictions on.

  • reference_img (np.array) – Reference image for grayscale fallback.

Returns:

Modified BGR image with focus overlay.

Return type:

np.array

visualize_organs(predictions: numpy.array, img_bgr: numpy.array) numpy.array

Visualizes organ segmentation predictions.

Parameters:
  • predictions (np.array) – Organ prediction mask.

  • img_bgr (np.array) – BGR image to overlay predictions on.

Returns:

Image with organ overlay.

Return type:

np.array

visualize_symptoms_det(predictions: numpy.array, img_bgr: numpy.array) numpy.array

Visualizes detected symptoms.

Parameters:
  • predictions (np.array) – Detection mask.

  • img_bgr (np.array) – Base image to draw detections on.

Returns:

Image with symptom detections.

Return type:

np.array

visualize_symptoms_seg(predictions: numpy.array, img_bgr: numpy.array) numpy.array

Visualizes segmented symptoms.

Parameters:
  • predictions (np.array) – Segmentation mask.

  • img_bgr (np.array) – Image to draw segmentations on.

Returns:

Image with symptom segmentation.

Return type:

np.array

visualize_detections(img_bgr: numpy.array, detections: numpy.array, color_mapping: dict, radius: int = 5, add_id: bool = False) numpy.array

Draws detection circles on the image.

Parameters:
  • img_bgr (np.array) – Image to draw on.

  • detections (np.array) – Detection mask.

  • color_mapping (dict) – Mapping from class ID to color.

  • radius (int) – Radius of circles to draw.

  • add_id (bool) – Whether to annotate detections with IDs.

Returns:

Annotated image.

Return type:

np.array

visualize_segmentations(img_bgr: numpy.array, segmentations: numpy.array, color_mapping: dict, alpha: float = 0.65) numpy.array

Overlays segmentation masks with transparency.

Parameters:
  • img_bgr (np.array) – Base image.

  • segmentations (np.array) – Segmentation mask.

  • color_mapping (dict) – Mapping of class IDs to colors.

  • alpha (float) – Blending factor.

Returns:

Image with overlaid segmentations.

Return type:

np.array

save_visualization(filename: str, image: numpy.array, visualization_category: str) None

Saves a visualization image to disk.

Parameters:
  • filename (str) – Output filename (without extension).

  • image (np.array) – Image to save.

  • visualization_category (str) – Folder name under export_root for saving.

class leaf.visualization.CanopyVisualizer(vis_all: bool = True, vis_symptoms: bool = True, visualize_acceptance: bool = True, vis_organs: bool = True, vis_focus: bool = True, vis_symptoms_det: bool = True, vis_symptoms_seg: bool = True, vis_background: bool = False, src_root: str = 'export', rgb_root: str = 'images', export_root: str = 'export/visualizations', organs_subfolder: str = 'organs/pred', focus_subfolder: str = 'focus/pred', symptoms_det_subfolder: str = 'symptoms_det/pred', symptoms_seg_subfolder: str = 'symptoms_seg/pred')

Bases: Visualizer

A derived visualizer class overloading some default values to simplify the visualization of typical canopy scenario.

Initializes the visualizer with paths and flags to control what types of data are visualized. Default values are adjusted to cover the canopy visualization use case.

Parameters:
  • vis_all (bool) – If True, visualize all available predictions.

  • vis_symptoms (bool) – If True, symptom segmentation and symptom detection will be combined.

  • visualize_acceptance (bool) – Whether to distinguish between accepted and rejected predictions.

  • vis_organs (bool) – Visualize organ segmentation predictions.

  • vis_focus (bool) – Visualize image focus predictions.

  • vis_symptoms_det (bool) – Visualize detected symptoms.

  • vis_symptoms_seg (bool) – Visualize segmented symptoms.

  • src_root (str) – Root directory containing prediction outputs.

  • rgb_root (str) – Directory containing original RGB images.

  • export_root (str) – Directory where visualizations will be saved.

  • organs_subfolder (str) – Path to organ predictions.

  • focus_subfolder (str) – Path to focus predictions.

  • symptoms_det_subfolder (str) – Path to symptom detection predictions.

  • symptoms_seg_subfolder (str) – Path to symptom segmentation predictions.

class leaf.visualization.FlattenedVisualizer(vis_all: bool = False, vis_symptoms: bool = True, visualize_acceptance: bool = False, vis_organs: bool = False, vis_focus: bool = False, vis_symptoms_det: bool = True, vis_symptoms_seg: bool = True, vis_background: bool = True, src_root: str = 'export', rgb_root: str = 'images', export_root: str = 'export/visualizations', organs_subfolder: str = 'organs/pred', focus_subfolder: str = 'focus/pred', symptoms_det_subfolder: str = 'symptoms_det/pred', symptoms_seg_subfolder: str = 'symptoms_seg/pred')

Bases: Visualizer

A derived visualizer class overloading some default values to simplify the visualization of typical flattened leaves scenario.

Initializes the visualizer with paths and flags to control what types of data are visualized. Default values are adjusted to cover the canopy visualization use case.

Parameters:
  • vis_all (bool) – If True, visualize all available predictions.

  • vis_symptoms (bool) – If True, symptom segmentation and symptom detection will be combined.

  • visualize_acceptance (bool) – Whether to distinguish between accepted and rejected predictions.

  • vis_organs (bool) – Visualize organ segmentation predictions.

  • vis_focus (bool) – Visualize image focus predictions.

  • vis_symptoms_det (bool) – Visualize detected symptoms.

  • vis_symptoms_seg (bool) – Visualize segmented symptoms.

  • src_root (str) – Root directory containing prediction outputs.

  • rgb_root (str) – Directory containing original RGB images.

  • export_root (str) – Directory where visualizations will be saved.

  • organs_subfolder (str) – Path to organ predictions.

  • focus_subfolder (str) – Path to focus predictions.

  • symptoms_det_subfolder (str) – Path to symptom detection predictions.

  • symptoms_seg_subfolder (str) – Path to symptom segmentation predictions.

leaf.visualization.save_image(path: str, image: numpy.array, color_convert: int = None) None

Saves an image to disk with optional color conversion.

Parameters:
  • path (str) – Destination file path.

  • image (np.array) – Image tensor (usually from torch) to save.

  • color_convert (int) – Optional OpenCV color conversion flag.

leaf.visualization.save_depth_overlay(normalized_image: numpy.array, mask: numpy.array, output_path: str) None

Creates an overlay of a binary mask on a normalized depth image and saves it.

Parameters:
  • normalized_image (np.array) – Normalized depth image.

  • mask (np.array) – Binary mask (1 = keep, 0 = mask).

  • output_path (str) – Path to save the overlay.

leaf.visualization.save_histogram(data: numpy.array, mean_d: float, std_d: float, initial_mean: float, initial_std: float, output_path: str, highlight_line: float = None) None

Plots and saves a histogram with statistical highlights.

Parameters:
  • data (np.array) – Input data for histogram.

  • mean_d (float) – Mean of the filtered data.

  • std_d (float) – Std dev of the filtered data.

  • initial_mean (float) – Original dataset mean.

  • initial_std (float) – Original dataset std dev.

  • output_path (str) – Output path for the histogram image.

  • highlight_line (float, optional) – Vertical line to highlight a value.

leaf.visualization.test()

This function runs a dry run of the complete visualization to validate your installation. It produces visualizations in newly created test/visualization folder. It requires predictions of the models.test() to create the desired output.