@@ -12,13 +12,22 @@ def _compute_box(mask, original_size=None):
1212 coords [1 ].min (), coords [0 ].min (),
1313 coords [1 ].max () + 1 , coords [0 ].max () + 1 ,
1414 ])
15- # FIXME how do we deal with aspect ratios???
15+ # TODO how do we deal with aspect ratios???
1616 if original_size is not None :
1717 trafo = ResizeLongestSide (max (original_size ))
1818 box = trafo .apply_boxes (box [None ], (256 , 256 )).squeeze ()
1919 return box
2020
2121
22+ def _process_box (box , original_size = None ):
23+ box_processed = box [[1 , 0 , 3 , 2 ]]
24+ # TODO how do we deal with aspect ratios???
25+ if original_size is not None :
26+ trafo = ResizeLongestSide (max (original_size ))
27+ box_processed = trafo .apply_boxes (box [None ], (256 , 256 )).squeeze ()
28+ return box_processed
29+
30+
2231def _compute_logits (mask , eps = 1e-3 ):
2332
2433 def inv_sigmoid (x ):
@@ -77,3 +86,17 @@ def segment_from_mask(
7786 return mask , scores , logits
7887 else :
7988 return mask
89+
90+
91+ def segment_from_box (
92+ predictor , box ,
93+ image_embeddings = None , i = None , original_size = None ,
94+ multimask_output = False , return_all = False ,
95+ ):
96+ if image_embeddings is not None :
97+ util .set_precomputed (predictor , image_embeddings , i )
98+ mask , scores , logits = predictor .predict (box = _process_box (box , original_size ), multimask_output = multimask_output )
99+ if return_all :
100+ return mask , scores , logits
101+ else :
102+ return mask
0 commit comments