moilutils Package

Moilutils Models

Project Name: MoilApps v4.1.0 Writer : Haryanto PROJECT MADE WITH: Qt Designer and PyQt6 Build for: MOIL-LAB Copyright: MOIL-2024

This project can be use a a template to create better user interface when you design a project.

There are limitations on Qt licenses if you want to use your products commercially, I recommend reading them on the official website: https://doc.qt.io/qtforpython/licenses.html

class models.moilutils.moilutils.MoilUtils[source]

Bases: MoilFisheyeMarker

static moil_camera(cam_type=None, cam_id=None, resolution: Tuple[int, int] = None)[source]

create camera object from moil camera package

Parameters:
  • cam_type

  • cam_id

  • resolution

Returns:

static select_parameter_name()[source]

Generate a Q-dialog for select camera parameter name.

Returns:

camera name

alternate text
select_media_source(from_plugin=False)[source]

Displays a dialog for selecting USB/web camera sources and detecting them. Available camera sources can be obtained by clicking the detection button on the dialog.

Returns:

Port number for usb cameras or the source link for web cameras

alternate text
main_controller(controller)[source]
form_camera_parameter(from_plugin=False)[source]

Generate a dialog for camera parameters setting.

alternate text
static show_image_to_label(label, image, width, options=None)[source]

Display an image to the label widget on the user interface.

Parameters:
  • label – destination label

  • image – image to show

  • width – width for resizing the image while keeping the original aspect ratio

  • options (dict) – optional parameters including angle, plus_icon, and scale_content

Returns:

None. Shows image on the label.

Example:

options = {'angle': 0, 'plus_icon': False, 'scale_content': False}
MoilUtils.show_image_to_label(label, image, 400, options)
classmethod connect_to_moildev(parameter_name, parameters_database=None, virtual_param_ratio=None)[source]

Return a Moildev instance for a specific type of camera.

Parameters:
  • parameter_name – name of camera (You can use ‘select_type_camera()’ to get the name.)

  • parameters_database – parameter of the camera

  • virtual_param_ratio

Returns:

Moildev instance

c_type = mutils.select_type_camera()
moildev_camera1 = mutils.connect_to_moildev(type_camera=c_type)
static remap_image(image, map_x, map_y)[source]
Take an image and a pair of X-Y maps generated by a Moildev instance as inputs,

then return a remapped image using the maps.

Parameters:
  • image – input image

  • map_x – mapping function in the x direction.

  • map_y – mapping function in the y direction.

Returns:

remapped image, typically it would be an anypoint image or a panorama image.

Return type:

image

  • Example:

image_anypoint = remap_image(image, mapX_anypoint, mapY_anypoint)
static select_file(parent=None, title='Open file', dir_path='.', file_filter='')[source]

Generate a dialog for file selection and return the path of the file selected. If no file is selected, an empty string is returned.

Parameters:
  • parent – parent windows of the dialog

  • title – dialog title

  • file_filter – filters for specific file types

  • dir_path – dialog’s working directory

Returns:

path of the file selected.

  • Example:

path_img = mutils.select_file(dir_path="./", file_filter='*.jpg')
static select_directory(parent=None, parent_dir='', title='Select Folder')[source]

Generate a dialog for directory selection and return the path of the directory selected. If no directory is selected, an empty string is returned.

Returns:

path of the directory selected

  • Example:

path_dir = mutils.select_file()
static copy_directory(src_directory, dst_directory)[source]

Recursively copy a whole directory to the destination directory.

Parameters:
  • src_directory – path of the source folder

  • dst_directory – path of the destination directory

Returns:

None

  • Example:

path_source = mutils.select_directory()
mutils.copy_directory(path_source, '/home')
static resize_image(image, width)[source]

Resize an image to one with a given width while maintaining its original aspect ratio and return it.

Parameters:
  • image – input image

  • width – desired image width

Returns:

resized image

static rotate_image(src, angle, center=None, scale=1.0)[source]

Return an image after rotation and scaling(not resizing). :param src: input image :param angle: rotation angle :param center: coordinate of the rotation center. By default, it’s the image center. :param scale: scaling factor

Returns:

rotated image

  • Example:

image = mutils.rotate_image(image, 90, center=(20,25))
static calculate_height(image, width)[source]

Return the aspect ratio keeping height for a given image width

Parameters:
  • image – input image

  • width – desired image width

Returns:

image height

  • Example:

height = calculate_height(image, 140)
static draw_polygon(image, mapX, mapY, is_fov=False)[source]

Return image with a drawn polygon indicating the remapped area given an anypoint map pair.

Parameters:
  • image – input image

  • map_x – anypoint mapX

  • map_y – anypoint mapY

Returns:

image with a polygon

  • Example:

img = mutils.read_image('sample_image.jpg')
c_type = mutils.select_type_camera()
m_instance = mutils.connect_to_moildev(type_camera=c_type)
mx, my = m_instance.maps_anypoint(0, -90, 4)
img = mutils.draw_polygon(img, mx, my)
static write_camera_type(image_file, type_camera)[source]

Write the camera type into the image’s metadata.

Parameters:
  • image_file – image file path

  • type_camera – name of camera

Returns:

None

mutils.write_camera_type('sample_image.jpg', 'Camera_name')
static read_camera_type(image_file)[source]

Read the camera type from image’s metadata.

Parameters:

image_file – image file path

Returns:

camera type

c_type = mutils.read_camera_type('sample_image.jpg')
static save_image(image, dst_directory, type_camera=None)[source]

Save an image to a directory and write the camera type into its metadata if the type is given. The file name would be the date and time when the image is saved.

Parameters:
  • image – input image

  • dst_directory – destination directory path

  • type_camera – camera type

Returns:

file name

save_image(img, '.', 'camera_1')
static draw_line(image, coordinate_point_1=None, coordinate_point_2=None)[source]

Draw a line on the image from the coordinate given. If no coordinate is given, it draws lines on image margins.

Parameters:
  • image – input image

  • coordinate_point_1 – point 1 coordinate (x, y)

  • coordinate_point_2 – point 2 coordinate (x, y)

Returns:

image with a line drawn

img = mutils.draw_line(img, (300, 300), (300, 400) )
static calculate_ratio_image2label(label, image)[source]

Calculate the width and height ratio of the image to a label.

Parameters:
  • label – UI label

  • image – input image

Returns:

width ratio and height ratio

w_ratio, h_ratio = mutils.calculate_ratio_image2label(label, img)
static cropping_image(image, left, right, top, bottom)[source]

Crop an image by ratio from every side.

Parameters:
  • image – input image

  • right – ratio of right side (1-0)

  • bottom – ratio of bottom side (1-0)

  • left – ratio of left side (0-1)

  • top – ratio of top side (0-1)

Returns:

image has already cropping

static draw_list_point_with_text(image, coordinate_point, radius=5)[source]

Draw points and their indices on the image.

Parameters:
  • image – input image

  • coordinate_point – a list of points’ coordinates

  • radius – point radius

Returns:

image with the point and their sequences drawn.

points_to_draw = [(100, 250), (200, 200), (450, 0)]
img = mutils.draw_list_point_with_text(img, points_to_draw, radius=3)
classmethod draw_rectangle(image, point_1, point_2, thickness=5)[source]

Draw rectangle on the image.

Parameters:
  • () (thickness) – input image

  • () – the first point

  • () – the second point to create rectangle

  • () – the thickness of rectangle line

Returns:

image with rectangle object

classmethod calculate_resolution_option(image, ratio_resize)[source]
classmethod convert_cv2_to_q_image(image)[source]

Convert an image from OpenCV format to Qt format. The function takes an image in OpenCV format and returns the equivalent image in Qt format. The image can be grayscale, RGB or RGBA. The conversion is done by creating a QImage object and setting the image data and format accordingly.

Parameters:

image (ndarray) – The image in OpenCV format (height x width x channels)

Returns:

The image in Qt format

Return type:

QImage

classmethod convert_q_image_to_mat(q_image)[source]

Converts a QImage to a NumPy array.

Parameters:

q_image – A QImage instance to be converted.

Returns:

A NumPy array with the image data.

Raises:

TypeError – If q_image is not a QImage instance.

Moildev Models

