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

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

Open
2 tasks done
stevensagaert opened this issue Aug 14, 2024 · 15 comments
Open
2 tasks done

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

stevensagaert opened this issue Aug 14, 2024 · 15 comments
Assignees
Labels
enhancement New feature or request

Comments

@stevensagaert
Copy link

stevensagaert commented Aug 14, 2024

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!
@stevensagaert stevensagaert added the enhancement New feature or request label Aug 14, 2024
@Sapienscoding
Copy link

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

@LinasKo
Copy link
Collaborator

LinasKo commented Oct 15, 2024

Hi @Sapienscoding,

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

@LinasKo
Copy link
Collaborator

LinasKo commented Oct 15, 2024

Hi @stevensagaert 👋

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

@AyuK03
Copy link

AyuK03 commented Oct 16, 2024

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!

@stevensagaert
Copy link
Author

stevensagaert commented Oct 16, 2024 via email

@AHB102
Copy link

AHB102 commented Oct 29, 2024

@stevensagaert Can I resolve this issue ?

@stevensagaert
Copy link
Author

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

@AHB102
Copy link

AHB102 commented Oct 30, 2024

@stevensagaert I would like to try to implement it.

@stevensagaert
Copy link
Author

stevensagaert commented Oct 30, 2024 via email

@LinasKo
Copy link
Collaborator

LinasKo commented Oct 30, 2024

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.

@AHB102
Copy link

AHB102 commented Oct 30, 2024

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

@AHB102
Copy link

AHB102 commented Nov 1, 2024

@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)?

@LinasKo
Copy link
Collaborator

LinasKo commented Nov 1, 2024

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 😉

@AHB102
Copy link

AHB102 commented Nov 1, 2024

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

@LinasKo
Copy link
Collaborator

LinasKo commented Nov 1, 2024

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).

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

No branches or pull requests

5 participants