Skip to content

PolygonZone: estimate how much of the zone is occupiedΒ #1449

Open

Description

@stevensagaert

Search before asking

  • I have searched the Supervision issues and found no similar feature requests.

Description

Next to the count of the objects in the zone, it would be useful to know how much of the zone area is occupied by the objects.

Use case

e.g. for loading zones in warehouses

Additional

Basically I've implemented it myself outside of the PolygonZone class. The core of what is needed is these two functions:

def polygon_to_binary_mask(img,contour):
    """convert bounding polygon coord to binary mask"""
    # Create binary mask
    b_mask = np.zeros(img.shape[:2], np.uint8)

    #  Extract contour result
    #contour = c.masks.xy.pop()
    #  Changing the type
    contour = contour.astype(np.int32) #Note: This must be int32 and not unit8!!!
    #  Reshaping
    contour = contour.reshape(-1, 1, 2)


    # Draw contour onto mask
    mask = cv2.drawContours(b_mask, [contour], -1, (1, 1, 1), cv2.FILLED)
    return mask

strategy:

  1. make union of binary masks of detected objects (only the classes for consideration in zone)
  2. make binary mask of the zone
  3. do and of zone mask & object masks
  4. sum the 1 pixels
def calculate_overlap_area(zone_mask, masks,show_plot=True):
    """calculate how much % of the zone is occupied"""
    #create one mask from the object masks as the union
    union_mask=np.bitwise_or.reduce(np.stack(masks),axis=0)
    #for debugging
    if show_plot:
        plt.title("union object mask")
        plt.imshow(union_mask,cmap='gray')
        plt.show()
    

    #do the bitwise and between union_mask & zone_mask
    overlap_mask=np.bitwise_and(union_mask,zone_mask)
    print(f"overlap_mask shape {overlap_mask.shape}")
    if show_plot:
        plt.title("overlap mask")
        plt.imshow(union_mask,cmap='gray')
        plt.show()
    
    overlap_size= np.sum(overlap_mask)
    zone_size=np.sum(zone_mask)
    return 100*overlap_size/zone_size

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!

Activity

  1. Sapienscoding commented on Oct 15, 2024

    @Sapienscoding

    Hi @stevensagaert, I'd like to help by submitting a PR

  2. LinasKo commented on Oct 15, 2024

    @LinasKo
    Contributor

    Hi @Sapienscoding,

    I've assigned another issue to you. Let's go with one at a time.

  3. LinasKo commented on Oct 15, 2024

    @LinasKo
    Contributor

    Hi @stevensagaert πŸ‘‹

    Apologies for the delay. If you're still keen, we'd be happy to include area occupation stats in our PolygonZone.

  4. AyuK03 commented on Oct 16, 2024

    @AyuK03

    Hi, it is my first time contributing to open source project and I would like to work on this issue. Please do guide me if possible. Thank you!

  5. stevensagaert commented on Oct 16, 2024

    @stevensagaert
    Author
  6. AHB102 commented on Oct 29, 2024

    @AHB102

    @stevensagaert Can I resolve this issue ?

  7. stevensagaert commented on Oct 30, 2024

    @stevensagaert
    Author

    You mean you want to implement it or just close it? I haven't had time to do it yet.

  8. AHB102 commented on Oct 30, 2024

    @AHB102

    @stevensagaert I would like to try to implement it.

  9. stevensagaert commented on Oct 30, 2024

    @stevensagaert
    Author
  10. LinasKo commented on Oct 30, 2024

    @LinasKo
    Contributor

    Hi @AHB102 πŸ‘‹

    Assigning this to you.

    Note that Detection can take one of 3 forms:

    • segmentation - these will contain the mask field
    • object detection - these will have the xyxy field, and not mask.
    • oriented bounding box - these will have a (N, 4, 2)-shaped polygon in detections.data[CLASS_NAME_DATA_FIELD]. (CLASS_NAME_DATA_FIELD == "xyxyxyxy" and resides in config.py)

    Ideally, this method should work regardless of detection type, but it's okay if it returns a slightly-approximated result.

    You may find the polygon_to_mask method helpful. it resides in: supervision/detection/utils.py.

  11. AHB102 commented on Oct 30, 2024

    @AHB102

    @LinasKo Thanks for the insight 😁, will get started on this.

  12. AHB102 commented on Nov 1, 2024

    @AHB102

    @LinasKo Hi πŸ‘‹,I haven't written much code for this feature. I mostly used Steven's existing code, but I added checks for the detection cases based on your recommendations. Had some questions :
    1)Type hinting: I wrote this feature in vanilla Python . I'm new to type hinting, is it still okay to use it ?
    2)Code location: I put the feature code in β€œsupervision/detection/utils.py”, outside of the PolygonZone class. Is that the right place?
    3)Testing: How do I test the feature? Can I use import statements, or is there a preferred way to write tests before submitting a pull request (PR)?

  13. LinasKo commented on Nov 1, 2024

    @LinasKo
    Contributor

    Hi @AHB102 πŸ‘‹

    1. Ideally, we'd love a bit of type hinting at least in function definitions. For arguments, it's as simple as adding : int, : str, : List[str] and so on. For numpy arrays it's fine to say np.ndarray. Return type is -> int: and so on. You can do -> None: if it returns nothing. I'd encourage you to try!
    2. I think so, but I'll need to see the PR first.
    3. We encourage making a Google Colab and showing how it works. If you want, you can use this Starter Template. There's no PolygonZone examples, but you can find some in the docs, the cheatsheet or a notebook.
      Try to come up with a situation where you can detect something in an image, and actually check the polygon zone occupancy. I suggest using Ultralytics for detection at the moment. Let me know how it goes, and if you can't figure it out πŸ˜‰
  14. AHB102 commented on Nov 1, 2024

    @AHB102

    Okay nice! Will definitely make the type hinting work, and can I commit the changes and make a PR ?

  15. LinasKo commented on Nov 1, 2024

    @LinasKo
    Contributor

    Yes, absolutely. Typically contributors fork the repo, make the changes their version of it, on a branch such as feat/estimate-polygonzone-occupation, and then open a PR via github website to supervision's develop (there should be a button, as soon as you push to your own fork).

  16. AHB102 commented on Nov 12, 2024

    @AHB102

    @LinasKo I got a bit lost in the first PR. I believe this one is correct. Thanks for your patienceπŸ˜…

  17. LinasKo commented on Nov 12, 2024

    @LinasKo
    Contributor

    Thank you @AHB102!

    I'll have a look as soon as I can, likely next week.

  18. SkalskiP commented on Jan 8, 2025

    @SkalskiP
    Collaborator

    @stevensagaert, @Sapienscoding, @AyuK03, @AHB102 I'm curious about what real-life use cases you are working on where information about zone occupancy level is interesting?

  19. stevensagaert commented on Jan 8, 2025

    @stevensagaert
    Author
  20. AHB102 commented on Jan 8, 2025

    @AHB102

    @SkalskiP I'm currently working on determining the risk level associated with the overlap between two bounding boxes, with a focus on safety. I'm using Intersection over Union (IoU) to assess this, but a more nuanced approach could be valuable for various industrial applications. For instance, in autonomous vehicle systems, especially in warehouses, understanding the overlap between vehicle paths and obstacles can ensure safe navigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions