MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / bbox_postprocess

Function bbox_postprocess

semantic_sam/modules/postprocessing.py:77–97  ·  view source on GitHub ↗

result: [xc,yc,w,h] range [0,1] to [x1,y1,x2,y2] range [0,w], [0,h]

(result, input_size, img_size, output_height, output_width)

Source from the content-addressed store, hash-verified

75 return results
76
77def bbox_postprocess(result, input_size, img_size, output_height, output_width):
78 """
79 result: [xc,yc,w,h] range [0,1] to [x1,y1,x2,y2] range [0,w], [0,h]
80 """
81 if result is None:
82 return None
83
84 scale = torch.tensor([input_size[1], input_size[0], input_size[1], input_size[0]])[None,:].to(result.device)
85 result = result.sigmoid() * scale
86 x1,y1,x2,y2 = result[:,0] - result[:,2]/2, result[:,1] - result[:,3]/2, result[:,0] + result[:,2]/2, result[:,1] + result[:,3]/2
87 h,w = img_size
88
89 x1 = x1.clamp(min=0, max=w)
90 y1 = y1.clamp(min=0, max=h)
91 x2 = x2.clamp(min=0, max=w)
92 y2 = y2.clamp(min=0, max=h)
93
94 box = torch.stack([x1,y1,x2,y2]).permute(1,0)
95 scale = torch.tensor([output_width/w, output_height/h, output_width/w, output_height/h])[None,:].to(result.device)
96 box = box*scale
97 return box
98
99def sem_seg_postprocess(result, img_size, output_height, output_width):
100 """

Callers

nothing calls this directly

Calls 1

toMethod · 0.80

Tested by

no test coverage detected