Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing attributes through during label coercions #1993

Merged
merged 4 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fiftyone/utils/data/exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ def _classification_to_detections(label):
label=label.label,
bounding_box=[0, 0, 1, 1],
confidence=label.confidence,
**dict(label.iter_attributes()),
)
]
)
Expand Down
67 changes: 41 additions & 26 deletions fiftyone/utils/data/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,12 @@ class ImageClassificationSampleParser(LabeledImageTupleSampleParser):
{
"label": <label-or-target>,
"confidence": <confidence>,
"attributes": <optional-attributes>,
}

- a :class:`fiftyone.core.labels.Classification` or
:class:`fiftyone.core.labels.Classifications` instance

Args:
classes (None): an optional list of class label strings. If provided,
it is assumed that ``target`` contains class ID that should be
Expand Down Expand Up @@ -947,6 +951,9 @@ def _parse_label(self, target):
if target is None:
return None

if isinstance(target, (fol.Classification, fol.Classifications)):
return target

is_list = isinstance(target, (list, tuple))

if not is_list:
Expand Down Expand Up @@ -989,36 +996,38 @@ class ImageDetectionSampleParser(LabeledImageTupleSampleParser):
- ``image_or_path`` is either an image that can be converted to numpy
format via ``np.asarray()`` or the path to an image on disk

- ``detections_or_path`` is either a list of detections in the
following format::

[
{
"<label_field>": <label-or-target>,
"<bounding_box_field>": [
<top-left-x>, <top-left-y>, <width>, <height>
],
"<confidence_field>": <optional-confidence>,
"<attributes_field>": {
<optional-name>: <optional-value>,
- ``detections_or_path`` can be any of the following:

- None, for unlabeled images
- a list of detections in the following format::

[
{
"<label_field>": <label-or-target>,
"<bounding_box_field>": [
<top-left-x>, <top-left-y>, <width>, <height>
],
"<confidence_field>": <optional-confidence>,
"<attributes_field>": {
<optional-name>: <optional-value>,
...
}
},
...
}
},
...
]
]

or the path to such a file on disk. For unlabeled images,
``detections_or_path`` can be ``None``.
In the above, ``label-or-target`` is either a class ID
(if ``classes`` is provided) or a label string, and the bounding
box coordinates can either be relative coordinates in ``[0, 1]``
(if ``normalized == True``) or absolute pixels coordinates
(if ``normalized == False``). The confidence and attributes
fields are optional for each sample.

In the above, ``label-or-target`` is either a class ID
(if ``classes`` is provided) or a label string, and the bounding box
coordinates can either be relative coordinates in ``[0, 1]``
(if ``normalized == True``) or absolute pixels coordinates
(if ``normalized == False``). The confidence and attributes fields
are optional for each sample.
The input field names can be configured as necessary when
instantiating the parser.

The input field names can be configured as necessary when
instantiating the parser.
- the path on disk to a file in the above format
- a :class:`fiftyone.core.labels.Detections` instance

Args:
label_field ("label"): the name of the object label field in the
Expand Down Expand Up @@ -1079,6 +1088,9 @@ def _parse_label(self, target, img=None):
if target is None:
return None

if isinstance(target, fol.Detections):
return target

if etau.is_str(target):
target = etas.read_json(target)

Expand Down Expand Up @@ -1257,6 +1269,9 @@ def get_label(self):
if labels is None:
return None

if isinstance(labels, fol.TemporalDetections):
return labels

detections = []
for label_dict in labels:
label = label_dict["label"]
Expand Down
13 changes: 12 additions & 1 deletion fiftyone/utils/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __iter__(self):
alpha=self.alpha,
)
if self.include_labels:
yield patch, detection.label
yield patch, _to_classification(detection)
else:
yield patch

Expand Down Expand Up @@ -167,3 +167,14 @@ def _load_image(image_path, force_rgb=False):
# pylint: disable=no-member
flag = cv2.IMREAD_COLOR if force_rgb else cv2.IMREAD_UNCHANGED
return etai.read(image_path, flag=flag)


def _to_classification(label):
if label is None:
return label

return fol.Classification(
label=label.label,
confidence=label.confidence,
**dict(label.iter_attributes()),
)