@@ -956,6 +956,46 @@ def __call__(
956
956
}
957
957
958
958
959
+ class ObjectDistance (Tool ):
960
+ name = "object_distance_"
961
+ description = "'object_distance_' calculates the distance between two objects in an image. It returns the minimum distance between the two objects."
962
+ usage = {
963
+ "required_parameters" : [
964
+ {"name" : "object1" , "type" : "Dict[str, Any]" },
965
+ {"name" : "object2" , "type" : "Dict[str, Any]" },
966
+ ],
967
+ "examples" : [
968
+ {
969
+ "scenario" : "Calculate the distance between these two objects {bboxes: [0.2, 0.21, 0.34, 0.42], masks: 'mask_file1.png'}, {bboxes: [0.3, 0.31, 0.44, 0.52], masks: 'mask_file2.png'}" ,
970
+ "parameters" : {
971
+ "object1" : {
972
+ "bboxes" : [0.2 , 0.21 , 0.34 , 0.42 ],
973
+ "scores" : 0.54 ,
974
+ "masks" : "mask_file1.png" ,
975
+ },
976
+ "object2" : {
977
+ "bboxes" : [0.3 , 0.31 , 0.44 , 0.52 ],
978
+ "scores" : 0.66 ,
979
+ "masks" : "mask_file2.png" ,
980
+ },
981
+ },
982
+ }
983
+ ],
984
+ }
985
+
986
+ def __call__ (self , object1 : Dict [str , Any ], object2 : Dict [str , Any ]) -> float :
987
+ if "masks" in object1 and "masks" in object2 :
988
+ mask1 = object1 ["masks" ]
989
+ mask2 = object2 ["masks" ]
990
+ return MaskDistance ()(mask1 , mask2 )
991
+ elif "bboxes" in object1 and "bboxes" in object2 :
992
+ bbox1 = object1 ["bboxes" ]
993
+ bbox2 = object2 ["bboxes" ]
994
+ return BoxDistance ()(bbox1 , bbox2 )
995
+ else :
996
+ raise ValueError ("Either of the objects should have masks or bboxes" )
997
+
998
+
959
999
class BoxDistance (Tool ):
960
1000
name = "box_distance_"
961
1001
description = "'box_distance_' calculates distance between two bounding boxes. It returns the minumum distance between the given bounding boxes"
@@ -966,7 +1006,7 @@ class BoxDistance(Tool):
966
1006
],
967
1007
"examples" : [
968
1008
{
969
- "scenario" : "Calculate the distance between the bounding boxes [0.2, 0.21, 0.34, 0.42] and [0.3, 0.31, 0.44, 0.52]" ,
1009
+ "scenario" : "Calculate the distance between these two bounding boxes [0.2, 0.21, 0.34, 0.42] and [0.3, 0.31, 0.44, 0.52]" ,
970
1010
"parameters" : {
971
1011
"bbox1" : [0.2 , 0.21 , 0.34 , 0.42 ],
972
1012
"bbox2" : [0.3 , 0.31 , 0.44 , 0.52 ],
@@ -1006,6 +1046,7 @@ def __call__(self, mask1: Union[str, Path], mask2: Union[str, Path]) -> float:
1006
1046
pil_mask2 = Image .open (str (mask2 ))
1007
1047
np_mask1 = np .clip (np .array (pil_mask1 ), 0 , 1 )
1008
1048
np_mask2 = np .clip (np .array (pil_mask2 ), 0 , 1 )
1049
+
1009
1050
mask1_points = np .transpose (np .nonzero (np_mask1 ))
1010
1051
mask2_points = np .transpose (np .nonzero (np_mask2 ))
1011
1052
dist_matrix = distance .cdist (mask1_points , mask2_points , "euclidean" )
@@ -1146,10 +1187,9 @@ def __call__(self, equation: str) -> float:
1146
1187
Crop ,
1147
1188
BboxArea ,
1148
1189
SegArea ,
1149
- SegIoU ,
1150
- MaskDistance ,
1190
+ ObjectDistance ,
1151
1191
BboxContains ,
1152
- BoxDistance ,
1192
+ SegIoU ,
1153
1193
OCR ,
1154
1194
Calculator ,
1155
1195
]
0 commit comments