MCPcopy Index your code
hub / github.com/PDFMathTranslate/PDFMathTranslate / scale_boxes

Method scale_boxes

pdf2zh/doclayout.py:132–156  ·  view source on GitHub ↗

Rescales bounding boxes (in the format of xyxy by default) from the shape of the image they were originally specified in (img1_shape) to the shape of a different image (img0_shape). Args: img1_shape (tuple): The shape of the image that the bounding boxes are for

(self, img1_shape, boxes, img0_shape)

Source from the content-addressed store, hash-verified

130 return image
131
132 def scale_boxes(self, img1_shape, boxes, img0_shape):
133 """
134 Rescales bounding boxes (in the format of xyxy by default) from the shape of the image they were originally
135 specified in (img1_shape) to the shape of a different image (img0_shape).
136
137 Args:
138 img1_shape (tuple): The shape of the image that the bounding boxes are for,
139 in the format of (height, width).
140 boxes (torch.Tensor): the bounding boxes of the objects in the image, in the format of (x1, y1, x2, y2)
141 img0_shape (tuple): the shape of the target image, in the format of (height, width).
142
143 Returns:
144 boxes (torch.Tensor): The scaled bounding boxes, in the format of (x1, y1, x2, y2)
145 """
146
147 # Calculate scaling ratio
148 gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])
149
150 # Calculate padding size
151 pad_x = round((img1_shape[1] - img0_shape[1] * gain) / 2 - 0.1)
152 pad_y = round((img1_shape[0] - img0_shape[0] * gain) / 2 - 0.1)
153
154 # Remove padding and scale boxes
155 boxes[..., :4] = (boxes[..., :4] - [pad_x, pad_y, pad_x, pad_y]) / gain
156 return boxes
157
158 def predict(self, image, imgsz=1024, **kwargs):
159 # Preprocess input image

Callers 2

predictMethod · 0.95
test_scale_boxesMethod · 0.80

Calls

no outgoing calls

Tested by 1

test_scale_boxesMethod · 0.64