Skip to content

Commit 99025df

Browse files
Bounding Box from checkpoint name (#191)
* add from_checkpoint_name constructor * format
1 parent e25ed94 commit 99025df

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

wkcuber/api/bounding_box.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# mypy: allow-untyped-defs
22
import json
3+
import re
34
from typing import Dict, Generator, Iterable, List, Optional, Tuple, Union, NamedTuple
45

56
import numpy as np
7+
68
from wkcuber.mag import Mag
79

810
Shape3D = Union[List[int], Tuple[int, int, int], np.ndarray]
@@ -59,6 +61,20 @@ def from_named_tuple(bb_named_tuple: BoundingBoxNamedTuple):
5961

6062
return BoundingBox(bb_named_tuple.topleft, bb_named_tuple.size)
6163

64+
@staticmethod
65+
def from_checkpoint_name(checkpoint_name: str) -> "BoundingBox":
66+
""" This function extracts a bounding box in the format x_y_z_sx_sy_xz which is contained in a string.
67+
"""
68+
regex = r"(([0-9]+_){5}([0-9]+))"
69+
match = re.search(regex, checkpoint_name)
70+
assert (
71+
match is not None
72+
), f"Could not extract bounding box from {checkpoint_name}"
73+
bbox_tuple = tuple(int(value) for value in match.group().split("_"))
74+
topleft = bbox_tuple[:3]
75+
size = bbox_tuple[3:6]
76+
return BoundingBox.from_tuple2((topleft, size))
77+
6278
@staticmethod
6379
def from_auto(obj) -> "BoundingBox":
6480
if isinstance(obj, BoundingBox):

0 commit comments

Comments
 (0)