class models.moilutils.moildev.Moildev.Moildev(file_camera_parameter=None, camera_type=None, resolution_ratio=1, **kwarg)[source]

Bases: object

Initializes the Moildev object and sets up the camera parameters.

The camera parameters must be configured at the start of the program. These parameters are the result of the MOIL laboratory’s camera calibration.

Parameters:
  • file_camera_parameter (str, optional) – Path to the *.json file containing the camera parameters.

  • camera_type (str, optional) – The name of the camera type used (use if passing parameters via a *.json file).

  • resolution_ratio (float, optional) – The ratio for resizing the image resolution. Defaults to 1.

  • camera_name (str, optional) – The name of the camera used.

  • camera_fov (float, optional) – Camera field of view (FOV).

  • sensor_width (float, optional) – Size of the sensor width.

  • sensor_height (float, optional) – Size of the sensor height.

  • icx (float, optional) – Center image in the x-axis.

  • icy (float, optional) – Center image in the y-axis.

  • ratio (float, optional) – The value of the ratio image.

  • image_width (int, optional) – The width of the image.

  • image_height (int, optional) – The height of the image.

  • calibration_ratio (float, optional) – The value of the calibration ratio.

  • parameter_5 (parameter_0 ..) – Intrinsic fisheye camera parameters obtained from calibration.

For more details, please refer to https://github.com/perseverance-tech-tw/moildev
classmethod version()[source]

Showing the information of the version moildev library.

Returns:

Moildev version information

property camera_name

Get camera name used.

Returns:

Camera name (string)

property sensor_width

Get sensor width used.

Returns:

Camera name (string)

property sensor_height

Get sensor height used.

Returns:

Camera name (string)

property ratio

Get ratio used.

Returns:

Camera name (string)

property calibration_ratio

Get calibration ratio used.

Returns:

Camera name (string)

property camera_fov

Get Field of View (FoV) from camera used.

Returns:

FoV camera (int)

property icx

Get center image x-axis from camera used.

Returns:

Image center X (int)

property icy

Get center image y-axis from camera used.

Returns:

Image center Y(int)

property image_width

Get the width of the image used.

Returns:

image width(int)

property image_height

Get the height of the image used.

Returns:

image height(int)

property param_0

Get the value of calibration parameter_0 from camera used.

Returns:

Parameter_0 (float)

property param_1

Get the value of calibration parameter_1 from camera used.

Returns:

Parameter_1 (float)

property param_2

Get the value of calibration parameter_2 from camera used.

Returns:

Parameter_2 (float)

property param_3

Get the value of calibration parameter_3 from camera used.

Returns:

Parameter_3 (float)

property param_4

Get the value of calibration parameter_4 from camera used.

Returns:

Parameter_4 (float)

property param_5

Get the value of calibration parameter_5 from camera used.

Returns:

Parameter_5 (float)

maps_anypoint_mode1(alpha, beta, zoom)[source]

Generate a pair of X-Y Maps for the specified alpha, beta and zoom parameters, and then utilize the resulting X-Y Maps to remap the original fisheye image to the target angle image. This function has 2 mode to generate maps anypoint, mode 1 is for tube application and mode 2 usually for car application

Parameters:
  • alpha – value of zenith distance(float).

  • beta – value of azimuthal distance based on cartography system(float)

  • zoom – value of zoom(float)

Returns:

the mapping matrices X mapY: the mapping matrices Y

Return type:

mapX

please reference: https://github.com/perseverance-tech-tw/moildev
maps_anypoint_mode2(pitch, yaw, zoom)[source]

Generate a pair of X-Y Maps for the specified pitch, yaw, and roll also zoom parameters, and then utilize the resulting X-Y Maps to remap the original fisheye image to the target image.

Parameters:
  • pitch – pitch rotation (from -110 to 110 degree)

  • yaw – yaw rotation (from -110 to 110 degree)

  • zoom – zoom scale (1 - 20)

Returns:

the mapping matrices X mapY: the mapping matrices Y

Return type:

mapX

maps_anypoint_car(pitch, yaw, roll, zoom)[source]

Generate a pair of X-Y Maps for the specified pitch, yaw, and roll also zoom parameters, and then utilize the resulting X-Y Maps to remap the original fisheye image to the target image.

