Skip to content

Commit

Permalink
split icparser & wildreceipt metafile fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xinke-wang committed Nov 15, 2022
1 parent fe9f844 commit d11757d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 65 deletions.
2 changes: 1 addition & 1 deletion dataset_zoo/wildreceipt/metafile.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Name: 'WildReceipt'
Paper:
Title: "Spatial Dual-Modality Graph Reasoning for Key Information Extraction"
URL: https://arxiv.org/pdf/2103.14470.pdf
URL: https://link.springer.com/article/10.1007/s10032-019-00334-z
Venue: arXiv
Year: '2021'
BibTeX: '@article{sun2021spatial,
Expand Down
11 changes: 1 addition & 10 deletions mmocr/datasets/preparers/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,12 @@ def parse_file(self, file: Tuple, split: str) -> Tuple:
split (str): Current split.
Returns:
Tuple: A tuple of (img_path, instance). Instance is a list of dict
Tuple: A tuple of (img_path, instance). Instance is a dict
containing parsed annotations, which should contain the
following keys:
- 'poly' or 'box' (textdet or textspotting)
- 'text' (textspotting or textrecog)
- 'ignore' (all task)
Examples:
An example of returned values:
>>> ('imgs/train/xxx.jpg',
>>> dict(
>>> poly=[[[0, 1], [1, 1], [1, 0], [0, 0]]],
>>> text='hello',
>>> ignore=False)
>>> )
"""
raise NotImplementedError

Expand Down
64 changes: 10 additions & 54 deletions mmocr/datasets/preparers/parsers/icdar_txt_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
from typing import List, Optional, Tuple

from ..data_preparer import DATA_PARSERS
Expand All @@ -8,7 +7,7 @@

@DATA_PARSERS.register_module()
class ICDARTxtTextDetAnnParser(BaseParser):
"""ICDAR Txt Format Text Detection Annotation Parser.
"""ICDAR2015 Text Detection Parser.
The original annotation format of this dataset is stored in txt files,
which is formed as the following format:
Expand All @@ -26,8 +25,6 @@ class ICDARTxtTextDetAnnParser(BaseParser):
to 1.
remove_strs (List[str], Optional): Used to remove redundant strings in
the transcription. Defaults to None.
mode (str, optional): The mode of the box converter. Supported modes
are 'xywh' and 'xyxy'. Defaults to None.
"""

def __init__(self,
Expand All @@ -36,13 +33,11 @@ def __init__(self,
format: str = 'x1,y1,x2,y2,x3,y3,x4,y4,trans',
encoding: str = 'utf-8-sig',
nproc: int = 1,
remove_strs: Optional[List[str]] = None,
mode: str = None) -> None:
remove_strs: Optional[List[str]] = None) -> None:
self.sep = separator
self.format = format
self.encoding = encoding
self.ignore = ignore
self.mode = mode
self.remove_strs = remove_strs
super().__init__(nproc=nproc)

Expand All @@ -54,44 +49,21 @@ def parse_file(self, file: Tuple, split: str) -> Tuple:
self.encoding):
anno = list(anno.values())
if self.remove_strs is not None:
for strs in self.remove_strs:
for flag in self.remove_strs:
for i in range(len(anno)):
if strs in anno[i]:
anno[i] = anno[i].replace(strs, '')
if flag in anno[i]:
anno[i] = anno[i].replace(flag, '')
poly = list(map(float, anno[0:-1]))
if self.mode is not None:
poly = self.convert_bbox(poly)
text = anno[-1]
instances.append(
dict(poly=poly, text=text, ignore=text == self.ignore))

return img_file, instances

def convert_bbox(self, poly: List) -> List:
"""Convert bbox format.
Args:
poly (List): The original bbox.
Returns:
List: The converted bbox.
"""
assert len(poly) == 4
if self.mode == 'xywh':
x, y, w, h = poly
poly = [x, y, x + w, y, x + w, y + h, x, y + h]
elif self.mode == 'xyxy':
x1, y1, x2, y2 = poly
poly = [x1, y1, x2, y1, x2, y2, x1, y2]
else:
raise NotImplementedError('Not supported mode.')

return poly


@DATA_PARSERS.register_module()
class ICDARTxtTextRecogAnnParser(BaseParser):
"""ICDAR Txt Format Text Recognition Annotation Parser.
"""ICDAR2015 Text Detection Parser.
The original annotation format of this dataset is stored in txt files,
which is formed as the following format:
Expand All @@ -106,43 +78,27 @@ class ICDARTxtTextRecogAnnParser(BaseParser):
'utf-8-sig'.
nproc (int): The number of processes to parse the annotation. Defaults
to 1.
base_name (bool): Whether to use the basename of the image path as the
image name. Defaults to False.
remove_strs (List[str], Optional): Used to remove redundant strings in
the transcription. Defaults to ['"'].
"""

def __init__(self,
separator: str = ',',
ignore: str = '#',
format: str = 'img,text',
encoding: str = 'utf-8-sig',
nproc: int = 1,
base_name: bool = False,
remove_strs: Optional[List[str]] = ['"']) -> None:
nproc: int = 1) -> None:
self.sep = separator
self.format = format
self.encoding = encoding
self.ignore = ignore
self.base_name = base_name
self.remove_strs = remove_strs
super().__init__(nproc=nproc)

def parse_files(self, files: str, split: str) -> List:
"""Parse annotations."""
assert isinstance(files, str)
samples = list()
for anno in self.loader(
file_path=files,
format=self.format,
encoding=self.encoding,
separator=self.sep):
text = anno['text'].strip()
if self.remove_strs is not None:
for strs in self.remove_strs:
text = text.replace(strs, '')
img_name = anno['img'] if not self.base_name else \
osp.basename(anno['img'])
samples.append((img_name, text))
file_path=files, format=self.format, encoding=self.encoding):
text = anno['text'].strip().replace('"', '')
samples.append((anno['img'], text))

return samples

0 comments on commit d11757d

Please sign in to comment.