You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PolygonZone: estimate how much of the zone is occupiedΒ #1449
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:
defpolygon_to_binary_mask(img,contour):
"""convert bounding polygon coord to binary mask"""# Create binary maskb_mask=np.zeros(img.shape[:2], np.uint8)
# Extract contour result#contour = c.masks.xy.pop()# Changing the typecontour=contour.astype(np.int32) #Note: This must be int32 and not unit8!!!# Reshapingcontour=contour.reshape(-1, 1, 2)
# Draw contour onto maskmask=cv2.drawContours(b_mask, [contour], -1, (1, 1, 1), cv2.FILLED)
returnmask
strategy:
make union of binary masks of detected objects (only the classes for consideration in zone)
make binary mask of the zone
do and of zone mask & object masks
sum the 1 pixels
defcalculate_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 unionunion_mask=np.bitwise_or.reduce(np.stack(masks),axis=0)
#for debuggingifshow_plot:
plt.title("union object mask")
plt.imshow(union_mask,cmap='gray')
plt.show()
#do the bitwise and between union_mask & zone_maskoverlap_mask=np.bitwise_and(union_mask,zone_mask)
print(f"overlap_mask shape {overlap_mask.shape}")
ifshow_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)
return100*overlap_size/zone_size
________________________________
From: LinasKo ***@***.***>
Sent: Tuesday, October 15, 2024 8:48 AM
To: roboflow/supervision ***@***.***>
Cc: Steven Sagaert ***@***.***>; Mention ***@***.***>
Subject: Re: [roboflow/supervision] PolygonZone: estimate how much of the zone is occupied (Issue #1449)
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
Hi @stevensagaert<https://github.com/stevensagaert> π
Apologies for the delay. If you're still keen, we'd be happy to include area occupation stats in our PolygonZone.
β
Reply to this email directly, view it on GitHub<#1449 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A7A5SMGF3R5NLUEQ43D6KW3Z3S3CRAVCNFSM6AAAAABMQRAQWKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMJTGAZTCOJZGU>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
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.
@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)?
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!
I think so, but I'll need to see the PR first.
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 π
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).
@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?
@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.
Activity
Sapienscoding commentedon Oct 15, 2024
Hi @stevensagaert, I'd like to help by submitting a PR
LinasKo commentedon Oct 15, 2024
Hi @Sapienscoding,
I've assigned another issue to you. Let's go with one at a time.
LinasKo commentedon 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 commentedon 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 commentedon Oct 16, 2024
AHB102 commentedon Oct 29, 2024
@stevensagaert Can I resolve this issue ?
stevensagaert commentedon Oct 30, 2024
You mean you want to implement it or just close it? I haven't had time to do it yet.
AHB102 commentedon Oct 30, 2024
@stevensagaert I would like to try to implement it.
stevensagaert commentedon Oct 30, 2024
LinasKo commentedon Oct 30, 2024
Hi @AHB102 π
Assigning this to you.
Note that Detection can take one of 3 forms:
maskfieldxyxyfield, and notmask.(N, 4, 2)-shaped polygon indetections.data[CLASS_NAME_DATA_FIELD]. (CLASS_NAME_DATA_FIELD=="xyxyxyxy"and resides inconfig.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_maskmethod helpful. it resides in:supervision/detection/utils.py.AHB102 commentedon Oct 30, 2024
@LinasKo Thanks for the insight π, will get started on this.
AHB102 commentedon 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 commentedon Nov 1, 2024
Hi @AHB102 π
: int,: str,: List[str]and so on. For numpy arrays it's fine to saynp.ndarray. Return type is-> int:and so on. You can do-> None:if it returns nothing. I'd encourage you to try!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 commentedon Nov 1, 2024
Okay nice! Will definitely make the type hinting work, and can I commit the changes and make a PR ?
LinasKo commentedon 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'sdevelop(there should be a button, as soon as you push to your own fork).AHB102 commentedon Nov 12, 2024
@LinasKo I got a bit lost in the first PR. I believe this one is correct. Thanks for your patienceπ
LinasKo commentedon Nov 12, 2024
Thank you @AHB102!
I'll have a look as soon as I can, likely next week.
SkalskiP commentedon Jan 8, 2025
@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?
stevensagaert commentedon Jan 8, 2025
AHB102 commentedon Jan 8, 2025
@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.