Parameters:
  • pitch – pitch rotation (from -110 to 110 degree)

  • yaw – yaw rotation (from -110 to 110 degree)

  • roll – roll rotation (from -110 to 110 degree)

  • zoom – zoom scale (1 - 20)

Returns:

the mapping matrices X mapY: the mapping matrices Y

Return type:

mapX

maps_panorama_tube(alpha_min, alpha_max)[source]

To generate a pair of X-Y Maps for alpha within 0 … alpha_max degree, the result X-Y Maps can be used later to generate a panorama image from the original fisheye image.

Parameters:
  • alpha_min – the minimum alpha degree given

  • alpha_max – the maximum alpha degree given. The recommended value is half of camera FOV. For example, use 90 for a 180 degree fisheye images and use 110 for a 220 degree fisheye images.

Returns:

the mapping matrices X mapY: the mapping matrices Y

Return type:

mapX

please reference: https://github.com/perseverance-tech-tw/moildev
maps_panorama_car(alpha_max, ic_alpha_degree, ic_beta_degree, flip=False)[source]

To generate a pair of X-Y Maps for alpha within 0 alpha_max degree, the result X-Y Maps can be used later to generate a panorama image from the original fisheye image. The panorama image centered at the 3D direction with alpha = ic_alpha_degree and beta = ic_beta_degree.

Parameters:
  • alpha_max – max of alpha. The recommended value is half of camera FOV. For example, use 90 for a 180 degree fisheye images and use 110 for a 220 degree fisheye images.

  • ic_alpha_degree – alpha angle of panorama center.

  • ic_beta_degree – beta angle of panorama center.

  • flip – flip image panorama.

Returns:

mapX mapY

please reference: https://github.com/perseverance-tech-tw/moildev
maps_panorama_rt(alpha_max, ic_alpha_degree, ic_beta_degree)[source]

To generate a pair of X-Y Maps for alpha within 0..alpha_max degree, the result X-Y Maps can be used later to generate a panorama image from the original fisheye image. The panorama image centered at the 3D direction with alpha = ic_alpha_degree and beta = ic_beta_degree.

Parameters:
  • alpha_max – max of alpha. The recommended value is half of camera FOV. For example, use 90 for a 180 degree fisheye images and use 110 for a 220 degree fisheye images.

  • ic_alpha_degree – alpha angle of panorama center.

  • ic_beta_degree – beta angle of panorama center.

Returns:

mapX mapY

please reference: https://github.com/perseverance-tech-tw/moildev
maps_recenter(alpha_max, beta_degree)[source]

Create maps for reverse image. this can work using input panorama rotation image

Parameters:
  • alpha_max – max of alpha. The recommended value is half of camera FOV. For example, use 90 for a 180 degree fisheye images and use 110 for a 220 degree fisheye images.

  • beta_degree – beta angle.

Returns:

maps_x_reverse, maps_y_reverse

anypoint_mode1(image, alpha, beta, zoom)[source]

Generate anypoint view image. for mode 1, the result rotation is betaOffset degree rotation around the Z-axis(roll) after alphaOffset degree rotation around the X-axis(pitch). for mode 2, The result rotation is thetaY degree rotation around the Y-axis(yaw) after thetaX degree rotation around the X-axis(pitch).

Parameters:
  • image – source image given

  • alpha – the alpha offset that corespondent to the pitch rotation

  • beta – the beta offset that corespondent to the yaw rotation

  • zoom – decimal zoom factor, normally 1..12

Returns:

anypoint image

please reference: https://github.com/perseverance-tech-tw/moildev
anypoint_mode2(image, pitch, yaw, zoom)[source]

Generate anypoint view image. for mode 1, the result rotation is betaOffset degree rotation around the Z-axis(roll) after alphaOffset degree rotation around the X-axis(pitch). for mode 2, The result rotation is thetaY degree rotation around the Y-axis(yaw) after thetaX degree rotation around the X-axis(pitch).

Parameters:
  • image – source image given

  • pitch – the alpha offset that corespondent to the pitch rotation

  • yaw – the beta offset that corespondent to the yaw rotation

  • zoom – decimal zoom factor, normally 1..12

