From d42e67b52349dce329b34fce6766a6309cf4782a Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Mon, 25 Jan 2021 15:08:02 -0800 Subject: [PATCH 1/3] prefix appearance/style model with Text and expose models --- .../azure-ai-formrecognizer/CHANGELOG.md | 5 +++ .../azure/ai/formrecognizer/__init__.py | 13 ++---- .../azure/ai/formrecognizer/_models.py | 45 ++++++++++++++++++- .../tests/test_repr.py | 3 +- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index 1a796f1488d2..4f8d734c2995 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -2,6 +2,11 @@ ## 3.1.0b3 (Unreleased) +**Breaking Changes** + +- `Appearance` is renamed to `TextAppearance` +- `Style` is renamed to `TextStyle` + ## 3.1.0b2 (2021-01-12) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py index 8f1bfd92b3b4..401ced1fcd82 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py @@ -7,13 +7,6 @@ from ._version import VERSION from ._form_recognizer_client import FormRecognizerClient from ._form_training_client import FormTrainingClient - -from ._generated.v2_1_preview_2.models import ( - Appearance, - Style -) - - from ._models import ( FormElement, LengthUnit, @@ -40,6 +33,8 @@ FieldValueType, CustomFormModelProperties, FormSelectionMark, + TextAppearance, + TextStyle ) from ._api_versions import FormRecognizerApiVersion @@ -73,8 +68,8 @@ 'FieldValueType', 'CustomFormModelProperties', 'FormSelectionMark', - 'Appearance', - 'Style' + 'TextAppearance', + 'TextStyle' ] __VERSION__ = VERSION diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py index e03ae6521c0a..8761da56cb9e 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py @@ -428,7 +428,7 @@ def __init__(self, **kwargs): @classmethod def _from_generated(cls, line, page): - line_appearance = line.appearance if hasattr(line, "appearance") else None + line_appearance = TextAppearance._from_generated(line.appearance) if hasattr(line, "appearance") else None return cls( text=line.text, bounding_box=get_bounding_box(line), @@ -1056,3 +1056,46 @@ def _from_generated(cls, model_info): def __repr__(self): return "CustomFormModelProperties(is_composed_model={})".format(self.is_composed_model) + + +class TextAppearance(object): + """An object representing the appearance of the text line. + + :param style: An object representing the style of the text line. + :type style: ~azure.ai.formrecognizer.TextStyle + """ + + def __init__(self, **kwargs): + self.style = kwargs.get('style', None) + + @classmethod + def _from_generated(cls, appearance): + if appearance is None: + return appearance + return cls( + style=TextStyle( + name=appearance.style.name, + confidence=appearance.style.confidence + ) + ) + + def __repr__(self): + return "TextAppearance(style={})".format(repr(self.style)) + + +class TextStyle(object): + """An object representing the style of the text line. + + :param name: The text line style name. + Possible values include: "other", "handwriting". + :type name: str + :param confidence: The confidence of text line style. + :type confidence: float + """ + + def __init__(self, **kwargs): + self.name = kwargs.get('name', None) + self.confidence = kwargs.get('confidence', None) + + def __repr__(self): + return "TextStyle(name={}, confidence={})".format(self.name, self.confidence) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py index 36d05b155d4a..7bb2b0ec87e8 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py @@ -8,7 +8,6 @@ import pytest import datetime from azure.ai.formrecognizer import _models -from azure.ai.formrecognizer import Appearance, Style # All features return a tuple of the object and the repr of the obejct @@ -36,7 +35,7 @@ def form_word(bounding_box): @pytest.fixture def form_line(bounding_box, form_word): - appearance = Appearance(style=Style(name="other", confidence=1.0)) + appearance = _models.TextAppearance(style=_models.TextStyle(name="other", confidence=1.0)) model = _models.FormLine(text="Word Word", bounding_box=bounding_box[0], words=[form_word[0], form_word[0]], page_number=1, appearance=appearance) model_repr = "FormLine(text=Word Word, bounding_box={}, words=[{}, {}], page_number=1, kind=line, appearance={})".format(bounding_box[1], form_word[1], form_word[1], appearance)[:1024] assert repr(model) == model_repr From 2f08e8212c91216b0d52481a7c94acd499e14823 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Mon, 25 Jan 2021 15:23:21 -0800 Subject: [PATCH 2/3] add line appearance to sample with signature --- .../samples/async_samples/sample_recognize_content_async.py | 3 +++ .../samples/sample_recognize_content.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_content_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_content_async.py index 9a61f4d22383..438ccd0432a3 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_content_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_content_async.py @@ -81,6 +81,9 @@ async def recognize_content(self): line.text, format_bounding_box(line.bounding_box) )) + if line.appearance: + if line.appearance.style.name == "handwriting" and line.appearance.style.confidence > 0.8: + print("Text line '{}' is handwritten and might be a signature.".format(line.text)) for word in line.words: print("...Word '{}' has a confidence of {}".format(word.text, word.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_content.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_content.py index cc1cbdbcd68a..2cd8084db43a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_content.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_content.py @@ -76,6 +76,9 @@ def recognize_content(self): line.text, format_bounding_box(line.bounding_box) )) + if line.appearance: + if line.appearance.style.name == "handwriting" and line.appearance.style.confidence > 0.8: + print("Text line '{}' is handwritten and might be a signature.".format(line.text)) for word in line.words: print("...Word '{}' has a confidence of {}".format(word.text, word.confidence)) From 81762f73c243c5016f33ecd08ea42577ac9e90c6 Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Tue, 26 Jan 2021 11:34:53 -0800 Subject: [PATCH 3/3] feedback --- .../azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py index 8761da56cb9e..06596df766b2 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py @@ -414,7 +414,7 @@ class FormLine(FormElement): :ivar int page_number: The 1-based number of the page in which this content is present. :ivar str kind: For FormLine, this is "line". - :ivar appearance: Text appearance properties. + :ivar appearance: An object representing the appearance of the line. :vartype appearance: ~azure.ai.formrecognizer.Appearance .. versionadded:: v2.1-preview