Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLDR-473 add bbox class from dedoc #13

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

v0.3.3 (2023-09-28)
-------------------
* Update BBox class
NastyBoget marked this conversation as resolved.
Show resolved Hide resolved

v0.3.2 (2023-09-25)
-------------------
* Add intervals to dependencies versions
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.2
0.3.3
52 changes: 42 additions & 10 deletions dedocutils/data_structures/bbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
@dataclass
class BBox:
NastyBoget marked this conversation as resolved.
Show resolved Hide resolved
"""
Box around some page object, the coordinate system starts from top left corner
Bounding box around some page object, the coordinate system starts from top left corner.
"""
"""

0------------------------------------------------------------------------------------------------> x
| BBox
Expand All @@ -21,10 +23,19 @@ class BBox:
|
V y
"""
x_top_left: int
y_top_left: int
width: int
height: int
def __init__(self, x_top_left: int, y_top_left: int, width: int, height: int) -> None:
"""
The following parameters should have values of pixels number.

:param x_top_left: x coordinate of the bbox top left corner
:param y_top_left: y coordinate of the bbox top left corner
:param width: bounding box width
:param height: bounding box height
"""
self.x_top_left = x_top_left
self.y_top_left = y_top_left
self.width = width
self.height = height

@property
def x_bottom_right(self) -> int:
Expand All @@ -34,24 +45,45 @@ def x_bottom_right(self) -> int:
def y_bottom_right(self) -> int:
return self.y_top_left + self.height

def __str__(self) -> str:
return f"BBox(x = {self.x_top_left} y = {self.y_top_left}, w = {self.width}, h = {self.height})"

def __repr__(self) -> str:
return self.__str__()

@property
def square(self) -> int:
"""
Square of the bbox.
"""
return self.height * self.width

@staticmethod
def from_two_points(top_left: Tuple[int, int], bottom_right: Tuple[int, int]) -> "BBox":
"""
Make the bounding box from two points.

:param top_left: (x, y) point of the bbox top left corner
:param bottom_right: (x, y) point of the bbox bottom right corner
"""
x_top_left, y_top_left = top_left
x_bottom_right, y_bottom_right = bottom_right
return BBox(x_top_left=x_top_left, y_top_left=y_top_left, width=x_bottom_right - x_top_left, height=y_bottom_right - y_top_left)

def have_intersection_with_box(self, box: "BBox", threshold: float = 0.3) -> bool:
"""
Check if the current bounding box has the intersection with another one.

:param box: another bounding box to check intersection with
:param threshold: the lowest value of the intersection over union used get boolean result
"""
# determine the (x, y)-coordinates of the intersection rectangle
x_min = max(self.x_top_left, box.x_top_left)
y_min = max(self.y_top_left, box.y_top_left)
x_max = min(self.x_top_left + self.width, box.x_top_left + box.width)
y_max = min(self.y_top_left + self.height, box.y_top_left + box.height)
x_a = max(self.x_top_left, box.x_top_left)
NastyBoget marked this conversation as resolved.
Show resolved Hide resolved
y_a = max(self.y_top_left, box.y_top_left)
x_b = min(self.x_top_left + self.width, box.x_top_left + box.width)
y_b = min(self.y_top_left + self.height, box.y_top_left + box.height)
# compute the area of intersection rectangle
inter_a_area = max(0, x_max - x_min) * max(0, y_max - y_min)
inter_a_area = max(0, x_b - x_a) * max(0, y_b - y_a)
# compute the area of both the prediction and ground-truth
# rectangles
box_b_area = float(box.width * box.height)
Expand Down
Loading