Returns:

anypoint image

please reference: https://github.com/perseverance-tech-tw/moildev
anypoint_car(image, pitch, yaw, roll, zoom)[source]

Anypoint car mode with roll rotation

Parameters:
  • image – source image given

  • pitch – the alpha offset that corespondent to the pitch rotation

  • yaw – the beta offset that corespondent to the yaw rotation

  • roll – the beta offset that corespondent to the yaw rotation

  • zoom – decimal zoom factor, normally 1..12

Returns:

anypoint image

please reference: https://github.com/perseverance-tech-tw/moildev
panorama_tube(image, alpha_min, alpha_max)[source]

The panorama image

Parameters:
  • image – image source given

  • alpha_min

  • alpha_max

Returns:

Panorama view image

please reference: https://github.com/perseverance-tech-tw/moildev
panorama_car(image, alpha_max, alpha, beta, left, right, top, bottom, flip=False)[source]

The function that generate a moil dash panorama image from fisheye camera. the image can control by alpha to change the pitch direction and beta for yaw direction. in order to select the roi, we can control by the parameter such as left, right, top, and bottom.

Parameters:
  • image – input fisheye image

  • alpha_max

  • alpha – change the pitch direction(0 ~ 180)

  • beta – change the yaw direction(-90 ~ 90)

  • left – crop the left image by scale(0 ~ 1)

  • right – crop the right image by scale(0 ~ 1)

  • top – crop the top image by scale(0 ~ 1)

  • bottom – crop the bottom image by scale(0 ~ 1)

  • flip – Horizontal flip (Bool)

Returns:

Panorama image

recenter(image, alpha_max, ic_alpha_degree, ic_beta_degree)[source]

Change the optical point of fisheye image.

Parameters:
  • image – input image

  • alpha_max – max of alpha. The recommended value is half of camera FOV. For example, use 90 for a 180 degree fisheye images and use 110 for a 220 degree fisheye images.

  • ic_alpha_degree – alpha angle of panorama center

  • ic_beta_degree – beta angle of panorama center

Returns:

reverse image

get_alpha_from_rho(rho)[source]

Get the alpha from rho image.

Parameters:

rho – the value of rho given

Returns:

alpha

get_rho_from_alpha(alpha)[source]

Get rho image from alpha given.

Parameters:

alpha – the value of alpha given

Returns:

rho image

get_alpha_beta(coordinateX, coordinateY, mode=1)[source]

Get the alpha beta from specific coordinate image.

Parameters:
  • coordinateX

  • coordinateY

  • mode

Returns:

alpha, beta (if you get none, the coordinate is out of range that can cover)

please reference: https://github.com/perseverance-tech-tw/moildev

Moil_camera Models

class models.moilutils.moil_camera.moilcam.MoilCam(cam_type: str, cam_id: int, resolution: Tuple[int, int] = None)[source]

Bases: object

Parameters:
  • cam_type – str

  • cam_id – int or str

  • resolution – Tuple[width: int, height: int]

Returns:

AbstractMoilCamera

static supported_cam_type()[source]

List all supported camera type

Returns:

list

static scan(cam_type: str, admin_pwd: str = None) list[source]

Detect valid camera ID by cam_type

Parameters:
  • admin_pwd – str

  • cam_type – str

Returns:

list

Moil_fisheye_marker Models

This module provides functionality for adding markers to fisheye images.

class models.moilutils.moil_fisheye_marker.fisheye_marker.MoilFisheyeMarker[source]

Bases: object

A class for marking points on fisheye images.

static point(image: ndarray, pixel_coordinate: Tuple[int, int], radius: int = None, color: tuple = (0, 0, 255), fill: bool = False) ndarray[source]

Marks a point on the image at the specified pixel coordinates.

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

  • pixel_coordinate (Tuple[int, int]) – Pixel coordinates (x, y) of the point.

  • radius (int, optional) – Radius of the circle. If not provided, it will be automatically determined based on image size.

  • color (tuple, optional) – Color of the point marker in BGR format. Default is (0, 0, 255) for red.

  • fill (bool, optional) – If True, fills the circle. Default is False.

Returns:

Image with the marked point.

Return type:

