Skip to content

Whale viewer

This module provides a streamlit rendering for the whales and dolphins that the classifier is aware of, and also holds the metadata for them (images, class names that the classifier uses, and URLS for further information about each species).

display_whale(whale_classes, i, viewcontainer=None)

Display whale image and reference to the provided viewcontainer.

Parameters:

Name Type Description Default
whale_classes List[str]

A list of whale class names.

required
i int

The index of the whale class to display.

required
viewcontainer

The container to display the whale information. If not provided, use the current streamlit context (works via 'with container' syntax)

None

Returns:

Type Description

None

TODO: how to find the object type of viewcontainer.? they are just "deltagenerators" but we want the result of the generator.. In any case, it works ok with either call signature.

Source code in src/whale_viewer.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def display_whale(whale_classes:List[str], i:int, viewcontainer=None):
    """
    Display whale image and reference to the provided viewcontainer.

    Args:
        whale_classes (List[str]): A list of whale class names.
        i (int): The index of the whale class to display.
        viewcontainer: The container to display the whale information. If 
            not provided, use the current streamlit context (works via 
            'with `container`' syntax)

    Returns:
        None

    TODO: how to find the object type of viewcontainer.? they are just "deltagenerators" but 
    we want the result of the generator.. In any case, it works ok with either call signature.
    """
    import streamlit as st
    if viewcontainer is None:
        viewcontainer = st

    # validate the input i should be within the range of the whale_classes
    if i >= len(whale_classes):
        raise ValueError(f"Index {i} is out of range. The whale_classes list has only {len(whale_classes)} elements.")

    # validate the existence of the whale class in the dataframe as a row key
    if whale_classes[i] not in df_whale_img_ref.index:
        raise ValueError(f"Whale class {whale_classes[i]} not found in the dataframe.")


    viewcontainer.markdown(
        "### :whale:  #" + str(i + 1) + ": " + format_whale_name(whale_classes[i])
    )
    current_dir = os.getcwd()
    image_path = os.path.join(current_dir, "src/images/references/")
    image = Image.open(image_path + df_whale_img_ref.loc[whale_classes[i], "WHALE_IMAGES"])

    viewcontainer.image(image, caption=df_whale_img_ref.loc[whale_classes[i], "WHALE_REFERENCES"])

format_whale_name(whale_class)

Formats a whale class name for display

Parameters:

Name Type Description Default
whale_class str

The class name of the whale, with words separated by underscores.

required

Returns:

Name Type Description
str str

The formatted whale name with spaces instead of underscores and each word capitalized.

Source code in src/whale_viewer.py
106
107
108
109
110
111
112
113
114
115
116
117
def format_whale_name(whale_class:str) -> str:
    """
    Formats a whale class name for display 

    Args:
        whale_class (str): The class name of the whale, with words separated by underscores.

    Returns:
        str: The formatted whale name with spaces instead of underscores and each word capitalized.
    """
    whale_name = whale_class.replace("_", " ").title()
    return whale_name