-
Notifications
You must be signed in to change notification settings - Fork 420
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
Imgur5k dataset integration #785
Merged
Merged
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
e7122a5
start synth
felixdittrich92 4fa0aff
cleanup
felixdittrich92 3ffdcc5
Merge branch 'mindee:main' into main
felixdittrich92 b74f06a
start synth
felixdittrich92 1a661e0
add synthtext
felixdittrich92 6270c93
add docu and tests
felixdittrich92 d74f148
apply code factor suggestions
felixdittrich92 9099e95
apply changes
felixdittrich92 23eca0d
Merge branch 'mindee:main' into main
felixdittrich92 7ba31e1
clean
felixdittrich92 02a8104
Merge branch 'mindee:main' into main
felixdittrich92 6955110
Merge branch 'mindee:main' into main
felixdittrich92 8fbeb30
Merge branch 'mindee:main' into main
felixdittrich92 7408935
Merge branch 'mindee:main' into main
felixdittrich92 a2b0fbc
Merge branch 'mindee:main' into main
felixdittrich92 a9cbd14
Merge branch 'mindee:main' into main
felixdittrich92 b245443
Merge branch 'mindee:main' into main
felixdittrich92 0cb2f7b
Merge branch 'mindee:main' into main
felixdittrich92 743c54a
Merge branch 'mindee:main' into main
felixdittrich92 1c1cbcb
Merge branch 'mindee:main' into main
felixdittrich92 cfbd898
Merge branch 'mindee:main' into main
felixdittrich92 8787f74
Merge branch 'mindee:main' into main
felixdittrich92 47ed381
Merge branch 'mindee:main' into main
felixdittrich92 eb7f59c
Merge branch 'mindee:main' into main
felixdittrich92 efaa2c0
Merge branch 'mindee:main' into main
felixdittrich92 f7288ca
Merge branch 'mindee:main' into main
felixdittrich92 044f523
Merge branch 'mindee:main' into main
felixdittrich92 9dc3983
Merge branch 'mindee:main' into main
felixdittrich92 fc4c2d9
Merge branch 'mindee:main' into main
felixdittrich92 78b8d27
Merge branch 'mindee:main' into main
felixdittrich92 d26341b
Merge branch 'mindee:main' into main
felixdittrich92 6338cac
Merge branch 'mindee:main' into main
felixdittrich92 68d3a52
Merge branch 'mindee:main' into main
felixdittrich92 4663fad
Merge branch 'mindee:main' into main
felixdittrich92 d04edd4
Merge branch 'mindee:main' into main
felixdittrich92 954cc5b
Merge branch 'mindee:main' into main
felixdittrich92 fea9aa3
Merge branch 'mindee:main' into main
felixdittrich92 06bb750
Merge branch 'mindee:main' into main
felixdittrich92 b11a702
Merge branch 'mindee:main' into main
felixdittrich92 2d241fd
Merge branch 'mindee:main' into main
felixdittrich92 97e28b0
Merge branch 'mindee:main' into main
felixdittrich92 b1fc731
Merge branch 'mindee:main' into main
felixdittrich92 e9daca8
Merge branch 'mindee:main' into main
felixdittrich92 be7280e
Merge branch 'mindee:main' into main
felixdittrich92 af85b08
Merge branch 'mindee:main' into main
felixdittrich92 ce7b56c
Merge branch 'mindee:main' into main
felixdittrich92 15d4da0
Merge branch 'mindee:main' into main
felixdittrich92 0549420
Merge branch 'mindee:main' into main
felixdittrich92 4d530d3
Merge branch 'mindee:main' into main
felixdittrich92 8407592
Merge branch 'mindee:main' into main
felixdittrich92 dfc7a3c
start imgur5k
felixdittrich92 52c1103
up
felixdittrich92 f50f647
update box computation
felixdittrich92 1945477
make flake happy
felixdittrich92 dad3b1f
filter images without boxes
felixdittrich92 5553a63
aqpply changes
felixdittrich92 70f0b78
change desc
felixdittrich92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Copyright (C) 2021-2022, Mindee. | ||
|
||
# This program is licensed under the Apache License version 2. | ||
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details. | ||
|
||
import json | ||
import os | ||
from pathlib import Path | ||
from typing import Any, Dict, List, Tuple | ||
|
||
import cv2 | ||
import numpy as np | ||
|
||
from .datasets import AbstractDataset | ||
from .utils import convert_target_to_relative | ||
|
||
__all__ = ["IMGUR5K"] | ||
|
||
|
||
class IMGUR5K(AbstractDataset): | ||
"""IMGUR5K dataset from `"TextStyleBrush: Transfer of Text Aesthetics from a Single Example" | ||
<https://arxiv.org/abs/2106.08385>`_ | | ||
`"repository" <https://github.com/facebookresearch/IMGUR5K-Handwriting-Dataset>`_. | ||
|
||
Example:: | ||
>>> # NOTE: You need to download/generate the dataset from the repository. | ||
>>> from doctr.datasets import IMGUR5K | ||
>>> train_set = IMGUR5K(train=True, img_folder="/path/to/IMGUR5K-Handwriting-Dataset/images", | ||
>>> label_path="/path/to/IMGUR5K-Handwriting-Dataset/dataset_info/imgur5k_annotations.json") | ||
>>> img, target = train_set[0] | ||
>>> test_set = IMGUR5K(train=False, img_folder="/path/to/IMGUR5K-Handwriting-Dataset/images", | ||
>>> label_path="/path/to/IMGUR5K-Handwriting-Dataset/dataset_info/imgur5k_annotations.json") | ||
>>> img, target = test_set[0] | ||
Args: | ||
img_folder: folder with all the images of the dataset | ||
label_path: path to the annotations file of the dataset | ||
train: whether the subset should be the training one | ||
use_polygons: whether polygons should be considered as rotated bounding box (instead of straight ones) | ||
**kwargs: keyword arguments from `AbstractDataset`. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
img_folder: str, | ||
label_path: str, | ||
train: bool = True, | ||
use_polygons: bool = False, | ||
**kwargs: Any, | ||
) -> None: | ||
super().__init__(img_folder, pre_transforms=convert_target_to_relative, **kwargs) | ||
|
||
# File existence check | ||
if not os.path.exists(label_path) or not os.path.exists(img_folder): | ||
raise FileNotFoundError( | ||
f"unable to locate {label_path if not os.path.exists(label_path) else img_folder}") | ||
|
||
self.data: List[Tuple[Path, Dict[str, Any]]] = [] | ||
self.train = train | ||
np_dtype = np.float32 | ||
|
||
img_names = os.listdir(img_folder) | ||
train_samples = int(len(img_names) * 0.9) | ||
set_slice = slice(train_samples) if self.train else slice(train_samples, None) | ||
|
||
with open(label_path) as f: | ||
annotation_file = json.load(f) | ||
|
||
for img_name in img_names[set_slice]: | ||
img_path = Path(img_folder, img_name) | ||
img_id = img_name.split(".")[0] | ||
|
||
# File existence check | ||
if not os.path.exists(os.path.join(self.root, img_name)): | ||
raise FileNotFoundError(f"unable to locate {os.path.join(self.root, img_name)}") | ||
|
||
# some files have no annotations which are marked with only a dot in the 'word' key | ||
# ref: https://github.com/facebookresearch/IMGUR5K-Handwriting-Dataset/blob/main/README.md | ||
if img_id not in annotation_file['index_to_ann_map'].keys(): | ||
continue | ||
ann_ids = annotation_file['index_to_ann_map'][img_id] | ||
annotations = [annotation_file['ann_id'][a_id] for a_id in ann_ids] | ||
|
||
labels = [ann['word'] for ann in annotations if ann['word'] != '.'] | ||
# x_center, y_center, width, height, angle | ||
_boxes = [list(map(float, ann['bounding_box'].strip('[ ]').split(', '))) | ||
for ann in annotations if ann['word'] != '.'] | ||
# (x, y) coordinates of top left, top right, bottom right, bottom left corners | ||
box_targets = [cv2.boxPoints(((box[0], box[1]), (box[2], box[3]), box[4])) for box in _boxes] | ||
|
||
if not use_polygons: | ||
# xmin, ymin, xmax, ymax | ||
box_targets = [np.concatenate((points.min(0), points.max(0)), axis=-1) for points in box_targets] | ||
|
||
# filter images without boxes | ||
if len(box_targets) > 0: | ||
self.data.append((img_path, dict(boxes=np.asarray(box_targets, dtype=np_dtype), labels=labels))) | ||
|
||
def extra_repr(self) -> str: | ||
return f"train={self.train}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing the "train" arg description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The arg descriptions need to be in the same order as in the
__init__
🙃