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

OCR tutorial using Forte Ontologies to store data #877

Merged
merged 36 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
869ac91
ocr notebook
hepengfe Jul 12, 2022
c207c3c
ocr included in sphinx
hepengfe Jul 13, 2022
fc357b7
change box api
hepengfe Jul 14, 2022
e66c561
pylint
hepengfe Jul 14, 2022
9d16f85
adapt bounding box inheritance of box
hepengfe Jul 14, 2022
fa402e9
change bounding box init method as well
hepengfe Jul 14, 2022
399375e
rewrite bounding box init method and make it clean
hepengfe Jul 14, 2022
d9f5526
add compute_iou method
hepengfe Jul 14, 2022
5971292
remove old compute_iou method
hepengfe Jul 14, 2022
708b004
correct return typing
hepengfe Jul 14, 2022
0d97747
Merge branch 'box_init' into ocr_showcase_3
hepengfe Jul 14, 2022
39bf42a
rewrite ImageAnnotation.image and image shape functions
hepengfe Jul 14, 2022
7e03a7d
add character dataclass
hepengfe Jul 14, 2022
f2ff9bb
edited ocr notebook
hepengfe Jul 14, 2022
35c1ca5
add temporary pylint disable for ImageAnnotation height and width att…
hepengfe Jul 14, 2022
02154ab
remove unused code
hepengfe Jul 14, 2022
c10630f
disable pylint check on attributes
hepengfe Jul 14, 2022
e3a61c1
correct docstring and simplify box_min/max_x/y logitc
hepengfe Jul 15, 2022
e1c8393
add docstring for BoundingBox.init_from_center_n_shape
hepengfe Jul 15, 2022
0bec826
Merge branch 'box_init' into ocr_showcase_3
hepengfe Jul 15, 2022
2d13b0b
made changes based on the code review
hepengfe Jul 15, 2022
b6ce8a1
Merge branch 'box_init' into ocr_showcase_3
hepengfe Jul 18, 2022
978106e
add processor for token and recognize all bounding boxes for a single…
hepengfe Jul 18, 2022
dbac2f2
Merge branch 'master' into ocr_showcase_3
hepengfe Jul 18, 2022
302ba9d
Merge branch 'master' into box_init
hepengfe Jul 18, 2022
8580c16
add spelling: tokenizes
hepengfe Jul 18, 2022
8ab7562
remove useless code
hepengfe Jul 18, 2022
f041f23
remove bounding box
hepengfe Jul 18, 2022
310360e
BoundingBox -> Box for all files
hepengfe Jul 19, 2022
0c9e9d9
remove duplicated import
hepengfe Jul 19, 2022
efe4a78
improve box docstring
hepengfe Jul 19, 2022
6316d8a
Merge branch 'master' into box_init
hepengfe Jul 19, 2022
758d833
merge from box_init
hepengfe Jul 19, 2022
4208693
Merge branch 'master' into ocr_showcase_3
mylibrar Jul 19, 2022
1846960
remove black format in ft/onto/base_ontology.py
hepengfe Jul 21, 2022
ad469d6
add docstring and make minor edit based on code review
hepengfe Jul 21, 2022
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
1 change: 1 addition & 0 deletions docs/ch9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Chapter 9. Tasks on Other modalities
toc/audio_processing.md

toc/image_processing.md
notebook_tutorial/ocr.ipynb
440 changes: 440 additions & 0 deletions docs/notebook_tutorial/ocr.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ GroupType
PathLike
ElementType
customizable
tokenizes
27 changes: 24 additions & 3 deletions forte/data/ontology/top.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,15 +878,36 @@ def image(self):
"Cannot get image because image annotation is not "
"attached to any data pack."
)
return self.pack.get_image_array(self._image_payload_idx)
return self.pack.get_payload_data_at(
Modality.Image, self._image_payload_idx
)

@property
def max_x(self):
return self.image.shape[1] - 1
return self._image_width - 1

