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

Add support for annotating frame views #4477

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

brimoor
Copy link
Contributor

@brimoor brimoor commented Jun 11, 2024

Adds support for passing frame views to annotate().

No changes are necessary in the annotation backends themselves to support frame views. From the backend's standpoint it's just like any other image collection is being annotated; the extra bookkeeping is handled automatically via the core SDK 🎉

import fiftyone as fo
import fiftyone.zoo as foz

dataset = foz.load_zoo_dataset("quickstart-video")

frames = dataset.to_frames(fps=1, sample_frames=True)

frames.annotate(
    "test",
    label_field="detections",
    label_type="detections",
    classes=["vehicle"],
    launch_editor=True,
)

# Add, edit, and delete vehicle labels...

dataset.load_annotations("test")

session = fo.launch_app(dataset)

Summary by CodeRabbit

  • Refactor
    • Improved the annotation process by removing checks for annotating frames and clips.
    • Enhanced ID mapping and handling of frame fields.
    • Better management of unexpected labels and annotation runs.
    • Improved merging of scalars and labels.
    • Updated methods for saving and updating ID maps.

These changes enhance the annotation functionality, especially for frames views, providing a more streamlined and efficient user experience.

@brimoor brimoor requested a review from ehofesmann June 11, 2024 03:45
Copy link
Contributor

coderabbitai bot commented Jun 11, 2024

Walkthrough

The changes in fiftyone/utils/annotations.py focus on enhancing the annotation process, particularly for frames views. This includes refactoring functions to handle frame fields, finalizing ID mapping, merging scalars and labels, and updating ID maps. The modifications aim to streamline the annotation workflow and improve data management.

Changes

File(s) Change Summary
fiftyone/utils/annotations.py Refactored annotation functionality, added methods for handling frame fields, finalizing ID maps, and merging labels. Improved support for frames views.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AnnotateFunction
    participant FinalizeIDMap
    participant LoadAnnotations
    participant MergeScalarsAndLabels

    User->>AnnotateFunction: Call annotate()
    AnnotateFunction->>FinalizeIDMap: Call _finalize_id_map()
    AnnotateFunction->>LoadAnnotations: Call load_annotations()
    LoadAnnotations->>MergeScalarsAndLabels: Call _merge_scalars() and _merge_labels()
    AnnotateFunction->>User: Return results
Loading

Poem

In fields of frames where labels play,
A rabbit hops, refines the way.
With IDs mapped and scalars merged,
Annotations now are surged.
Data flows in seamless streams,
Enhancing all our coding dreams.
🐇✨📊


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range and nitpick comments (9)
fiftyone/utils/annotations.py (9)

Line range hint 60-60: Replace percent format with format specifiers for better readability and performance.

- raise ValueError("The '%s' backend does not expose an API" % backend)
+ raise ValueError(f"The '{backend}' backend does not expose an API")

Line range hint 573-573: Replace equality comparison to True with a direct truth check.

- if F("filled") == True:
-     return samples.filter_labels(label_field, F("filled"), only_matches=False)
+ if F("filled"):
+     return samples.filter_labels(label_field, F("filled"), only_matches=False)

Line range hint 578-578: Replace equality comparison to False with a direct falsity check.

- if F("filled") == False:
-     return samples.filter_labels(label_field, not F("filled"), only_matches=False)
+ if not F("filled"):
+     return samples.filter_labels(label_field, not F("filled"), only_matches=False)

Line range hint 822-825: Use a ternary operator for a more concise and readable assignment.

- if etau.is_str(classes):
-     classes = [classes]
- else:
-     classes = list(classes)
+ classes = [classes] if etau.is_str(classes) else list(classes)

Line range hint 878-878: Replace equality comparison to True with a direct truth check.

- if attributes == True:
-     attributes = _get_label_attributes(samples, backend, label_field, label_type, classes=classes)
+ if attributes:
+     attributes = _get_label_attributes(samples, backend, label_field, label_type, classes=classes)

Line range hint 1045-1045: Simplify the method call by removing the unnecessary default parameter.

- expected_type = _RETURN_TYPES_MAP.get(label_type, None)
+ expected_type = _RETURN_TYPES_MAP.get(label_type)

Line range hint 1362-1365: Use a ternary operator for a more concise and readable assignment.

- if is_frame_field:
-     images = sample.frames.values()
- else:
-     images = [sample]
+ images = sample.frames.values() if is_frame_field else [sample]
Tools
Ruff

1357-1360: Use ternary operator sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None) instead of if-else-block (SIM108)

Replace if-else-block with sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None)


Line range hint 1603-1606: Use a ternary operator for a more concise and readable assignment.

- if is_list:
-     labels = image_label[list_field]
- else:
-     labels = [image_label]
+ labels = image_label[list_field] if is_list else [image_label]

Line range hint 2337-2337: Remove unnecessary inheritance from object in Python 3.

- class AnnotationAPI(object):
+ class AnnotationAPI:
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a78d4e6 and ae01126.

Files selected for processing (1)
  • fiftyone/utils/annotations.py (26 hunks)
Additional context used
Ruff
fiftyone/utils/annotations.py

60-60: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


206-207: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


220-221: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


277-278: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


289-289: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


429-436: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


446-449: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


453-456: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


573-573: Avoid equality comparisons to True; use if F("filled"): for truth checks (E712)

Replace with F("filled")


578-578: Avoid equality comparisons to False; use if not F("filled"): for false checks (E712)

Replace with not F("filled")


582-583: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


636-637: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


648-650: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


669-670: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


722-729: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


764-764: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


802-803: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


817-817: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


822-825: Use ternary operator classes = [classes] if etau.is_str(classes) else list(classes) instead of if-else-block (SIM108)

Replace if-else-block with classes = [classes] if etau.is_str(classes) else list(classes)


878-878: Avoid equality comparisons to True; use if attributes: for truth checks (E712)

Replace with attributes


938-938: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


943-950: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


960-961: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


968-969: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1045-1045: Use _RETURN_TYPES_MAP.get(label_type) instead of _RETURN_TYPES_MAP.get(label_type, None) (SIM910)

Replace _RETURN_TYPES_MAP.get(label_type, None) with _RETURN_TYPES_MAP.get(label_type)


1223-1226: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1230-1232: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1273-1273: Do not use bare except (E722)


1292-1294: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1298-1300: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1357-1360: Use ternary operator sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None) instead of if-else-block (SIM108)

Replace if-else-block with sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None)


1362-1365: Use ternary operator images = sample.frames.values() if is_frame_field else [sample] instead of if-else-block (SIM108)

Replace if-else-block with images = sample.frames.values() if is_frame_field else [sample]


1378-1378: Do not use bare except (E722)


1494-1494: Use key in dict instead of key in dict.keys() (SIM118)

Remove .keys()


1498-1498: Use key in dict instead of key in dict.keys() (SIM118)

Remove .keys()


1557-1560: Use ternary operator sample_annos = anno_dict if is_frames_view else anno_dict[sample_id] instead of if-else-block (SIM108)

Replace if-else-block with sample_annos = anno_dict if is_frames_view else anno_dict[sample_id]


1562-1565: Use ternary operator images = sample.frames.values() if is_frame_field else [sample] instead of if-else-block (SIM108)

Replace if-else-block with images = sample.frames.values() if is_frame_field else [sample]


1603-1606: Use ternary operator labels = image_label[list_field] if is_list else [image_label] instead of if-else-block (SIM108)

Replace if-else-block with labels = image_label[list_field] if is_list else [image_label]


2337-2337: Class AnnotationAPI inherits from object (UP004)

Remove object inheritance


2352-2352: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


2371-2371: Use format specifiers instead of percent format (UP031)

Replace with format specifiers

fiftyone/utils/annotations.py Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ae01126 and b77da70.

Files selected for processing (1)
  • fiftyone/utils/annotations.py (25 hunks)
Additional comments not posted (2)
fiftyone/utils/annotations.py (2)

1346-1349: Utilize existing comment to improve code readability.

The existing comment from a previous review already suggests using a ternary operator here for more concise code. Consider applying this change to improve readability.


1037-1037: Check the initialization of is_frames_view.

#!/bin/bash
# Description: Verify if `is_frames_view` is properly initialized before use.

