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
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
Are you willing to submit a PR?
Yes I'd like to help by submitting a PR!
The text was updated successfully, but these errors were encountered:
Search before asking
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:
strategy:
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: