Polygon Zone Superposition - Clipping bug
Search before asking
- [x] I have searched the Supervision issues and found no similar bug report.
Bug
I've identified a bug in the PolygonZone class that allows a single object to occupy two ROIs even if they have no overlap. It seems the bug was introduced here:
https://github.com/roboflow/supervision/issues/1101#issuecomment-2051863063
By moving the clipping calculation from using the frame height/width to the ROI height/width you're creating two separate bounding boxes for each separate ROI. This clips the boxes to exist within the bounds of the boolean numpy array (good) but then calculates the anchor point AFTER modifying the box (bad). This allows a single object to have two different anchor points for different ROIs causing the object to exists within two zones at once.
I propose that instead of clipping the incoming BBox to within the ROI then calculating anchor points do the following:
- Calculate the center of the box
- Check if the center point exceeds the min/max of the shape of the
self.maskarray. If so return False as the BBox can not possible occupy the ROI - Finally check the point against the
self.maskarray and return the result.
Environment
- Supervision: 0.26.1
- Python 3.12.3
Minimal Reproducible Example
rois: roi1 = np.array([[0,167],[422,79],[457,427],[102,547]], np.int32) roi2 = np.array([[458,67],[1011,75],[997,309],[515,609]], np.int32) roi3 = np.array([[0,683],[457,474],[474,639],[219,720]], np.int32)
detection: Detections(xyxy=array([[441.50900269, 258.48202515, 718.4284668 , 712.74450684]]), mask=None, confidence=array([0.89746094]), class_id=array([0]), tracker_id=array([3]), data={'class_name': array(['person'], dtype='<U6')}, metadata={})
Additional
No response
Are you willing to submit a PR?
- [x] Yes I'd like to help by submitting a PR!
I've been looking into this issue and I think I have a potential fix. Would it be okay if I worked on this and submitted a PR?
Hmm seems like you beat me to it. I've created my own PR here: https://github.com/roboflow/supervision/pull/1992