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

Fix polygon trigger false positives when bounding boxes are clipped #1748

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

Conversation

YoungjaeDev
Copy link

@YoungjaeDev YoungjaeDev commented Dec 17, 2024

Description

Please include a summary of the change and which issue is fixed or implemented. Please also include relevant motivation and context (e.g. links, docs, tickets etc.).

Fixed an issue where objects partially overlapping with polygons were incorrectly detected. Previously, when a polygon was positioned on the left side of the screen and a person on the right, with the person's bounding box slightly overlapping the polygon, the clip_boxes() function would forcefully clip the boxes to fit within the frame. This caused objects outside the frame to be incorrectly identified as being inside the polygon.

Changes

  • Modified the trigger() function to use original bounding box coordinates (pre-clipping) for polygon intersection checks
  • This reduces false positives and ensures more accurate counting while maintaining logical consistency

image

The original bounding box is large, and its bottom center point does not overlap with the polygon. However, during the clipping process, since the bottom-left corner intersects with the polygon, the box is forcefully shrunk. This causes the center point to shift inside the polygon, resulting in false detections.

Thank you

List any dependencies that are required for this change.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How has this change been tested, please provide a testcase or example of how you tested the change?

YOUR_ANSWER

Any specific deployment considerations

For example, documentation changes, usability, usage/costs, secrets, etc.

Docs

  • Docs updated? What were the changes:

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@SkalskiP
Copy link
Collaborator

Hi @YoungjaeDev 👋🏻 I'm not sure I understand correctly. Did your bounding boxes have coordinates that extended beyond the video frame?

@JShengP
Copy link

JShengP commented Dec 18, 2024

Hi, @SkalskiP 👋
Please also check #844 (comment)
PolygonZone trigger error

@YoungjaeDev
Copy link
Author

YoungjaeDev commented Dec 18, 2024

@SkalskiP

Hi,

In the video, the trigger_anchor settings are BOTTOM_LEFT and BOTTOM_CENTER.

However, as shown in the image, the bounding box of the person had only the left corner inside the polygon, yet the count still increased.

Upon inspecting the zone_count code, it seems that clipping the detections to the polygon’s x-max and y-max causes the bounding box width to narrow. Consequently, the BOTTOM_CENTER anchor ends up inside the polygon, triggering a count.

In the PR I submitted, the code is as follows

original_anchors = np.array([
    np.ceil(detections.get_anchors_coordinates(anchor)).astype(int)
    for anchor in self.triggering_anchors
])

original_anchors_clamped = np.clip(
    original_anchors,
    a_min=[0, 0],
    a_max=[self.mask.shape[1] - 1, self.mask.shape[0] - 1]
)

is_in_zone_original = self.mask[
    original_anchors_clamped[:, :, 1],
    original_anchors_clamped[:, :, 0]
].transpose().astype(bool)

This code calculates the anchors based on the original detection coordinates and clamps them to the polygon mask’s dimensions. This approach avoids distorting the bounding box and accurately checks whether the anchor is within the polygon.

@YoungjaeDev
Copy link
Author

Please understand that there are many masks on the image to protect privacy.

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.

4 participants