Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions mindee/geometry/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ class Point(NamedTuple):
y: float
"""Y coordinate"""

def __str__(self) -> str:
return f"({self.x},{self.y})"


Points = Sequence[Point]
3 changes: 3 additions & 0 deletions mindee/geometry/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def is_point_in_y(self, point: Point) -> bool:
min_y, max_y = get_min_max_y(self)
return is_point_in_y(point, min_y, max_y)

def __str__(self):
return "(" + ", ".join(str(p) for p in self) + ")"


def is_point_in_polygon_x(point: Point, polygon: Polygon) -> bool:
"""
Expand Down
3 changes: 2 additions & 1 deletion mindee/parsing/v2/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def __init__(self, raw_response: StringDict):
def __str__(self) -> str:
return (
f"Inference\n#########"
f"\n{self.model}"
f"\n{self.job}"
f"\n\n{self.model}"
f"\n\n{self.file}"
f"\n\n{self.active_options}"
f"\n\n{self.result}\n"
Expand Down
4 changes: 4 additions & 0 deletions mindee/v2/parsing/inference/base_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
from mindee.parsing.common.string_dict import StringDict
from mindee.parsing.v2.inference_file import InferenceFile
from mindee.parsing.v2.inference_model import InferenceModel
from mindee.v2.parsing.inference.inference_job import InferenceJob


class BaseInference(ABC):
"""Base class for V2 inference objects."""

job: InferenceJob
"""Job the inference belongs to."""
model: InferenceModel
"""Model info for the inference."""
file: InferenceFile
Expand All @@ -18,6 +21,7 @@ class BaseInference(ABC):

def __init__(self, raw_response: StringDict):
self.id = raw_response["id"]
self.job = InferenceJob(raw_response["job"])
self.model = InferenceModel(raw_response["model"])
self.file = InferenceFile(raw_response["file"])

Expand Down
14 changes: 14 additions & 0 deletions mindee/v2/parsing/inference/inference_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from mindee.parsing.common.string_dict import StringDict


class InferenceJob:
"""Inference Job info."""

id: str
"""UUID of the Job."""

def __init__(self, raw_response: StringDict) -> None:
self.id = raw_response["id"]

def __str__(self) -> str:
return f"Job\n===\n:ID: {self.id}"
8 changes: 7 additions & 1 deletion mindee/v2/product/classification/classification_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ def __init__(self, raw_response: StringDict) -> None:
self.result = ClassificationResult(raw_response["result"])

def __str__(self) -> str:
return f"Inference\n#########\n{self.model}\n{self.file}\n{self.result}\n"
return (
f"Inference\n#########"
f"\n{self.job}"
f"\n\n{self.model}"
f"\n\n{self.file}"
f"\n\n{self.result}\n"
)
8 changes: 7 additions & 1 deletion mindee/v2/product/crop/crop_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ def __init__(self, raw_response: StringDict) -> None:
self.result = CropResult(raw_response["result"])

def __str__(self) -> str:
return f"Inference\n#########\n{self.model}\n{self.file}\n{self.result}\n"
return (
f"Inference\n#########"
f"\n{self.job}"
f"\n\n{self.model}"
f"\n\n{self.file}"
f"\n\n{self.result}\n"
)
4 changes: 2 additions & 2 deletions mindee/v2/product/crop/crop_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def __init__(self, raw_response: StringDict) -> None:
def __str__(self) -> str:
crops = "\n"
if len(self.crops) > 0:
crops += "\n\n".join([str(crop) for crop in self.crops])
out_str = f"Crops\n======{crops}"
crops += "\n".join([str(crop) for crop in self.crops])
out_str = f"Crops\n====={crops}"
return out_str
8 changes: 7 additions & 1 deletion mindee/v2/product/ocr/ocr_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ def __init__(self, raw_response: StringDict) -> None:
self.result = OCRResult(raw_response["result"])

def __str__(self) -> str:
return f"Inference\n#########\n{self.model}\n{self.file}\n{self.result}\n"
return (
f"Inference\n#########"
f"\n{self.job}"
f"\n\n{self.model}"
f"\n\n{self.file}"
f"\n\n{self.result}\n"
)
8 changes: 7 additions & 1 deletion mindee/v2/product/split/split_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ def __init__(self, raw_response: StringDict) -> None:
self.result = SplitResult(raw_response["result"])

def __str__(self) -> str:
return f"Inference\n#########\n{self.model}\n{self.file}\n{self.result}\n"
return (
f"Inference\n#########"
f"\n{self.job}"
f"\n\n{self.model}"
f"\n\n{self.file}"
f"\n\n{self.result}\n"
)
22 changes: 8 additions & 14 deletions tests/v2/product/classification/test_classification_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from mindee import LocalResponse
from mindee.v2.product.classification.classification_classifier import (
ClassificationClassifier,
)
Expand All @@ -9,24 +8,19 @@
ClassificationResponse,
)
from mindee.v2.product.classification.classification_result import ClassificationResult
from tests.utils import V2_PRODUCT_DATA_DIR
from tests.v2.product.utils import get_product_samples


@pytest.mark.v2
def test_classification_single():
input_inference = LocalResponse(
V2_PRODUCT_DATA_DIR / "classification" / "classification_single.json"
json_sample, _ = get_product_samples(
product="classification", file_name="classification_single"
)
classification_response = input_inference.deserialize_response(
ClassificationResponse
)
assert isinstance(classification_response.inference, ClassificationInference)
assert isinstance(classification_response.inference.result, ClassificationResult)
response = ClassificationResponse(json_sample)
assert isinstance(response.inference, ClassificationInference)
assert isinstance(response.inference.result, ClassificationResult)
assert isinstance(
classification_response.inference.result.classification,
response.inference.result.classification,
ClassificationClassifier,
)
assert (
classification_response.inference.result.classification.document_type
== "invoice"
)
assert response.inference.result.classification.document_type == "invoice"
102 changes: 55 additions & 47 deletions tests/v2/product/crop/test_crop_response.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
import pytest

from mindee import LocalResponse
from mindee.v2.product.crop.crop_box import CropBox
from mindee.v2.product.crop import CropInference
from mindee.v2.product.crop.crop_response import CropResponse
from mindee.v2.product.crop.crop_result import CropResult
from tests.utils import V2_PRODUCT_DATA_DIR

from tests.v2.product.utils import get_product_samples


@pytest.mark.v2
def test_crop_single():
input_inference = LocalResponse(V2_PRODUCT_DATA_DIR / "crop" / "crop_single.json")
crop_response = input_inference.deserialize_response(CropResponse)
assert isinstance(crop_response.inference, CropInference)
assert crop_response.inference.result.crops
assert len(crop_response.inference.result.crops[0].location.polygon) == 4
assert crop_response.inference.result.crops[0].location.polygon[0][0] == 0.15
assert crop_response.inference.result.crops[0].location.polygon[0][1] == 0.254
assert crop_response.inference.result.crops[0].location.polygon[1][0] == 0.85
assert crop_response.inference.result.crops[0].location.polygon[1][1] == 0.254
assert crop_response.inference.result.crops[0].location.polygon[2][0] == 0.85
assert crop_response.inference.result.crops[0].location.polygon[2][1] == 0.947
assert crop_response.inference.result.crops[0].location.polygon[3][0] == 0.15
assert crop_response.inference.result.crops[0].location.polygon[3][1] == 0.947
assert crop_response.inference.result.crops[0].location.page == 0
assert crop_response.inference.result.crops[0].object_type == "invoice"
json_sample, rst_sample = get_product_samples(
product="crop", file_name="crop_single"
)
response = CropResponse(json_sample)
assert isinstance(response.inference, CropInference)
assert response.inference.result.crops
assert len(response.inference.result.crops[0].location.polygon) == 4
assert response.inference.result.crops[0].location.polygon[0][0] == 0.15
assert response.inference.result.crops[0].location.polygon[0][1] == 0.254
assert response.inference.result.crops[0].location.polygon[1][0] == 0.85
assert response.inference.result.crops[0].location.polygon[1][1] == 0.254
assert response.inference.result.crops[0].location.polygon[2][0] == 0.85
assert response.inference.result.crops[0].location.polygon[2][1] == 0.947
assert response.inference.result.crops[0].location.polygon[3][0] == 0.15
assert response.inference.result.crops[0].location.polygon[3][1] == 0.947
assert response.inference.result.crops[0].location.page == 0
assert response.inference.result.crops[0].object_type == "invoice"

assert rst_sample == str(response)


@pytest.mark.v2
def test_crop_multiple():
input_inference = LocalResponse(V2_PRODUCT_DATA_DIR / "crop" / "crop_multiple.json")
crop_response = input_inference.deserialize_response(CropResponse)
assert isinstance(crop_response.inference, CropInference)
assert isinstance(crop_response.inference.result, CropResult)
assert isinstance(crop_response.inference.result.crops[0], CropBox)
assert len(crop_response.inference.result.crops) == 2

assert len(crop_response.inference.result.crops[0].location.polygon) == 4
assert crop_response.inference.result.crops[0].location.polygon[0][0] == 0.214
assert crop_response.inference.result.crops[0].location.polygon[0][1] == 0.079
assert crop_response.inference.result.crops[0].location.polygon[1][0] == 0.476
assert crop_response.inference.result.crops[0].location.polygon[1][1] == 0.079
assert crop_response.inference.result.crops[0].location.polygon[2][0] == 0.476
assert crop_response.inference.result.crops[0].location.polygon[2][1] == 0.979
assert crop_response.inference.result.crops[0].location.polygon[3][0] == 0.214
assert crop_response.inference.result.crops[0].location.polygon[3][1] == 0.979
assert crop_response.inference.result.crops[0].location.page == 0
assert crop_response.inference.result.crops[0].object_type == "invoice"

assert len(crop_response.inference.result.crops[1].location.polygon) == 4
assert crop_response.inference.result.crops[1].location.polygon[0][0] == 0.547
assert crop_response.inference.result.crops[1].location.polygon[0][1] == 0.15
assert crop_response.inference.result.crops[1].location.polygon[1][0] == 0.862
assert crop_response.inference.result.crops[1].location.polygon[1][1] == 0.15
assert crop_response.inference.result.crops[1].location.polygon[2][0] == 0.862
assert crop_response.inference.result.crops[1].location.polygon[2][1] == 0.97
assert crop_response.inference.result.crops[1].location.polygon[3][0] == 0.547
assert crop_response.inference.result.crops[1].location.polygon[3][1] == 0.97
assert crop_response.inference.result.crops[1].location.page == 0
assert crop_response.inference.result.crops[1].object_type == "invoice"
json_sample, rst_sample = get_product_samples(
product="crop", file_name="crop_multiple"
)
response = CropResponse(json_sample)
assert isinstance(response.inference, CropInference)
assert isinstance(response.inference.result, CropResult)
assert isinstance(response.inference.result.crops[0], CropBox)
assert len(response.inference.result.crops) == 2

assert len(response.inference.result.crops[0].location.polygon) == 4
assert response.inference.result.crops[0].location.polygon[0][0] == 0.214
assert response.inference.result.crops[0].location.polygon[0][1] == 0.079
assert response.inference.result.crops[0].location.polygon[1][0] == 0.476
assert response.inference.result.crops[0].location.polygon[1][1] == 0.079
assert response.inference.result.crops[0].location.polygon[2][0] == 0.476
assert response.inference.result.crops[0].location.polygon[2][1] == 0.979
assert response.inference.result.crops[0].location.polygon[3][0] == 0.214
assert response.inference.result.crops[0].location.polygon[3][1] == 0.979
assert response.inference.result.crops[0].location.page == 0
assert response.inference.result.crops[0].object_type == "invoice"

assert len(response.inference.result.crops[1].location.polygon) == 4
assert response.inference.result.crops[1].location.polygon[0][0] == 0.547
assert response.inference.result.crops[1].location.polygon[0][1] == 0.15
assert response.inference.result.crops[1].location.polygon[1][0] == 0.862
assert response.inference.result.crops[1].location.polygon[1][1] == 0.15
assert response.inference.result.crops[1].location.polygon[2][0] == 0.862
assert response.inference.result.crops[1].location.polygon[2][1] == 0.97
assert response.inference.result.crops[1].location.polygon[3][0] == 0.547
assert response.inference.result.crops[1].location.polygon[3][1] == 0.97
assert response.inference.result.crops[1].location.page == 0
assert response.inference.result.crops[1].object_type == "invoice"

assert rst_sample == str(response)
Loading