Bounding Box

Create a box by mentioning the top-left (x, y) and bottom-right (X, Y) coordinates
Say x, y, X, Y are 10, 20, 40, 50 respectively

bb = BB([10, 20, 40, 50])

You get the following attributes for free


bb.x=10              (top left - x)
bb.y=20              (top left - y)
bb.X=40              (bottom right - x)
bb.Y=50              (bottom right - y)
bb.w=30              (width)
bb.h=30              (height)
bb.xc=25.0           (center x)
bb.yc=35.0           (center y)
bb.c=(25.0, 35.0)    (center)
bb.area=900          (area)
bb.shape=(30, 30)    (height, width)
from torch_snippets import show, read, P, pd
assets = P("/Users/yeshwanth.y/code/torch_snippets/assets/")
im = read(assets / "Preamble.png")
df = pd.read_csv(assets / "Preamble.csv")
show(df.head())
df = to_absolute(df, *im.shape[:2])
show(df.head())
df = to_relative(df, *im.shape[:2])
show(df.head())
x y X Y text block_id
0 135 181 308 218 ConstITUtIO 0
1 156 264 217 284 NLTHE 1
2 218 264 276 284 PEOPLE 1
3 267 265 295 282 OF 1
4 297 264 341 284 INDIA, 1
x y X Y text block_id
0 135 181 308 218 ConstITUtIO 0
1 156 264 217 284 NLTHE 1
2 218 264 276 284 PEOPLE 1
3 267 265 295 282 OF 1
4 297 264 341 284 INDIA, 1
x y X Y text block_id
0 0.249538 0.253501 0.569316 0.305322 ConstITUtIO 0
1 0.288355 0.369748 0.401109 0.397759 NLTHE 1
2 0.402957 0.369748 0.510166 0.397759 PEOPLE 1
3 0.493530 0.371148 0.545287 0.394958 OF 1
4 0.548983 0.369748 0.630314 0.397759 INDIA, 1
show(im, df=df, sz=10)

show(im, df=to_absolute(df, *im.shape[:2]), sz=10)

_df = to_absolute(df, *im.shape[:2])
show(im, df=to_relative(_df, *im.shape[:2]), sz=10)

_df = combine_xyXY_to_bb(_df)
show(im, df=to_relative(_df, *im.shape[:2]), sz=10)

to_relative(_df, *im.shape[:2])
text block_id bb
0 ConstITUtIO 0 [0.24953789279112754, 0.2535014005602241, 0.56...
1 NLTHE 1 [0.28835489833641403, 0.3697478991596639, 0.40...
2 PEOPLE 1 [0.4029574861367837, 0.3697478991596639, 0.510...
3 OF 1 [0.49353049907578556, 0.3711484593837535, 0.54...
4 INDIA, 1 [0.5489833641404805, 0.3697478991596639, 0.630...
... ... ... ...
68 GIVE 13 [0.4011090573012939, 0.7366946778711485, 0.478...
69 TO 13 [0.4879852125693161, 0.7366946778711485, 0.536...
70 oUrSELVES 13 [0.5508317929759704, 0.7394957983193278, 0.706...
71 THIS 13 [0.7190388170055453, 0.7366946778711485, 0.783...
72 CONSTITUTION. 14 [0.23290203327171904, 0.7689075630252101, 0.44...

73 rows × 3 columns


isin

 isin (bboxes1, bboxes2, return_matrix=True)

return indexes of those boxes from bboxes1 that are completely inside bboxes2


merge_by_bb

 merge_by_bb (df1, df2, suffixes=('_x', '_y'), iou_threshold=0.1)

Merge df1 columns to df2 by using iou Make sure both df1 & df2 are relative or both absolute