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

[formrecognizer] renames line appearance/style and adds to samples #16334

Merged
merged 3 commits into from
Jan 27, 2021
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
5 changes: 5 additions & 0 deletions sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -40,6 +33,8 @@
FieldValueType,
CustomFormModelProperties,
FormSelectionMark,
TextAppearance,
TextStyle
)
from ._api_versions import FormRecognizerApiVersion

Expand Down Expand Up @@ -73,8 +68,8 @@
'FieldValueType',
'CustomFormModelProperties',
'FormSelectionMark',
'Appearance',
'Style'
'TextAppearance',
'TextStyle'
]

__VERSION__ = VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth putting this description in line 417


: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)
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down