Source code for models.model_view_panorama

"""
This model_view_panorama module handles Panorama view settings and operations within the MoilApp
"""
import cv2


[docs] class ModelViewPanorama: """ Class responsible for managing Panorama view settings and operations. Attributes: _model (object): The main model instance of the application. _map_x_pano (numpy.ndarray): Map for the x-coordinate transformation in Panorama view. _map_y_pano (numpy.ndarray): Map for the y-coordinate transformation in Panorama view. _panorama_mode (str): Current Panorama mode. Methods: panorama_mode.setter: Setter method for the current Panorama mode. maps_panorama: Getter method for the Panorama maps. panorama_image(image): Transforms the image using the Panorama maps. get_cr_left_right_panorama_car(pano_car): Calculates the left and right coordinates of the panorama car. crop_panorama_car(image): Crops the input image for the panorama car feature. create_maps_panorama_car(): Creates Panorama maps for the car feature based on the configuration file. image_car_with_color_polygon(): Creates an image of the car with polygon color drawing. create_maps_panorama_tube(): Creates Panorama maps for the tube feature based on the configuration file. crop_panorama_tube(image): Crops the input image for the panorama tube feature. """ def __init__(self, model): self._model = model self._map_x_pano = None self._map_y_pano = None # self.cr_left_car_color = None # self.cr_right_car_color = None self._panorama_mode = "car" @property def panorama_mode(self): """ Get the current mode for panorama transformation. Return: str: The current mode for panorama transformation. """ try: return self._panorama_mode except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.panorama_mode(): Error: {str(e)}") @panorama_mode.setter def panorama_mode(self, mode): """ Set the mode for panorama transformation. Arg: mode (str): The mode to set for panorama transformation. """ try: self._panorama_mode = mode self._model.active_view_mode = mode except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.panorama_mode(): Error: {str(e)}") @property def maps_panorama(self): """ Get the panorama maps. Return: tuple: A tuple containing the X and Y panorama maps. """ try: return self._map_x_pano, self._map_y_pano except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.maps_panorama(): Error: {str(e)}")
[docs] def panorama_image(self, image): """ Apply panorama transformation to the given image. Arg: image: The image to be transformed. Return: The transformed image. """ try: return self._model.remap_image(image, self._map_x_pano, self._map_y_pano) except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.panorama_image(): Error: {str(e)}")
[docs] @classmethod def get_cr_left_right_panorama_car(cls, pano_car): """ Calculate the left and right coordinates of the panorama car in the image. This method identifies the left and right coordinates of the panorama car in the given image. It searches for pixels with the color [0, 255, 0] and collects their x-coordinates to determine the left and right boundaries of the car. Arg: pano_car: The input image of the panorama car. Return: cr_left_car_color, cr_right_car_color: Left and right coordinates of the panorama car. """ width1 = pano_car.shape[1] // 2 width2 = pano_car.shape[1] height1 = pano_car.shape[0] / 2 height1_min = int(height1 - 1) height2_max = int(height1_min + 1) coordinate = [] for y in range(height1_min, height2_max): for x in range(0, width1): color = pano_car[y, x] if list(color) == [0, 255, 0]: coordinate.append(x) coordinate2 = [] for y in range(height1_min, height2_max): for x in range(width1, width2): color = pano_car[y, x] if list(color) == [0, 255, 0]: coordinate2.append(x)
# cr_left_car_color = (max(list(coordinate))) * 2 # cr_right_car_color = (min(list(coordinate2))) * 2
[docs] def crop_panorama_car(self, image): """ Crop the input image for the panorama car feature. This method crops the input image for the panorama car feature using the cropping parameters defined in the configuration file loaded through the private method __load_config(). It resizes the image to twice its original width, then crops the image using the crop_left, crop_right, crop_top, and crop_bottom parameters. The resulting image is then returned. Arg: image: The input image to be cropped. Return: The cropped image. """ try: if self._model.debug_mode: self._model.activity_logger.info("ModelViewPanorama: crop_panorama_car(), " "Crop image panorama car on top, bottom, left and right") crop_left = self._model.main_config["Pano_car"]["crop_left"] crop_right = self._model.main_config["Pano_car"]["crop_right"] - crop_left crop_top = self._model.main_config["Pano_car"]["crop_top"] crop_bottom = self._model.main_config["Pano_car"]["crop_bottom"] - crop_top image = cv2.resize(image, (image.shape[1] * 2, image.shape[0])) # if self.__cr_left_car_color is not None and self.__cr_right_car_color is not None: # image = image[0:image.shape[0], self._cr_left_car_color:self.__cr_right_car_color] # image = self._model.cropping_image(image, 0, 1, crop_top, crop_bottom) # # else: image = self._model.cropping_image(image, crop_left, crop_right, crop_top, crop_bottom) return image except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.crop_panorama_car(): Error: {str(e)}")
[docs] def create_maps_panorama_car(self): """ Create maps x and y images for panorama car feature. This method creates the map x and map y images needed for the panorama car feature using the parameters from the configuration file. It sets the generated maps to the respective attributes. Return: None """ try: if self._model.debug_mode: self._model.activity_logger.info("ModelViewPanorama: create_maps_panorama_car(), " "Creating a maps x and map y image to create panorama Car") alpha_max = self._model.main_config["Pano_car"]["alpha_max"] alpha = self._model.main_config["Pano_car"]["alpha"] beta = self._model.main_config["Pano_car"]["beta"] flip = self._model.main_config["Pano_car"]["flip"] moildev = self._model.set_moildev.moildev_main if moildev is not None: self._map_x_pano, self._map_y_pano = moildev.maps_panorama_car(alpha_max, alpha, beta, flip) except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.create_maps_panorama_car(): Error: {str(e)}")
[docs] def image_car_with_color_polygon(self): """ Create image of car with color polygon drawing. This method creates an image of the car with polygon color drawing. It draws a polygon on the resized image using the FOV maps and then remaps the image to the panorama view. Return: None """ try: if self._model.debug_mode: self._model.activity_logger.info("ModelViewPanorama: image_car_with_color_polygon(), " "Crete image car with polygon color drawing") image = self._model.draw_polygon(self._model.image_resize.copy(), self._model.maps_x_fov, self._model.maps_y_fov, is_fov=True) image_result = self._model.remap_image(image, self._model.map_x_pano, self._model.map_y_pano) self._model.get_cr_left_right_panorama_car(image_result) except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.image_car_with_color_polygon(): Error: {str(e)}")
[docs] def create_maps_panorama_tube(self): """ Create the map images for the panorama tube feature. This method creates the map images for the panorama tube feature using the MOIL SDK. If the configuration file exists, it retrieves the minimum and maximum alpha values for the tube from the configuration file using the private method __load_config(). If the MOIL device object is not None, this method creates the map images for the panorama tube feature using the alpha values and sets the image result. Return: None. """ try: if self._model.debug_mode: self._model.activity_logger.info("ModelViewPanorama: create_maps_panorama_tube(), " "Creating a maps x and map y image to create panorama Tube") alpha_min = self._model.main_config["Pano_tube"]["alpha_min"] alpha_max = self._model.main_config["Pano_tube"]["alpha_max"] moildev = self._model.set_moildev.moildev_main if moildev is not None: self._map_x_pano, self._map_y_pano = moildev.maps_panorama_tube(alpha_min, alpha_max) except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.create_maps_panorama_tube(): Error: {str(e)}")
[docs] def crop_panorama_tube(self, image): """ Crop the top and bottom of the input image based on configuration values. This method crops the top and bottom of the input image based on configuration values for the panorama tube feature. The crop values are retrieved from the configuration file using the private method load_config(). The cropped image is then returned. Arg: image: The input image to crop. Return: The cropped image. """ try: if self._model.debug_mode: self._model.activity_logger.info("ModelViewPanorama: crop_panorama_tube(), " "Crop image panorama car on top and bottom side") crop_top = self._model.main_config["Pano_tube"]["crop_top"] crop_bottom = self._model.main_config["Pano_tube"]["crop_bottom"] - crop_top image = self._model.cropping_image(image, 0, 1, crop_top, crop_bottom) return image except Exception as e: self._model.activity_logger.error(f"ModelViewPanorama.crop_panorama_tube(): Error: {str(e)}")