@property
def max_y(self):
return self.image.shape[0] - 1
return self._image_height - 1

@property
def image_shape(self):
return (self._image_height, self._image_width)

def set_image_shape(self, width, height):
mylibrar marked this conversation as resolved.
Show resolved Hide resolved
"""
This function is used to set the shape of the image.

Args:
width: the width of the image. The unit is pixel.
height: the height of the image. The unit is pixel.
"""
self._image_width = ( # pylint: disable=attribute-defined-outside-init
width
)
self._image_height = ( # pylint: disable=attribute-defined-outside-init
height
)

def __eq__(self, other):
if other is None:
Expand Down
5 changes: 5 additions & 0 deletions forte/ontology_specs/base_ontology.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"name": "base_ontology",
"definitions": [
{
"entry_name": "ft.onto.base_ontology.Character",
"parent_entry": "forte.data.ontology.top.Annotation",
"description": "A span based annotation :class:`Character`, used to represent a character."
},
{
"entry_name": "ft.onto.base_ontology.Token",
"parent_entry": "forte.data.ontology.top.Annotation",
Expand Down
87 changes: 70 additions & 17 deletions ft/onto/base_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from typing import Optional

__all__ = [
"Character",
"Token",
"Subword",
"Classification",
Expand Down Expand Up @@ -60,6 +61,16 @@
]


@dataclass
class Character(Annotation):
"""
A span based annotation :class:`Character`, used to represent a character.
"""

def __init__(self, pack: DataPack, begin: int, end: int):
super().__init__(pack, begin, end)


@dataclass
class Token(Annotation):
"""
Expand Down Expand Up @@ -311,7 +322,12 @@ class PredicateLink(Link):
ParentType = PredicateMention
ChildType = PredicateArgument

def __init__(self, pack: DataPack, parent: Optional[Entry] = None, child: Optional[Entry] = None):
mylibrar marked this conversation as resolved.
Show resolved Hide resolved
def __init__(
self,
pack: DataPack,
parent: Optional[Entry] = None,
child: Optional[Entry] = None,
):
super().__init__(pack, parent, child)
self.arg_type: Optional[str] = None

Expand All @@ -331,7 +347,12 @@ class Dependency(Link):
ParentType = Token
ChildType = Token

def __init__(self, pack: DataPack, parent: Optional[Entry] = None, child: Optional[Entry] = None):
def __init__(
self,
pack: DataPack,
parent: Optional[Entry] = None,
child: Optional[Entry] = None,
):
super().__init__(pack, parent, child)
self.dep_label: Optional[str] = None
self.rel_type: Optional[str] = None
Expand All @@ -340,7 +361,7 @@ def __init__(self, pack: DataPack, parent: Optional[Entry] = None, child: Option
@dataclass
class EnhancedDependency(Link):
"""
A `Link` type entry which represent a enhanced dependency:
A `Link` type entry which represent a enhanced dependency:
https://universaldependencies.org/u/overview/enhanced-syntax.html
Attributes:
dep_label (Optional[str]): The enhanced dependency label in Universal Dependency.
Expand All @@ -351,7 +372,12 @@ class EnhancedDependency(Link):
ParentType = Token
ChildType = Token

def __init__(self, pack: DataPack, parent: Optional[Entry] = None, child: Optional[Entry] = None):
def __init__(
self,
pack: DataPack,
parent: Optional[Entry] = None,
child: Optional[Entry] = None,
):
super().__init__(pack, parent, child)
self.dep_label: Optional[str] = None

Expand All @@ -369,7 +395,12 @@ class RelationLink(Link):
ParentType = EntityMention
ChildType = EntityMention

def __init__(self, pack: DataPack, parent: Optional[Entry] = None, child: Optional[Entry] = None):
def __init__(
self,
pack: DataPack,
parent: Optional[Entry] = None,
child: Optional[Entry] = None,
):
super().__init__(pack, parent, child)
self.rel_type: Optional[str] = None

Expand All @@ -387,7 +418,12 @@ class CrossDocEntityRelation(MultiPackLink):
ParentType = EntityMention
ChildType = EntityMention

def __init__(self, pack: MultiPack, parent: Optional[Entry] = None, child: Optional[Entry] = None):
def __init__(
self,
pack: MultiPack,
parent: Optional[Entry] = None,
child: Optional[Entry] = None,
):
super().__init__(pack, parent, child)
self.rel_type: Optional[str] = None

Expand All @@ -400,7 +436,9 @@ class CoreferenceGroup(Group):

MemberType = EntityMention

def __init__(self, pack: DataPack, members: Optional[Iterable[Entry]] = None):
def __init__(
self, pack: DataPack, members: Optional[Iterable[Entry]] = None
):
super().__init__(pack, members)


Expand All @@ -417,7 +455,12 @@ class EventRelation(Link):
ParentType = EventMention
ChildType = EventMention

def __init__(self, pack: DataPack, parent: Optional[Entry] = None, child: Optional[Entry] = None):
def __init__(
self,
pack: DataPack,
parent: Optional[Entry] = None,
child: Optional[Entry] = None,
):
super().__init__(pack, parent, child)
self.rel_type: Optional[str] = None

Expand All @@ -435,7 +478,12 @@ class CrossDocEventRelation(MultiPackLink):
ParentType = EventMention
ChildType = EventMention

def __init__(self, pack: MultiPack, parent: Optional[Entry] = None, child: Optional[Entry] = None):
def __init__(
self,
pack: MultiPack,
parent: Optional[Entry] = None,
child: Optional[Entry] = None,
):
super().__init__(pack, parent, child)
self.rel_type: Optional[str] = None

Expand All @@ -457,17 +505,17 @@ class ConstituentNode(Annotation):
sentiment: Dict[str, float]
is_root: Optional[bool]
is_leaf: Optional[bool]
parent_node: Optional['ConstituentNode']
children_nodes: FList['ConstituentNode']
parent_node: Optional["ConstituentNode"]
children_nodes: FList["ConstituentNode"]

def __init__(self, pack: DataPack, begin: int, end: int):
super().__init__(pack, begin, end)
self.label: Optional[str] = None
self.sentiment: Dict[str, float] = dict()
self.is_root: Optional[bool] = None
self.is_leaf: Optional[bool] = None
self.parent_node: Optional['ConstituentNode'] = None
self.children_nodes: FList['ConstituentNode'] = FList(self)
self.parent_node: Optional["ConstituentNode"] = None
self.children_nodes: FList["ConstituentNode"] = FList(self)


@dataclass
Expand All @@ -492,7 +540,6 @@ def __init__(self, pack: DataPack, begin: int, end: int):

@dataclass
class MCOption(Annotation):

def __init__(self, pack: DataPack, begin: int, end: int):
super().__init__(pack, begin, end)

Expand Down Expand Up @@ -572,7 +619,9 @@ class AudioPayload(Payload):

sample_rate: Optional[int]

def __init__(self, pack: DataPack, payload_idx: int = 0, uri: Optional[str] = None):
def __init__(
self, pack: DataPack, payload_idx: int = 0, uri: Optional[str] = None
):
super().__init__(pack, payload_idx, uri)
self.sample_rate: Optional[int] = None

Expand All @@ -583,7 +632,9 @@ class TextPayload(Payload):
A payload that caches text data
"""

def __init__(self, pack: DataPack, payload_idx: int = 0, uri: Optional[str] = None):
def __init__(
self, pack: DataPack, payload_idx: int = 0, uri: Optional[str] = None
):
super().__init__(pack, payload_idx, uri)


Expand All @@ -593,5 +644,7 @@ class ImagePayload(Payload):
A payload that caches image data
"""

def __init__(self, pack: DataPack, payload_idx: int = 0, uri: Optional[str] = None):
def __init__(
self, pack: DataPack, payload_idx: int = 0, uri: Optional[str] = None
):
super().__init__(pack, payload_idx, uri)