# Test: Search for the initialization of `is_frames_view`. Expect: Initialization before use.
rg --type python --multiline $'is_frames_view =.*\n.*results._is_frames'

fiftyone/utils/annotations.py Show resolved Hide resolved
fiftyone/utils/annotations.py Show resolved Hide resolved
fiftyone/utils/annotations.py Show resolved Hide resolved
fiftyone/utils/annotations.py Show resolved Hide resolved
fiftyone/utils/annotations.py Show resolved Hide resolved
fiftyone/utils/annotations.py Show resolved Hide resolved
fiftyone/utils/annotations.py Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b77da70 and b0ba35c.

Files selected for processing (1)
  • fiftyone/utils/annotations.py (25 hunks)
Additional context used
Ruff
fiftyone/utils/annotations.py

60-60: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


206-207: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


220-221: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


277-278: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


289-289: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


429-436: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


446-449: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


453-456: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


573-573: Avoid equality comparisons to True; use if F("filled"): for truth checks (E712)

Replace with F("filled")


578-578: Avoid equality comparisons to False; use if not F("filled"): for false checks (E712)

Replace with not F("filled")


582-583: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


636-637: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


648-650: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


669-670: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


722-729: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


764-764: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


802-803: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


817-817: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


822-825: Use ternary operator classes = [classes] if etau.is_str(classes) else list(classes) instead of if-else-block (SIM108)

Replace if-else-block with classes = [classes] if etau.is_str(classes) else list(classes)


878-878: Avoid equality comparisons to True; use if attributes: for truth checks (E712)

Replace with attributes


938-938: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


943-950: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


960-961: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


968-969: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1045-1045: Use _RETURN_TYPES_MAP.get(label_type) instead of _RETURN_TYPES_MAP.get(label_type, None) (SIM910)

Replace _RETURN_TYPES_MAP.get(label_type, None) with _RETURN_TYPES_MAP.get(label_type)


1212-1215: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1219-1221: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1262-1262: Do not use bare except (E722)


1281-1283: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1287-1289: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1346-1349: Use ternary operator sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None) instead of if-else-block (SIM108)

Replace if-else-block with sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None)


1351-1354: Use ternary operator images = sample.frames.values() if is_frame_field else [sample] instead of if-else-block (SIM108)

Replace if-else-block with images = sample.frames.values() if is_frame_field else [sample]


1367-1367: Do not use bare except (E722)


1483-1483: Use key in dict instead of key in dict.keys() (SIM118)

Remove .keys()


1487-1487: Use key in dict instead of key in dict.keys() (SIM118)

Remove .keys()


1546-1549: Use ternary operator sample_annos = anno_dict if is_frames_view else anno_dict[sample_id] instead of if-else-block (SIM108)

Replace if-else-block with sample_annos = anno_dict if is_frames_view else anno_dict[sample_id]


1551-1554: Use ternary operator images = sample.frames.values() if is_frame_field else [sample] instead of if-else-block (SIM108)

Replace if-else-block with images = sample.frames.values() if is_frame_field else [sample]


1592-1595: Use ternary operator labels = image_label[list_field] if is_list else [image_label] instead of if-else-block (SIM108)

Replace if-else-block with labels = image_label[list_field] if is_list else [image_label]


2338-2338: Class AnnotationAPI inherits from object (UP004)

Remove object inheritance


2353-2353: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


2372-2372: Use format specifiers instead of percent format (UP031)

Replace with format specifiers

Additional comments not posted (8)
fiftyone/utils/annotations.py (8)

250-251: Ensure proper documentation for _finalize_id_map method.


1062-1064: Refactor to handle frame fields more efficiently.


1098-1102: Simplify the handling of unexpected labels.


1151-1156: Optimize the handling of unexpected annotations for frame views.


1204-1209: Ensure input validation for user-provided field names.


1314-1320: Clarify the mapping of frame IDs to sample IDs.


1346-1349: Use a ternary operator for a more concise and readable assignment.

Tools
Ruff

1346-1349: Use ternary operator sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None) instead of if-else-block (SIM108)

Replace if-else-block with sample_annos = anno_dict if is_frames_view else anno_dict.get(sample.id, None)


2236-2248: Document the _finalize_id_map method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant