Spaces:
Runtime error
Runtime error
Create coord_utils.py
Browse files- drs/coord_utils.py +17 -0
drs/coord_utils.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# drs/coord_utils.py
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
|
| 5 |
+
# Rescale coords (xyxy) from img1_shape to img0_shape
|
| 6 |
+
if ratio_pad is None: # calculate from shapes
|
| 7 |
+
gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])
|
| 8 |
+
pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2
|
| 9 |
+
else:
|
| 10 |
+
gain = ratio_pad[0][0]
|
| 11 |
+
pad = ratio_pad[1]
|
| 12 |
+
|
| 13 |
+
coords[:, [0, 2]] -= pad[0] # x padding
|
| 14 |
+
coords[:, [1, 3]] -= pad[1] # y padding
|
| 15 |
+
coords[:, :4] /= gain
|
| 16 |
+
coords[:, :4] = coords[:, :4].clamp(min=0)
|
| 17 |
+
return coords
|