From d8e1140df4f153dd882130a2756d66e704aa02b9 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Thu, 28 Sep 2023 11:44:50 +0300 Subject: [PATCH 1/9] add BBox class from dedoc --- dedocutils/data_structures/bbox.py | 31 +++++------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/dedocutils/data_structures/bbox.py b/dedocutils/data_structures/bbox.py index bf69090..853679f 100644 --- a/dedocutils/data_structures/bbox.py +++ b/dedocutils/data_structures/bbox.py @@ -1,10 +1,7 @@ -import math from collections import OrderedDict from dataclasses import dataclass from typing import Dict, Tuple -import numpy as np - @dataclass class BBox: @@ -48,24 +45,6 @@ def x_bottom_right(self) -> int: def y_bottom_right(self) -> int: return self.y_top_left + self.height - @staticmethod - def crop_image_by_box(image: np.ndarray, bbox: "BBox") -> np.ndarray: - return image[bbox.y_top_left:bbox.y_bottom_right, bbox.x_top_left:bbox.x_bottom_right] - - def rotate_coordinates(self, angle_rotate: float, image_shape: Tuple[int]) -> None: - xb, yb = self.x_top_left, self.y_top_left - xe, ye = self.x_bottom_right, self.y_bottom_right - rad = angle_rotate * math.pi / 180 - - xc = image_shape[1] / 2 - yc = image_shape[0] / 2 - - bbox_xb = min((int(float(xb - xc) * math.cos(rad) - float(yb - yc) * math.sin(rad) + xc)), image_shape[1]) - bbox_yb = min((int(float(yb - yc) * math.cos(rad) + float(xb - xc) * math.sin(rad) + yc)), image_shape[0]) - bbox_xe = min((int(float(xe - xc) * math.cos(rad) - float(ye - yc) * math.sin(rad) + xc)), image_shape[1]) - bbox_ye = min((int(float(ye - yc) * math.cos(rad) + float(xe - xc) * math.sin(rad) + yc)), image_shape[0]) - self.__init__(bbox_xb, bbox_yb, bbox_xe - bbox_xb, bbox_ye - bbox_yb) - def __str__(self) -> str: return f"BBox(x = {self.x_top_left} y = {self.y_top_left}, w = {self.width}, h = {self.height})" @@ -99,12 +78,12 @@ def have_intersection_with_box(self, box: "BBox", threshold: float = 0.3) -> boo :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) + 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) From b1f0f5ab9a7eb0cac76dd40e5a85c95181df8e14 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Thu, 28 Sep 2023 11:48:09 +0300 Subject: [PATCH 2/9] update version --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 899d4b1..e55e651 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Changelog v0.3.3 (2023-09-28) ------------------- -* Update `BBox` class +* Update BBox class v0.3.2 (2023-09-25) ------------------- From 02a3230036687c74020c888626773396bd35a389 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Thu, 28 Sep 2023 12:22:01 +0300 Subject: [PATCH 3/9] update imports and changelog --- CHANGELOG.md | 2 +- dedocutils/data_structures/bbox.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e55e651..899d4b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Changelog v0.3.3 (2023-09-28) ------------------- -* Update BBox class +* Update `BBox` class v0.3.2 (2023-09-25) ------------------- diff --git a/dedocutils/data_structures/bbox.py b/dedocutils/data_structures/bbox.py index 853679f..a45d3e7 100644 --- a/dedocutils/data_structures/bbox.py +++ b/dedocutils/data_structures/bbox.py @@ -1,7 +1,10 @@ +import math from collections import OrderedDict from dataclasses import dataclass from typing import Dict, Tuple +import numpy as np + @dataclass class BBox: @@ -45,6 +48,24 @@ def x_bottom_right(self) -> int: def y_bottom_right(self) -> int: return self.y_top_left + self.height + @staticmethod + def crop_image_by_box(image: np.ndarray, bbox: "BBox") -> np.ndarray: + return image[bbox.y_top_left:bbox.y_bottom_right, bbox.x_top_left:bbox.x_bottom_right] + + def rotate_coordinates(self, angle_rotate: float, image_shape: Tuple[int]) -> None: + xb, yb = self.x_top_left, self.y_top_left + xe, ye = self.x_bottom_right, self.y_bottom_right + rad = angle_rotate * math.pi / 180 + + xc = image_shape[1] / 2 + yc = image_shape[0] / 2 + + bbox_xb = min((int(float(xb - xc) * math.cos(rad) - float(yb - yc) * math.sin(rad) + xc)), image_shape[1]) + bbox_yb = min((int(float(yb - yc) * math.cos(rad) + float(xb - xc) * math.sin(rad) + yc)), image_shape[0]) + bbox_xe = min((int(float(xe - xc) * math.cos(rad) - float(ye - yc) * math.sin(rad) + xc)), image_shape[1]) + bbox_ye = min((int(float(ye - yc) * math.cos(rad) + float(xe - xc) * math.sin(rad) + yc)), image_shape[0]) + self.__init__(bbox_xb, bbox_yb, bbox_xe - bbox_xb, bbox_ye - bbox_yb) + def __str__(self) -> str: return f"BBox(x = {self.x_top_left} y = {self.y_top_left}, w = {self.width}, h = {self.height})" From 50e97d4630be79cc7dce71dff4f01fc264e4dd84 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Thu, 28 Sep 2023 12:26:16 +0300 Subject: [PATCH 4/9] change var names --- dedocutils/data_structures/bbox.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dedocutils/data_structures/bbox.py b/dedocutils/data_structures/bbox.py index a45d3e7..bf69090 100644 --- a/dedocutils/data_structures/bbox.py +++ b/dedocutils/data_structures/bbox.py @@ -99,12 +99,12 @@ def have_intersection_with_box(self, box: "BBox", threshold: float = 0.3) -> boo :param threshold: the lowest value of the intersection over union used get boolean result """ # determine the (x, y)-coordinates of the intersection rectangle - x_a = max(self.x_top_left, box.x_top_left) - 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) + 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) # compute the area of intersection rectangle - inter_a_area = max(0, x_b - x_a) * max(0, y_b - y_a) + inter_a_area = max(0, x_max - x_min) * max(0, y_max - y_min) # compute the area of both the prediction and ground-truth # rectangles box_b_area = float(box.width * box.height) From 5e6f06764fb48f8c9cc2d49b5e8bd1405478c1e9 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Thu, 28 Sep 2023 18:02:47 +0300 Subject: [PATCH 5/9] add serializable --- dedocutils/data_structures/bbox.py | 4 +++- dedocutils/data_structures/serializable.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 dedocutils/data_structures/serializable.py diff --git a/dedocutils/data_structures/bbox.py b/dedocutils/data_structures/bbox.py index bf69090..310de53 100644 --- a/dedocutils/data_structures/bbox.py +++ b/dedocutils/data_structures/bbox.py @@ -5,9 +5,11 @@ import numpy as np +from dedocutils.data_structures.serializable import Serializable + @dataclass -class BBox: +class BBox(Serializable): """ Bounding box around some page object, the coordinate system starts from top left corner. """ diff --git a/dedocutils/data_structures/serializable.py b/dedocutils/data_structures/serializable.py new file mode 100644 index 0000000..61e17f1 --- /dev/null +++ b/dedocutils/data_structures/serializable.py @@ -0,0 +1,16 @@ +from abc import ABC, abstractmethod + + +class Serializable(ABC): + """ + Base class for the serializable objects which we need convert to dict. + """ + @abstractmethod + def to_dict(self) -> dict: + """ + Convert class data into dictionary representation. + Dictionary key should be string and dictionary value should be json serializable. + + :return: dict with all class data. + """ + pass \ No newline at end of file From 4d3aa1f396d84efde01ac29db1f13e5e002b66c9 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Thu, 28 Sep 2023 18:14:40 +0300 Subject: [PATCH 6/9] change version --- CHANGELOG.md | 4 ++++ VERSION | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 899d4b1..a4699b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Changelog ========= +v0.3.4 (2023-09-28) +------------------- +* Make `BBox` class `Serializable` + v0.3.3 (2023-09-28) ------------------- * Update `BBox` class diff --git a/VERSION b/VERSION index 87a0871..448a0fa 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.3 \ No newline at end of file +0.3.4 \ No newline at end of file From 71fde9672a6639511271fe078058cd49eabd6813 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Fri, 29 Sep 2023 10:41:09 +0300 Subject: [PATCH 7/9] fix style --- dedocutils/data_structures/serializable.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dedocutils/data_structures/serializable.py b/dedocutils/data_structures/serializable.py index 61e17f1..f15626a 100644 --- a/dedocutils/data_structures/serializable.py +++ b/dedocutils/data_structures/serializable.py @@ -13,4 +13,5 @@ def to_dict(self) -> dict: :return: dict with all class data. """ - pass \ No newline at end of file + pass + \ No newline at end of file From 9b0bf862ce361c954547204a53fc888513923ed9 Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Fri, 29 Sep 2023 10:51:50 +0300 Subject: [PATCH 8/9] fix style --- dedocutils/data_structures/serializable.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dedocutils/data_structures/serializable.py b/dedocutils/data_structures/serializable.py index f15626a..9de5d23 100644 --- a/dedocutils/data_structures/serializable.py +++ b/dedocutils/data_structures/serializable.py @@ -14,4 +14,3 @@ def to_dict(self) -> dict: :return: dict with all class data. """ pass - \ No newline at end of file From ccf5a1e52ccb00944489864116d54a6931c44b9d Mon Sep 17 00:00:00 2001 From: Nikita Shevtsov Date: Fri, 29 Sep 2023 13:11:00 +0300 Subject: [PATCH 9/9] fix changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4699b7..fd0cd2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ Changelog v0.3.4 (2023-09-28) ------------------- -* Make `BBox` class `Serializable` +* Add `Serializable` class +* Make `BBox` child class of `Serializable` v0.3.3 (2023-09-28) -------------------