Skip to content

Add new methods to BoundingBox#4

Merged
ankandrew merged 4 commits intomainfrom
ankandrew/bbox-class-more-functionality
Apr 4, 2025
Merged

Add new methods to BoundingBox#4
ankandrew merged 4 commits intomainfrom
ankandrew/bbox-class-more-functionality

Conversation

@ankandrew
Copy link
Copy Markdown
Owner

  • Add from_detection_data utility
  • Add new clamp, is_valid and __iter__ methods to BoundingBox
  • Tests for new bbox class methods

@ankandrew ankandrew requested a review from Copilot April 4, 2025 18:14
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines +99 to +105
return BoundingBox(
x1=max(0, min(self.x1, max_width)),
y1=max(0, min(self.y1, max_height)),
x2=max(0, min(self.x2, max_width)),
y2=max(0, min(self.y2, max_height)),
)

Copy link

Copilot AI Apr 4, 2025

Choose a reason for hiding this comment

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

The clamp method might produce a bounding box with invalid dimensions (e.g., x1 == x2 or y1 == y2) when the original coordinates are out of bounds. Consider adjusting the logic or adding a safeguard to ensure the clamped bounding box remains valid.

Suggested change
return BoundingBox(
x1=max(0, min(self.x1, max_width)),
y1=max(0, min(self.y1, max_height)),
x2=max(0, min(self.x2, max_width)),
y2=max(0, min(self.y2, max_height)),
)
x1 = max(0, min(self.x1, max_width))
y1 = max(0, min(self.y1, max_height))
x2 = max(0, min(self.x2, max_width))
y2 = max(0, min(self.y2, max_height))
# Ensure the clamped bounding box has valid dimensions
if x1 >= x2:
x2 = x1 + 1
if y1 >= y2:
y2 = y1 + 1
return BoundingBox(x1, y1, x2, y2)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The clamp method is meant only to restrict the coordinates to the valid range, not to fix or validate the structure of the bounding box itself.

@ankandrew ankandrew requested a review from Copilot April 4, 2025 18:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

open_image_models/detection/core/base.py:83

  • The iter method returns the bounding box coordinates as (x1, y1, x2, y2), which differs from the to_xywh method that returns (x1, y1, width, height). Consider clarifying this difference in the docstring to avoid confusion.
def __iter__(self) -> Iterator[int]:

open_image_models/detection/core/base.py:133

  • The new from_detection_data method for DetectionResult is not covered by tests in this PR. Consider adding tests to verify that DetectionResult instances are correctly created from bounding box data, confidence scores, and class labels.
@classmethod

Comment on lines +97 to +98
:return: A new, clamped `BoundingBox`.
"""
Copy link

Copilot AI Apr 4, 2025

Choose a reason for hiding this comment

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

The clamp method directly clamps each coordinate without validating the original bounding box order. In cases where the input box is already invalid (for example, x1 >= x2), the clamping may produce an inverted or degenerate box. Consider handling or documenting the behavior for such inputs.

Suggested change
:return: A new, clamped `BoundingBox`.
"""
:return: A new, clamped `BoundingBox`.
:raises ValueError: If the bounding box is invalid.
"""
if not self.is_valid(max_width, max_height):
raise ValueError("Cannot clamp an invalid bounding box.")

Copilot uses AI. Check for mistakes.
@ankandrew ankandrew merged commit 69007bb into main Apr 4, 2025
7 checks passed
@ankandrew ankandrew deleted the ankandrew/bbox-class-more-functionality branch September 15, 2025 00:28
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.

2 participants