payloadcomputerdroneprojekt.image_analysis package#

Submodules#

payloadcomputerdroneprojekt.image_analysis.data_handler module#

class payloadcomputerdroneprojekt.image_analysis.data_handler.DataHandler(path: str)[source]#

Bases: object

Handles loading, saving, and processing of DataItem objects for image analysis.

Parameters:

path (str) – Directory path where data files are stored.

get_filterd_items(distance_threshold: float) Dict[str, Dict[str, List[Dict[str, Any]]]][source]#

Filters and clusters detected objects by color and shape, then computes their mean positions.

Parameters:

distance_threshold (float) – Distance threshold for clustering.

Returns:

Filtered and clustered object data.

Return type:

dict

get_filtered_storage() str[source]#

Returns the path to the filtered data storage file.

Returns:

Path to filtered data file.

Return type:

str

get_items() List[Dict[str, Any]][source]#

Returns a list of all DataItems as dictionaries.

Returns:

List of DataItem dictionaries.

Return type:

list

reset_data() None[source]#

Resets the data handler by clearing the internal list and deleting the data file.

payloadcomputerdroneprojekt.image_analysis.data_handler.get_mean(sorted_list: Dict[str, Dict[str, Dict[int, List[Dict[str, Any]]]]]) Dict[str, Dict[str, List[Dict[str, Any]]]][source]#

Computes the mean latitude and longitude for each cluster of objects.

Parameters:

sorted_list (dict) – Nested dictionary of clustered objects.

Returns:

Nested dictionary with mean positions and associated times/IDs.

Return type:

dict

payloadcomputerdroneprojekt.image_analysis.data_handler.sort_list(object_store: Dict[str, Dict[str, List[Dict[str, Any]]]], distance_threshold: float) Dict[str, Dict[str, Dict[int, List[Dict[str, Any]]]]][source]#

Clusters objects by their latitude and longitude using hierarchical clustering.

Parameters:
  • object_store (dict) – Nested dictionary of objects grouped by color and shape.

  • distance_threshold (float) – Distance threshold for clustering.

Returns:

Nested dictionary of clustered objects.

Return type:

dict

payloadcomputerdroneprojekt.image_analysis.data_item module#

class payloadcomputerdroneprojekt.image_analysis.data_item.DataItem(path: str)[source]#

Bases: object

Represents a data item for image analysis, storing image paths, metadata, and detected objects.

Parameters:

path (str) – Directory path where images will be saved.

add_computed_image(image: ndarray) None[source]#

Save and register the computed (processed) image.

Parameters:

image (np.ndarray) – Computed image as a numpy array.

add_height(height: float) None[source]#

Add the height at which the image was taken.

Parameters:

height (float) – Height value.

add_image(image, name: str) None[source]#

Save and register an image with a specific name.

Parameters:
  • image (np.ndarray) – Image as a numpy array.

  • name (str) – Name for the saved image.

add_image_position(latlonalt: ndarray) None[source]#

Add the image’s GPS position (latitude, longitude, altitude).

Parameters:

latlonalt (np.ndarray) – Array containing latitude, longitude, and altitude.

add_objects(objects: List[Dict[str, Any]]) None[source]#

Add detected objects to the data item and assign unique IDs to each.

Parameters:

objects (List[Dict[str, Any]]) – List of detected object dictionaries.

add_quality(quality: float) None[source]#

Add a quality metric to the data item.

Parameters:

quality (float) – Quality value (e.g., confidence score).

add_raw_image(image: ndarray) None[source]#

Save and register the raw image.

Parameters:

image (np.ndarray) – Raw image as a numpy array.

get_dict() Dict[str, Any][source]#

Get the data item as a dictionary, including its ID.

Returns:

Dictionary representation of the data item.

Return type:

Dict[str, Any]

payloadcomputerdroneprojekt.image_analysis.ia_class module#

class payloadcomputerdroneprojekt.image_analysis.ia_class.ImageAnalysis(config: dict, camera: AbstractCamera, comms: Communications)[source]#

Bases: object

Handles image analysis for drone payload computer, including color and shape detection, object localization, and image quality assessment.

Parameters:
  • config (dict) – Configuration dictionary for image analysis parameters.

  • camera (AbstractCamera) – Camera object implementing AbstractCamera.

  • comms (Communications) – Communications object for drone telemetry.

add_lat_lon(obj: dict, rotation: List[float] | ndarray, height: float, image_size: Tuple[int, int], loc_to_global: Callable[[float, float], Any]) None[source]#

Add latitude and longitude to an object based on its local offset.

Parameters:
  • obj (dict) – Object dictionary.

  • rotation (list or np.array) – Rotation vector.

  • height (float) – Height value.

  • image_size (tuple) – Image size (height, width).

  • loc_to_global (callable) – Function to convert local to global coordinates.

Returns:

None

compute_image(image: ndarray, item: DataItem | None = None, height: float = 1) Tuple[List[dict], ndarray][source]#

Filter image for defined colors and detect objects.

Parameters:

image (np.array) – Input image.

Returns:

Tuple of (list of detected objects, shape-filtered image).

Return type:

tuple[list[dict], np.array]

detect_obj(objects: List[dict], filtered_image: Dict[str, Any], height: float = 1) None[source]#

Detect objects in a filtered image and append to objects list.

Parameters:
  • objects (list[dict]) – List to append detected objects.

  • filtered_image (dict) – Dictionary with color and filtered image.

  • height (float) – Minimum height for object detection.

Returns:

None

filter_color(image: ndarray, color: str, shape_mask: ndarray | None = None) ndarray[source]#

Filter the image for a specific color.

Parameters:
  • image (np.array) – Input image.

  • color (str) – Color name (must be in defined colors).

  • shape_mask (np.array or None) – Optional shape mask to apply.

Returns:

Filtered image.

Return type:

np.array

Raises:

IndexError – If color is not defined.

filter_colors(image: ndarray) Tuple[List[Dict[str, Any]], ndarray][source]#

Filter the image for each defined color and for the shape color.

Parameters:

image (np.array) – Input image.

Returns:

Tuple of (list of color-filtered dicts, shape-filtered image).

Return type:

tuple[list[dict], np.array]

filter_shape_color(image: ndarray) ndarray[source]#

Filter the image for the shape color.

Parameters:

image (np.array) – Input image.

Returns:

Shape-filtered image.

Return type:

np.array

find_code(obj: dict, shape_image: ndarray, height: float = 1) bool[source]#

Find code elements (e.g., QR code-like) inside the object.

Parameters:
  • obj (dict) – Object dictionary.

  • shape_image (np.array) – Shape-filtered image.

  • height (float) – Minimum height for code element detection.

Returns:

True if code found, False otherwise.

Return type:

bool

get_all_obj_list() List[dict][source]#

Get a list of all detected objects with color and shape.

Returns:

List of object dictionaries.

Return type:

list[dict]

get_closest_element(image: ndarray, color: str, shape: str | None, item: DataItem | None = None, height: float = 1) dict | None[source]#

Get the closest detected object of a given color and shape.

Parameters:
  • image (np.array) – Input image.

  • color (str) – Color name.

  • shape (str) – Shape name.

Returns:

Closest object dictionary or None.

Return type:

dict or None

get_color_obj_list(color: str) List[dict][source]#

Get a list of all objects for a given color.

Parameters:

color (str) – Color name.

Returns:

List of object dictionaries.

Return type:

list[dict]

async get_current_offset_closest(color: str, shape: str, yaw_zero: bool = True, indoor: bool = False) Tuple[List[float] | None, float | None, float | None][source]#

Get the offset from the drone to the closest object of a given color and shape.

Parameters:
  • color (str) – Color name to detect.

  • shape (str) – Shape name to detect.

  • yaw_zero (bool) – If True, set yaw to zero for calculation.

Returns:

Tuple (offset [x, y], height, yaw offset).

Return type:

tuple or (None, None, None) if not found

get_filtered_objs() Dict[str, Dict[str, List[dict]]][source]#

Get a dictionary of all filtered objects.

Returns:

Dictionary of filtered objects by color and shape.

Return type:

dict[str, dict[str, list]]

get_local_offset(obj: dict, rotation: List[float] | ndarray, height: float, image_size: Tuple[int, int]) ndarray[source]#

Get the local offset of an object in the drone’s coordinate system.

Parameters:
  • obj (dict) – Object dictionary.

  • rotation (list or np.array) – Rotation vector.

  • height (float) – Height value.

  • image_size (tuple) – Image size (height, width).

Returns:

Local offset [x, y, z].

Return type:

np.array

get_matching_objects(color: str, shape: str | None = None) List[dict][source]#

Get all matching filtered objects for a color and optional shape.

Parameters:
  • color (str) – Color name.

  • shape (str or None) – Shape name (optional).

Returns:

List of object dictionaries.

Return type:

list[dict]

get_shape(obj: dict, shape_image: ndarray, height: float = 1) str | bool[source]#

Detect the shape inside the object boundaries.

Parameters:
  • obj (dict) – Object dictionary.

  • shape_image (np.array) – Shape-filtered image.

  • height (float) – Minimum height for shape detection.

Returns:

Shape name (“Dreieck”, “Rechteck”, “Kreis”) or False.

Return type:

str or bool

async image_loop() None[source]#

Main logic for per-frame image analysis.

Returns:

None

static quality_of_image(image: ndarray) float[source]#

Assess the quality of an image using Laplacian variance.

Parameters:

image (np.array) – Image array.

Returns:

Laplacian variance (higher is sharper).

Return type:

float

start_cam(images_per_second: float = 1.0) bool[source]#

Start capturing and saving images asynchronously.

Parameters:

images_per_second (float) – Images per second (frames per second).

Returns:

True if camera started successfully, False otherwise.

Return type:

bool

stop_cam() bool[source]#

Stop capturing and saving images.

Returns:

True if stopped successfully, False otherwise.

Return type:

bool

async take_image() bool[source]#

Take a single image asynchronously.

Returns:

True if successful, False otherwise.

Return type:

bool

payloadcomputerdroneprojekt.image_analysis.math_helper module#

payloadcomputerdroneprojekt.image_analysis.math_helper.compute_local(pixel_x, pixel_y, rotation_angles, image_size, field_of_view)[source]#

Computes the local 3D direction vector for a given pixel in the image, considering camera rotation.

Parameters:
  • pixel_x (int or float) – Pixel x-coordinate.

  • pixel_y (int or float) – Pixel y-coordinate.

  • rotation_angles (list or np.ndarray) – Camera rotation angles (roll, pitch, yaw) in degrees.

  • image_size (tuple) – Image size as (height, width).

  • field_of_view (tuple) – Field of view as (horizontal_fov, vertical_fov) in degrees.

Returns:

Local 3D direction vector.

Return type:

np.ndarray

payloadcomputerdroneprojekt.image_analysis.math_helper.compute_pixel_vec(pixel_x, pixel_y, image_size, field_of_view)[source]#

Computes the normalized direction vector from the camera center to a pixel in the image.

Parameters:
  • pixel_x (int or float) – Pixel x-coordinate.

  • pixel_y (int or float) – Pixel y-coordinate.

  • image_size (tuple) – Image size as (height, width).

  • field_of_view (tuple) – Field of view as (horizontal_fov, vertical_fov) in degrees.

Returns:

Normalized direction vector.

Return type:

np.ndarray

payloadcomputerdroneprojekt.image_analysis.math_helper.compute_rotation_angle(top_left, bottom_left)[source]#

Computes the rotation angle (in degrees) between two points.

Parameters:
  • top_left (tuple or np.ndarray) – Top-left point (x, y).

  • bottom_left (tuple or np.ndarray) – Bottom-left point (x, y).

Returns:

Rotation angle in degrees.

Return type:

float

payloadcomputerdroneprojekt.image_analysis.math_helper.find_relative_position(points: list)[source]#

Finds the relative positions of three points such that two vectors are orthogonal and the cross product is positive.

Parameters:

points (list) – List of 3D points.

Returns:

Tuple of points (top_left, bottom_left, top_right) if found, else None.

Return type:

tuple or None

payloadcomputerdroneprojekt.image_analysis.math_helper.find_shortest_longest_sides(points: list)[source]#

Finds the shortest and longest sides of a quadrilateral defined by four points.

Parameters:

points (list) – List of four (x, y) points.

Returns:

Tuple of (longest sides), (shortest sides).

Return type:

tuple

payloadcomputerdroneprojekt.image_analysis.math_helper.local_to_global(origin_latitude, origin_longitude)[source]#

Returns a function to convert local (x, y) coordinates to global (lat, lon) coordinates.

Parameters:
  • origin_latitude (float) – Latitude of the origin.

  • origin_longitude (float) – Longitude of the origin.

Returns:

Function that converts (x, y) to (lat, lon).

Return type:

function

payloadcomputerdroneprojekt.image_analysis.math_helper.rotation_matrix(rotation_angles)[source]#

Creates a rotation matrix from Euler angles.

Parameters:

rotation_angles (list or np.ndarray) – Rotation angles (roll, pitch, yaw) in degrees.

Returns:

3x3 rotation matrix.

Return type:

np.ndarray

Module contents#