fiftyone.utils.annotations¶
Data annotation utilities.
Classes
Configuration class that controls the look-and-feel of the annotations rendered on images/videos. |
Functions
|
Draws an annotated version of the image sample with its label field(s) overlaid to disk. |
|
Renders annotated versions of the image samples with label field(s) overlaid to the given directory. |
|
Draws an annotated version of the video sample with its label field(s) overlaid to disk. |
|
Renders annotated versions of the video samples with label field(s) overlaid to the given directory. |
-
class
fiftyone.utils.annotations.
AnnotationConfig
(d)¶ Bases:
eta.core.annotations.AnnotationConfig
Configuration class that controls the look-and-feel of the annotations rendered on images/videos.
- Parameters
show_frame_attr_names – (True) whether to render video/frame attribute names, if available
show_frame_attr_confidences – (False) whether to render video/frame attribute confidences, if available
frame_attrs_box_gap – (“1%”) the gap between the frame attributes box and the upper left corner of the image. This value is parsed by
eta.core.image.Width(frame_attrs_box_gap)
show_object_boxes – (True) whether to render object bounding boxes, if available. If this is
False
, labels, confidences, attributes, etc. are also hiddenshow_object_names – (True) whether to render object names, if available
show_object_labels – (True) whether to render object labels, if available
show_object_attrs – (True) whether to render object attributes, if available
show_object_confidences – (False) whether to render object label confidences, if available
per_object_name_colors – (True) whether to render boxes for objects with different names in different colors
per_object_label_colors – (True) whether to render boxes for objects with different labels in different colors
per_object_index_colors – (True) whether to render boxes for objects with different indexes in different colors
show_object_attr_names – (True) whether to render object attribute names, if available
show_object_attr_confidences – (False) whether to render object attribute confidences, if available
show_object_indices – (True) whether to render object indices, if available
show_object_masks – (True) whether to render object segmentation masks, if available
occluded_object_attr – (“occluded”) the name of the boolean attribute indicating whether an object is occluded
hide_occluded_objects – (False) whether to hide objects when they are occluded
show_event_boxes – (True) whether to render event bounding boxes, if available. If this is
False
, all attributes, confidences, etc. are also hiddenshow_event_labels – (True) whether to render event labels, if available
show_event_attrs – (True) whether to render event attributes, if available
show_event_names – (True) whether to render event names, if available
show_event_confidences – (False) whether to render event label confidences, if available
per_event_name_colors – (True) whether to render boxes for events with different names in different colors
per_event_label_colors – (True) whether to render boxes for events with different labels in different colors
per_event_index_colors – (True) whether to render boxes for events with different indexes in different colors
show_event_attr_names – (True) whether to render event attribute names, if available
show_event_attr_confidences – (False) whether to render event attribute confidences, if available
show_event_indices – (True) whether to render event indices, if available. By default, this is
True
show_event_masks – (True) whether to render event segmentation masks, if available
show_event_label_on_objects – (True) whether to render event labels as attributes ob objects that belong to events
show_event_objects_in_same_color – (True) whether to render objects that belong to events in the same color as their parent event
occluded_event_attr – (“occluded”) the name of the boolean attribute indicating whether an event is occluded
hide_occluded_events – (False) whether to hide events when they are occluded
bbox_alpha – (0.75) the transparency of bounding boxes
bbox_label_text_pad_pixels – (2) the padding, in pixels, around the text in bounding box labels
bbox_linewidth –
the linewidth, in pixels, of bounding boxes
mask_border_thickness – (2) the thickness, in pixels, to use when drawing the borders of segmentation masks
mask_fill_alpha – (0.7) the transparency of segmentation masks
show_frame_mask_semantics – (True) whether to render semantic labels for frame mask regions, when mask indexes are available
attrs_box_render_method – (“panel”) the method used to render object attributes
attrs_box_bg_color – (“#000000”) the background color for attributes boxes
attrs_box_bg_alpha – (0.5) the transparency of attribute panel boxes
attrs_box_text_pad_pixels – (5) the padding, in pixels, around the text in attribute boxes
attrs_box_text_line_spacing_pixels – (1) the padding, in pixels, between each line of text in attribute boxes
show_keypoints_names – (True) whether to render keypoints names, if available
show_keypoints_labels – (True) whether to render keypoints labels, if available
show_keypoints_attrs – (True) whether to render keypoints attributes, if available
show_keypoints_attr_names – (True) whether to render keypoint attribute names, if available
show_keypoints_attr_confidences – (False) whether to render keypoint attribute confidences, if available
per_keypoints_name_colors – (True) whether to render keypoints with different names in different colors
per_keypoints_label_colors – (True) whether to render keypoints with different labels in different colors
keypoints_size –
the size to render keypoints
keypoints_alpha – (0.75) the transparency of keypoints
show_polyline_names – (True) whether to render polyline names, if available
show_polyline_labels – (True) whether to render polyline labels, if available
show_polyline_attrs – (True) whether to render polyline attributes, if available
show_polyline_attr_names – (True) whether to render polyline attribute names, if available
show_polyline_attr_confidences – (False) whether to render polyline attribute confidences, if available
hide_non_filled_polyline_annos – (False) whether to override other settings and hide the annotation panels for non-filled polylines (those with
filled == False
)per_polyline_name_colors – (True) whether to render polylines with different names in different colors
per_polyline_label_colors – (True) whether to render polylines with different labels in different colors
polyline_alpha – (0.75) the transparency of polylines
polyline_linewidth – (3) the linewidth, in pixels, of non-filled polylines
fill_polylines – (True) whether to draw polylines as filled, when possible
show_all_names – (False) whether to render all names, if available. If set to
True
, this overrides all other name flagshide_all_names – (False) whether to hide all names, if available. If set to
True
, this overrides all other name flagsshow_name_only_titles – (False) whether to render titles that only contain the
name
of the entity (i.e., no label, confidence, index, etc)show_all_confidences – (False) whether to render all confidences, if available. If set to
True
, this overrides all other confidence flagshide_all_confidences – (False) whether to hide all confidences, if available. If set to
True
, this overrides all other confidence flagslabels_whitelist – (None) an optional whitelist of labels (of any kind). If provided, only entities whose
label
is in this list will be renderedlabels_blacklist – (None) an optional list of labels (of any kind) to not render
attr_names_blacklist – (None) an optional list of attribute names (of any kind) to not render
attr_values_blacklist – (None) an optional list of attribute values (of any kind) to not render
hide_false_boolean_attrs – (False) whether to hide attributes (of any kind) when they are
False
confidence_scaled_alpha – (False) whether to scale alpha values of objects and events based on their associated confidences
colormap_config – (None) the
eta.core.annotations.ColormapConfig
to use to select colors for objects/event boxestext_color – (“#FFFFFF”) the annotation text color
font_path – (
eta.core.constants.DEFAULT_FONT_PATH
) the path to thePIL.ImageFont
to usefont_size –
the font size to use
scale_by_media_height – (True) whether to scale font sizes and linewidths according to the height of the media (relative to a height of 720 pixels)
add_logo – (False) whether to add a logo to the frames
logo_config – (None) the
eta.core.logo.LogoConfig
describing the logo to use
-
attributes
()¶ Returns a list of class attributes to be serialized.
This method is called internally by serialize() to determine the class attributes to serialize.
Subclasses can override this method, but, by default, all attributes in vars(self) are returned, minus private attributes, i.e., those starting with “_”. The order of the attributes in this list is preserved when serializing objects, so a common pattern is for subclasses to override this method if they want their JSON files to be organized in a particular way.
- Returns
a list of class attributes to be serialized
-
classmethod
builder
()¶ Returns a ConfigBuilder instance for this class.
-
property
colormap
¶
-
copy
()¶ Returns a deep copy of the object.
- Returns
a Serializable instance
-
custom_attributes
(dynamic=False, private=False)¶ Returns a customizable list of class attributes.
By default, all attributes in vars(self) are returned, minus private attributes (those starting with “_”).
- Parameters
dynamic – whether to include dynamic properties, e.g., those defined by getter/setter methods or the @property decorator. By default, this is False
private – whether to include private properties, i.e., those starting with “_”. By default, this is False
- Returns
a list of class attributes
-
classmethod
default
()¶ Returns the default config instance.
By default, this method instantiates the class from an empty dictionary, which will only succeed if all attributes are optional. Otherwise, subclasses should override this method to provide the desired default configuration.
-
property
font
¶
-
classmethod
from_dict
(d)¶ Constructs a Config object from a JSON dictionary.
Config subclass constructors accept JSON dictionaries, so this method simply passes the dictionary to cls().
- Parameters
d – a dict of fields expected by cls
- Returns
an instance of cls
-
classmethod
from_json
(path, *args, **kwargs)¶ Constructs a Serializable object from a JSON file.
Subclasses may override this method, but, by default, this method simply reads the JSON and calls from_dict(), which subclasses must implement.
- Parameters
path – the path to the JSON file on disk
*args – optional positional arguments for self.from_dict()
**kwargs – optional keyword arguments for self.from_dict()
- Returns
an instance of the Serializable class
-
classmethod
from_kwargs
(**kwargs)¶ Constructs a Config object from keyword arguments.
- Parameters
**kwargs – keyword arguments that define the fields expected by cls
- Returns
an instance of cls
-
classmethod
from_str
(s, *args, **kwargs)¶ Constructs a Serializable object from a JSON string.
Subclasses may override this method, but, by default, this method simply parses the string and calls from_dict(), which subclasses must implement.
- Parameters
s – a JSON string representation of a Serializable object
*args – optional positional arguments for self.from_dict()
**kwargs – optional keyword arguments for self.from_dict()
- Returns
an instance of the Serializable class
-
classmethod
get_class_name
()¶ Returns the fully-qualified class name string of this object.
-
property
linewidth
¶
-
classmethod
load_default
()¶ Loads the default config instance from file.
Subclasses must implement this method if they intend to support default instances.
-
property
logo
¶
-
property
media_height
¶
-
static
parse_array
(d, key, default=<eta.core.config.NoDefault object>)¶ Parses a raw array attribute.
- Parameters
d – a JSON dictionary
key – the key to parse
default – a default list to return if key is not present
- Returns
a list of raw (untouched) values
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_bool
(d, key, default=<eta.core.config.NoDefault object>)¶ Parses a boolean value.
- Parameters
d – a JSON dictionary
key – the key to parse
default – a default bool to return if key is not present
- Returns
True/False
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_categorical
(d, key, choices, default=<eta.core.config.NoDefault object>)¶ Parses a categorical JSON field, which must take a value from among the given choices.
- Parameters
d – a JSON dictionary
key – the key to parse
choices – either an iterable of possible values or an enum-like class whose attributes define the possible values
default – a default value to return if key is not present
- Returns
the raw (untouched) value of the given field, which is equal to a value from choices
- Raises
ConfigError – if the key was present in the dictionary but its value was not an allowed choice, or if no default value was provided and the key was not found in the dictionary
-
static
parse_dict
(d, key, default=<eta.core.config.NoDefault object>)¶ Parses a dictionary attribute.
- Parameters
d – a JSON dictionary
key – the key to parse
default – a default dict to return if key is not present
- Returns
a dictionary
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_int
(d, key, default=<eta.core.config.NoDefault object>)¶ Parses an integer attribute.
- Parameters
d – a JSON dictionary
key – the key to parse
default – a default integer value to return if key is not present
- Returns
an int
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_mutually_exclusive_fields
(fields)¶ Parses a mutually exclusive dictionary of pre-parsed fields, which must contain exactly one field with a truthy value.
- Parameters
fields – a dictionary of pre-parsed fields
- Returns
the (field, value) that was set
- Raises
ConfigError – if zero or more than one truthy value was found
-
static
parse_number
(d, key, default=<eta.core.config.NoDefault object>)¶ Parses a number attribute.
- Parameters
d – a JSON dictionary
key – the key to parse
default – a default numeric value to return if key is not present
- Returns
a number (e.g. int, float)
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_object
(d, key, cls, default=<eta.core.config.NoDefault object>)¶ Parses an object attribute.
The value of d[key] can be either an instance of cls or a serialized dict from an instance of cls.
- Parameters
d – a JSON dictionary
key – the key to parse
cls – the class of d[key]
default – a default cls instance to return if key is not present
- Returns
an instance of cls
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_object_array
(d, key, cls, default=<eta.core.config.NoDefault object>)¶ Parses an array of objects.
The values in d[key] can be either instances of cls or serialized dicts from instances of cls.
- Parameters
d – a JSON dictionary
key – the key to parse
cls – the class of the elements of list d[key]
default – the default list to return if key is not present
- Returns
a list of cls instances
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_object_dict
(d, key, cls, default=<eta.core.config.NoDefault object>)¶ Parses a dictionary whose values are objects.
The values in d[key] can be either instances of cls or serialized dicts from instances of cls.
- Parameters
d – a JSON dictionary
key – the key to parse
cls – the class of the values of dictionary d[key]
default – the default dict of cls instances to return if key is not present
- Returns
a dictionary whose values are cls instances
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
static
parse_raw
(d, key, default=<eta.core.config.NoDefault object>)¶ Parses a raw (arbitrary) JSON field.
- Parameters
d – a JSON dictionary
key – the key to parse
default – a default value to return if key is not present
- Returns
the raw (untouched) value of the given field
- Raises
ConfigError – if no default value was provided and the key was not found in the dictionary
-
static
parse_string
(d, key, default=<eta.core.config.NoDefault object>)¶ Parses a string attribute.
- Parameters
d – a JSON dictionary
key – the key to parse
default – a default string to return if key is not present
- Returns
a string
- Raises
ConfigError – if the field value was the wrong type or no default value was provided and the key was not found in the dictionary
-
property
scale_factor
¶
-
serialize
(reflective=False)¶ Serializes the object into a dictionary.
Serialization is applied recursively to all attributes in the object, including element-wise serialization of lists and dictionary values.
- Parameters
reflective – whether to include reflective attributes when serializing the object. By default, this is False
- Returns
a JSON dictionary representation of the object
-
set_media_size
(frame_size=None, shape=None, img=None)¶ Sets the size of the media to the given value. This allows for optimizing font sizes, linewidths, and logo resolutions to suit the dimensions of the media being annotated.
Exactly one keyword argument must be provided.
- Parameters
frame_size – the (width, height) of the image/video frame
shape – the (height, width, …) of the image/video frame, e.g. from img.shape
img – an example image/video frame
-
to_str
(pretty_print=True, **kwargs)¶ Returns a string representation of this object.
- Parameters
pretty_print – whether to render the JSON in human readable format with newlines and indentations. By default, this is True
**kwargs – optional keyword arguments for self.serialize()
- Returns
a string representation of the object
-
static
validate_all_or_nothing_fields
(fields)¶ Validates a dictionary of pre-parsed fields checking that either all or none of the fields have a truthy value.
- Parameters
fields – a dictionary of pre-parsed fields
- Raises
ConfigError – if some values are truth and some are not
-
write_json
(path, pretty_print=False, **kwargs)¶ Serializes the object and writes it to disk.
- Parameters
path – the output path
pretty_print – whether to render the JSON in human readable format with newlines and indentations. By default, this is False
**kwargs – optional keyword arguments for self.serialize()
-
fiftyone.utils.annotations.
draw_labeled_images
(samples, anno_dir, label_fields=None, annotation_config=None)¶ Renders annotated versions of the image samples with label field(s) overlaid to the given directory.
The filenames of the sample images are maintained, unless a name conflict would occur in
anno_dir
, in which case an index of the form"-%d" % count
is appended to the base filename.The images are written in format
fo.config.default_image_ext
.- Parameters
samples – an iterable of
fiftyone.core.sample.Sample
instancesanno_dir – the directory to write the annotated images
label_fields (None) – a list of
fiftyone.core.labels.ImageLabel
fields to render. If omitted, all compatiable fields are renderedannotation_config (None) – an
AnnotationConfig
specifying how to render the annotations
- Returns
the list of paths to the labeled images
-
fiftyone.utils.annotations.
draw_labeled_image
(sample, outpath, label_fields=None, annotation_config=None)¶ Draws an annotated version of the image sample with its label field(s) overlaid to disk.
- Parameters
sample – a
fiftyone.core.sample.Sample
instanceoutpath – the path to write the annotated image
label_fields (None) – a list of
fiftyone.core.labels.ImageLabel
fields to render. If omitted, all compatiable fields are renderedannotation_config (None) – an
AnnotationConfig
specifying how to render the annotations
-
fiftyone.utils.annotations.
draw_labeled_videos
(samples, anno_dir, label_fields=None, annotation_config=None)¶ Renders annotated versions of the video samples with label field(s) overlaid to the given directory.
The filenames of the sample videos are maintained, unless a name conflict would occur in
anno_dir
, in which case an index of the form"-%d" % count
is appended to the base filename.The videos are written in format
fo.config.default_video_ext
.- Parameters
samples – an iterable of
fiftyone.core.sample.Sample
instancesanno_dir – the directory to write the annotated videos
label_fields (None) – a list of
fiftyone.core.labels.ImageLabel
fields on the frames of the samples to render. If omitted, all compatiable fields are renderedannotation_config (None) – an
AnnotationConfig
specifying how to render the annotations
- Returns
the list of paths to the labeled videos
-
fiftyone.utils.annotations.
draw_labeled_video
(sample, outpath, label_fields=None, annotation_config=None)¶ Draws an annotated version of the video sample with its label field(s) overlaid to disk.
- Parameters
sample – a
fiftyone.core.sample.Sample
instanceoutpath – the path to write the annotated image
label_fields (None) – a list of
fiftyone.core.labels.ImageLabel
fields on the frames of the sample to render. If omitted, all compatiable fields are renderedannotation_config (None) – an
AnnotationConfig
specifying how to render the annotations