API¶
This part of the documentation covers all the interfaces of Image Segmantic.
Annotation Object¶
-
class
imantics.
Annotation
(image=None, category=None, bbox=None, mask=None, polygons=None, id=0, color=None, metadata={}, width=0, height=0)[source]¶ Annotation is a marking on an image.
This class acts as a level ontop of
BBox
,Mask
andPolygons
to manage and generate other annotations or export formats.-
area
¶ Qantity that expresses the extent of a two-dimensional figure
-
array
¶ Numpy array boolean mask repsentation of the annotations
-
coco
(include=True)[source]¶ Generates COCO format of annotation
Parameters: include (bool) – True to include all COCO formats, Fale to generate just annotation format Returns: COCO format of annotation Return type: dict
-
export
(style='coco')¶ Exports object into specified style
-
classmethod
from_bbox
(bbox, image=None, category=None)[source]¶ Creates annotation from bounding box
Parameters:
-
classmethod
from_mask
(mask, image=None, category=None)[source]¶ Creates annotation class from a mask
Parameters:
-
classmethod
from_polygons
(polygons, image=None, category=None)[source]¶ Creates annotation from polygons
Accepts following format for lists:
# Segmentation Format [ [x1, y1, x2, y2, x3, y3,...], [x1, y1, x2, y2, x3, y3,...], ... ]
or
# Point Format [ [[x1, y1], [x2, y2], [x3, y3],...], [[x1, y1], [x2, y2], [x3, y3],...], ... ]
No sepcificaiton is reqiured between which format is used
Parameters:
-
size
¶ Tuple of width and height
-
vgg
()¶ Export object in VGG format
-
Bounding Box Object¶
-
class
imantics.
BBox
(bbox, style=None)[source]¶ Bounding Box is an enclosing retangular box for a image marking
-
MIN_MAX
= 'minmax'¶ Bounding box format style [x1, y1, x2, y2]
-
WIDTH_HEIGHT
= 'widthheight'¶ Bounding box format style [x1, y1, width, height]
-
bbox
(style=None)[source]¶ Generates tuple repersentation of bounding box
Parameters: style – stlye to generate bounding box (defaults: MIN_MAX) Returns: tuple of bounding box with specified style
-
bottom_left
¶ Tops left point of the bounding box:
[ ]------[ ] | | | | [X]------[ ]
-
bottom_right
¶ Tops left point of the bounding box:
[ ]------[ ] | | | | [ ]------[X]
-
draw
(image, color=None, thickness=2)[source]¶ Draws a bounding box to the image array of shape (width, height, 3)
This function modifies the image array
Parameters:
-
classmethod
from_mask
(mask)[source]¶ Creates
BBox
from maskParameters: mask ( Mask
, numpy.ndarray, list) – object to generate bounding boxReturns: BBox
repersentation
-
classmethod
from_polygons
(polygons)[source]¶ Creates
BBox
from polygonsParameters: polygons ( Polygons
, list) – object to generate bounding boxReturns: BBox
repersentation
-
mask
(width=None, height=None)[source]¶ Returns or generates
Mask
representation of bounding box.Returns: Mask representation Return type: Mask
-
max_point
¶ Maximum points of the bounding box (x2, y2)
-
min_point
¶ Minimum points of the bounding box (x1, y1)
-
polygons
()[source]¶ Returns or generates
Polygons
representation of bounding box.Returns: Polygon representation Return type: Polygons
-
size
¶ Width and height as a tuple (width, height)
-
top_left
¶ Tops left point of the bounding box:
[X]------[ ] | | | | [ ]------[ ]
-
top_right
¶ Tops right point of the bounding box:
[ ]------[X] | | | | [ ]------[ ]
-
Category Object¶
Color Object¶
-
class
imantics.
Color
(hls=None, rgb=None, hex=None)[source]¶ -
classmethod
create
(color)[source]¶ Creates color class
string - generates color from hex tuple and values between [0, 1] - generates from hls tuple and values between [0, 255] - generates from rgb
Parameters: color – tuple, list, str Returns: color class
-
hex
¶ Hex representation of color
-
hls
¶ HLS representation of color
-
classmethod
random
(h=(0, 1), l=(0.35, 0.7), s=(0.6, 1))[source]¶ Generates a random color
Parameters: Returns: randomly generated color
Return type:
-
rgb
¶ RGB representation of color
-
classmethod
Dataset Object¶
-
class
imantics.
Dataset
(name, images=[], id=0, metadata={})[source]¶ -
add
(image)[source]¶ Adds image(s) to the current dataset
Parameters: image ( Image
Annotation
, list, typle, path) – list, object or path to add to dataset
-
export
(style='coco')¶ Exports object into specified style
-
classmethod
from_coco
(coco_obj, name='COCO Datset')[source]¶ Generates a dataset from a COCO object or python dict
Parameters: coco_obj (dict, pycocotools.coco.COCO) – Raises: ImportError – Raised if coco_obj is a pycocotools.coco.COCO object and it cannot be imported
-
split
(ratios, random=False)[source]¶ Splits dataset images into mutiple sub datasets of the given ratios
If a tuple of (1, 1, 2) was passed in the result would return 3 dataset objects of 25%, 25% and 50% of the images.
percents = ratios / ratios.sum()
Parameters: Returns: tuple of datasets with length of the number of ratios
Return type:
-
vgg
()¶ Export object in VGG format
-
voc
()¶ Export object in VOC format
Returns: object in format Return type: lxml.element
-
Image Object¶
-
class
imantics.
Image
(image_array=None, annotations=[], path='', id=0, metadata={}, dataset=None, width=0, height=0)[source]¶ -
add
(annotation, category=None)[source]¶ Adds an annotation, list of annotation, mask, polygon or bbox to current image. If annotation is not a Annotation a category is required List of non-Annotaiton objects will have the same category
Parameters: - annotation – annotaiton to add to current image
- category – required if annotation is not an Annotation object
-
draw
(bbox=True, outline=True, mask=True, text=True, thickness=3, alpha=0.5, categories=None, text_scale=0.5, color_by_category=False)[source]¶ Draws annotations on top of the image. If no image is loaded, annotations will be applied to a black image array.
Parameters: - bbox – Draw bboxes
- outline – Draw mask outlines
- mask – Draw masks
- alpha – opacity of masks (only applies to masks)
- thickness – pixel width of lines for outline and bbox
- color_by_category – Use the annotations’s category to us as color
- categories – List of categories to show
Returns: Image array with annotations
Return type: numpy.ndarray
-
export
(style='coco')¶ Exports object into specified style
-
classmethod
from_coco
(coco, dataset=None)[source]¶ Creates an
Image
from a dict in COCO formatted imageParameters: coco (dict) – COCO formatted image Return type: Image
-
classmethod
from_folder
(directory)[source]¶ Creates
Image
’s from all images found in directoryReturns: list of Image
’s
-
classmethod
from_path
(path)[source]¶ Returns an array of images if path is a directory Returns an
Image
if path is a file
-
vgg
()¶ Export object in VGG format
-
Mask Object¶
-
class
imantics.
Mask
(array)[source]¶ Mask class
-
bbox
()[source]¶ Returns or generates
BBox
representation of mask.Returns: Bounding Box representation Return type: BBox
-
contains
(item)[source]¶ Checks whether a point (tuple), array or mask is within current mask.
Note: Masks and arrays must be fully contained to return True
Parameters: item – object to check Returns: bool if item is contained
-
draw
(image, color=None, alpha=0.5)[source]¶ Draws current mask to the image array of shape (width, height, 3)
This function modifies the image array
Parameters:
-
intersect
(other)[source]¶ Intersects the array of the specified mask with this masks’s array and returns the result as a new mask.
Parameters: other ( Mask
, numpy.ndarray) – mask to intersect withReturns: resulting Mask
-
iou
(other)[source]¶ Intersect over union value of the specified masks
Parameters: other ( Mask
, numpy.ndarray) – mask to compute value withReturns: resulting float value
-
match
(item, threshold=0.5)[source]¶ Given a overlap threashold determines if masks match
Parameters: - item (
Mask
) – item to compare with - threshold – max amount of overlap (percentage)
Returns: boolean determining if the items match
- item (
-
polygons
()[source]¶ Returns or generates
Polygons
representation of mask.Returns: Polygons representation Return type: Polygons
-
Polygons Object¶
-
class
imantics.
Polygons
(polygons)[source]¶ -
INSTANCE_TYPES
= (<class 'list'>, <class 'tuple'>)¶ Polygon instance types
-
bbox
()[source]¶ Returns or generates
BBox
representation of polygons.Returns: Bounding Box representation Return type: BBox
-
draw
(image, color=None, thickness=3)[source]¶ Draws the polygons to the image array of shape (width, height, 3)
This function modifies the image array
Parameters:
-
classmethod
from_bbox
(bbox, style=None)[source]¶ Creates
Polygons
from bounding boxParameters: bbox ( BBox
, list, tuple) – object to generate bounding boxReturns: Polygons
repersentation
-
classmethod
from_mask
(mask)[source]¶ Creates
Polygons
from maskParameters: mask ( Mask
, numpy.ndarray, list) – object to generate maskReturns: Polygons
repersentation
-
mask
(width=None, height=None)[source]¶ Returns or generates
Mask
representation of polygons.Returns: Mask representation Return type: Mask
-
points
¶ Returns polygon in point format:
[ [[x1, y1], [x2, y2], [x3, y3], ...], [[x1, y1], [x2, y2], [x3, y3], ...], ... ]
-
segmentation
¶ Returns polygon in segmentation format:
[ [x1, y1, x2, y2, x3, y3, ...], [x1, y1, x2, y2, x3, y3, ...], ... ]
-