np.ndarray

static crosshair(image: ndarray, pixel_coordinate: Tuple[int, int], color: Tuple[int, int, int] = (0, 0, 255)) ndarray[source]

Draws a crosshair at the specified pixel coordinates on the image.

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

  • pixel_coordinate (Tuple[int, int]) – Pixel coordinates (x, y) of the center of the crosshair.

  • color (Tuple[int, int, int], optional) – Color of the crosshair in BGR format. Default is (0, 0, 255) for red.

Returns:

Image with the drawn crosshair.

Return type:

np.ndarray

static cross(image: ndarray, pixel_coordinate: Tuple[int, int], color: Tuple[int, int, int] = (0, 0, 255)) ndarray[source]

Draws a cross at the specified pixel coordinates on the image.

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

  • pixel_coordinate (Tuple[int, int]) – Pixel coordinates (x, y) of the center of the cross.

  • color (Tuple[int, int, int], optional) – Color of the cross in BGR format. Default is (0, 0, 255) for red.

Returns:

Image with the drawn cross.

Return type:

np.ndarray

static square(image: ndarray, pixel_coordinate: Tuple[int, int], color: Tuple[int, int, int] = (0, 0, 255)) ndarray[source]

Draws a square around the specified pixel coordinates on the image.

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

  • pixel_coordinate (Tuple[int, int]) – Pixel coordinates (x, y) of the center of the square.

  • color (Tuple[int, int, int], optional) – Color of the square in BGR format. Default is (0, 0, 255) for red.

Returns:

Image with the drawn square.

Return type:

np.ndarray

static triangle(image: ndarray, pixel_coordinate: Tuple[int, int], color: Tuple[int, int, int] = (0, 0, 255)) ndarray[source]

Draws a triangle centered at the specified pixel coordinates on the image.

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

  • pixel_coordinate (Tuple[int, int]) – Pixel coordinates (x, y) of the center of the triangle.

  • color (Tuple[int, int, int], optional) – Color of the triangle in BGR format. Default is (0, 0, 255) for red.

Returns:

Image with the drawn triangle.

Return type:

np.ndarray

static boundary_fov(image: ndarray, moildev, fov: int = 90, color: tuple = (255, 255, 0)) ndarray[source]

Draws the boundary of the field of view (FOV) on the image for a given Moildev object and FOV angle.

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

  • moildev – Moildev object representing the camera model.

  • fov (int, optional) – Field of view angle in degrees. Default is 90 degrees.

  • color (tuple, optional) – Color of the boundary in BGR format. Default is (255, 255, 0) for light blue.

Returns:

Image with the drawn FOV boundary circle.

Return type:

np.ndarray

static line_horizontal_vertical(image: ndarray, pixel_coordinate: Tuple[int, int], color: Tuple[int, int, int] = (0, 0, 0), translucent: float = 0.5)[source]

Draws horizontal and vertical lines passing through the specified pixel coordinates on the image.

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

  • pixel_coordinate (Tuple[int, int]) – Pixel coordinates (x, y) through which the lines pass.

  • color (Tuple[int, int, int], optional) – Color of the lines in BGR format. Default is (0, 0, 0) for black.

  • translucent (float, optional) – Transparency level of the lines. Should be between 0 and 1. Default is 0.5, where 0 means fully transparent and 1 means fully opaque.

Returns:

Image with the drawn horizontal and vertical lines.

Return type:

np.ndarray

static line_p2p_distorted(image: ndarray, moildev, parameter_file: str, start_img_point: tuple, end_img_point: tuple, color: Tuple[int, int, int] = (0, 0, 255)) ndarray[source]

Draws a distorted line between two points on the image based on the camera model parameters.

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

  • moildev – Moildev object representing the camera model.

  • parameter_file (str) – Path to the JSON file containing camera model parameters.

  • start_img_point (tuple) – Pixel coordinates (x, y) of the starting point of the line.

  • end_img_point (tuple) – Pixel coordinates (x, y) of the ending point of the line.

  • color (Tuple[int, int, int], optional) – Color of the line in BGR format. Default is (0, 0, 255) for red.

Returns:

Image with the drawn distorted line between the two points.

Return type:

np.ndarray