bbox_converter#
Set of functions to import, export or simply convert bounding box numpy arrays or dataframe depending on the convention used.
12 compatible formats, compatible with
cAIpy JSON
COCO
darknet
etc…
Functions
Generate the column names from the format string. |
|
Generate the column names from dictionary of options, generated by |
|
Convert bounding box from a particular format to another, using a composition of |
|
Convert bounding boxes in Lours's reference to a desired format. |
|
Convert bounding boxes from a particular format to cAIpy/COCO reference. |
|
Function used to parse the format string and convert it to a dictionary with options as boolean and an order list |
- column_names_from_format_string(format_string: str) list[str][source]#
Generate the column names from the format string.
Essentially, it calls a composition of
parse_format_string()andcolumn_names_from_options()This function is useful when you want to drop these columns from the annotations dataframe in you Dataset object
- column_names_from_options(relative: bool, center: bool, size: bool, point: bool) list[str][source]#
Generate the column names from dictionary of options, generated by
parse_format_string()This is useful to know what column to take from input dataframe when importing bounding boxes, or what name must be given to output dataframe when exporting bounding boxes
- Parameters:
relative – Whether the box coordinates are relative to image size (coordinates are usually between 0 and 1) or not (coordinates are in pixels).
center – Whether the first xy coordinate are linked to center of box or bottom left corner (in this case, it’s xmin and ymin)
size – Whether the second xy coordinates are box width and height or coordinate of top right corner (in this case, it’s xmax and ymax). Note that size cannot be False if center is True.
point – Whether the second xy coordinates are expected to be 0 (because it’s only a point) or not.
- Returns:
list of column names in the right order
- convert_bbox(bounding_boxes: ndarray | DataFrame, images_df: DataFrame, image_ids: Sequence[int] | None = None, input_format: str = 'XYWH', output_format: str = 'cxwcyh') DataFrame[source]#
Convert bounding box from a particular format to another, using a composition of
import_bbox()andexport_bbox().See their documentation for an explanation of format strings
- Parameters:
bounding_boxes –
array or dataframe containing the bbox coordinates.
If it’s an array,
image_idsmust be given.If it’s a dataframe, corresponding columns of
input_formatmust be present
images_df – dataframe containing image data, and especially image sizes.
image_ids – optional list to link every bounding box to its corresponding image with its id. As such, this must be the same length as
bounding_boxesarray. Note that ifbounding_boxesis a dataframe and has aimage_idcolumn, this option is not used. Defaults to None.input_format – string defining the expected input format. Defaults to “XYWH”.
output_format – string describing the desired bounding box format. Defaults to “XYWH”.
- Raises:
ValueError – This error is raised if the input format string is not well formatted or if bounding boxes and image ids shapes are incompatible
- Returns:
dataframe containing the bounding box coordinates with the corresponding column names. It will have the same index as
bounding_boxesif it is a dataframe.
- export_bbox(annotations_df: DataFrame, images_df: DataFrame, input_format: str = 'XYWH', output_format: str = 'XYWH') DataFrame[source]#
Convert bounding boxes in Lours’s reference to a desired format.
output_formatis a format string following the same syntax asinput_formatinimport_bbox().cxstands for x-coordinate of the center of the boxfirst
xstands for x-coordinate of the leftmost point of the box (i.e. the minimum x value)second
xstands for x-coordinate of the rightmost point of the box (i.e. the maximum x value)wstands for the box widthcystands for the y-coordinates of the center of the boxfirst
ystands for y-coordinate of the upper point of the box (i.e. the minimum y value)second
ystands for y-coordinate of the bottom point of the box (i.e. the maximum y value)hstands fo the box height
In addition, letters in uppercase indicate that the coordinate are in pixels, while lowercase indicate that they are relative to the image size, i.e. 1 is the full width of height of the image.
Example
cAIpy / COCO:
"XYWH" # x left, y top, width, height, all in pixels
darknet:
"cxwcyh" # x center, width, y center, height, all in relative
Note
x-coordinates and y-coordinates must follow the same convention. As such, examples like
xxYHare not allowedorder can be either
{x1}{x2}{y1}{y2}or{x1}{y1}{x2}{y2}. Order is important if the output dataframe is expected to be converted to numpy after calling this functioncenter coordinate can only be used with box size.
cxxcyyis thus not allowedFinally, only 12 options are allowed options. Here is the list :
cxwcyh,xwyh,xxyy,cxcywh,xywh,xyxy,CXWCYH,XWYH,XXYY,CXCYWH,XYWH,XYXY
The created dataframe will have the following column names
center coordinates are named
box_{x/y}_centerextremal coordinated are named
box_{x/y}_{min/max}box sizes are named
box_{width/height}If coordinates are relative, names get appended the
_relativesuffix
Example
cAipy / COCO:
"XYWH" # ["box_x_min", "box_y_min", "box_width", "box_height"]
darknet:
"cxwcyh" # ["box_x_center_relative", "box_width_relative", # "box_y_center_relative", "box_height_relative"]
- Parameters:
annotations_df – dataframe containing the annotations in Lours format. Must have at least
box_x_min,box_width,box_y_min,box_heightandimage_idcolumns.images_df – dataframe containing the image sizes in Lours format. Must have at least
width,height, and the ids must correspond in the columnimage_idthe annotations dataframe.input_format – string defining the expected input format. Defaults to “XYWH”.
output_format – string describing the desired bounding box format (see above). Defaults to “XWYH”.
- Returns:
dataframe containing the bounding box coordinates with the corresponding column names. It will have the same index as
annotations_df
- import_bbox(bounding_boxes: DataFrame | ndarray, images_df: DataFrame, image_ids: Sequence[int] | Series | ndarray | None = None, input_format: str = 'XYWH') DataFrame[source]#
Convert bounding boxes from a particular format to cAIpy/COCO reference. Essentially, this will convert bounding box coordinates to pixel coordinates with box size.
input_formatis a format string following a particular syntax to quickly let the function figure what conversion operation to do.cxstands for x-coordinate of the center of the boxfirst
xstands for x-coordinate of the leftmost point of the box (i.e. the minimum x value)second
xstands for x-coordinate of the rightmost point of the box (i.e. the maximum x value)wstands for the box widthcystands for the y-coordinates of the center of the boxfirst
ystands for y-coordinate of the upper point of the box (i.e. the minimum y value)second
ystands for y-coordinate of the bottom point of the box (i.e. the maximum y value)hstands fo the box height
In addition, letters in uppercase indicate that the coordinate are in pixels, while lowercase indicate that they are relative to the image size, i.e. 1 is the full width of height of the image.
Example
cAIpy / COCO:
"XYWH" # x left, y top, width, height, all in pixels
darknet:
"cxwcyh" # x center, width, y center, height, all in relative
Note
x-coordinates and y-coordinates must follow the same convention. As such, examples like
xxYHare not allowedorder can be either
{x1}{x2}{y1}{y2}or{x1}{y1}{x2}{y2}. Ifbounding_boxesis a dataframe, it should not change anything as columns are named, but order is important if it is a numpy array insteadcenter coordinate can only be used with box size.
cxxcyyis thus not allowedFinally, only 12 options are allowed options. Here is the list :
cxwcyh,xwyh,xxyy,cxcywh,xywh,xyxy,CXWCYH,XWYH,XXYY,CXCYWH,XYWH,XYXY
In the case
bounding_boxesis a dataframe, columns must correspond to the given format string. The convention is the following:center coordinates are named
box_{x/y}_centerextremal coordinated are named
box_{x/y}_{min/max}box sizes are named
box_{width/height}If coordinates are relative, names get appended the
_relativesuffix
Example
cAipy / COCO:
"XYWH" # ["box_x_min", "box_y_min", "box_width", "box_height"]
darknet:
"cxwcyh" # ["box_x_center_relative", "box_width_relative", # "box_y_center_relative", "box_height_relative"]
- Parameters:
bounding_boxes –
array or dataframe containing the bbox coordinates
If it’s an array,
image_idsmust be given, and its shape must be (4, N)If it’s a dataframe, corresponding columns of
input_formatmust be present (see example above)
images_df – dataframe containing image data, and especially image sizes.
image_ids – optional list to link every bounding box to its corresponding image with its id. As such, this must be the same length as
bounding_boxesarray. Note that ifbounding_boxesis an array and has aimage_idcolumn, this option is not used. Defaults to None.input_format – string defining the expected input format. Defaults to “XYWH”.
- Raises:
ValueError – This error is raised if the input format string is not well formatted or if bounding boxes and image ids shapes are incompatible
- Returns:
DataFrame containing the reference cAIpy format, i.e. with x left, width, y top and height, in pixels. index is the same one os
bounding_boxes, and columns are, as described above,box_x_min,box_width,box_y_minandbox_height. This is the format expected in theDatasetorEvaluator <lours.evaluation.Evaluator
- parse_format_string(format_string: str) tuple[dict[str, bool], list[int]][source]#
Function used to parse the format string and convert it to a dictionary with options as boolean and an order list
output dictionary keys are:
“center”: if the first x or y coordinate is the center of the box or the minimum value (left for x, top for y)
“size”: if the second coordinate is the size of the box (width for x, height for y), or the maximum value (right for x, bottom for y)
“relative”: if the coordinated are in pixels of normalized with image size (image width for x, image height for y)
order output list is order in which x1, x2, y1, y2 needs to be taken for the input array of bounding boxes. Possible values are
[0, 1, 2, 3](for e.g.xxyy) or[0, 2, 1, 3](for e.g.xyxy)- Parameters:
format_string – format string describing the convention used for box coordinates can be e.g.
XYWH(cAIpy / COCO format),cxwcyh(darknet format) etc. See documentation inimport_bbox()andexport_bbox()for a breakdown of the syntax.- Raises:
ValueError – raised when the formatstring is not in the 12 allowed values.
- Returns:
tuple containing two elements - dictionary of options (see detailed keys above) - list of column positions for x1, x2, y1, y1 in the bbox array