Skip to content

Commit

Permalink
style: Added isort ordering & dedicated CI check (mindee#577)
Browse files Browse the repository at this point in the history
* chore: Added isort config

* ci: Added isort CI job

* docs: Updated CONTRIBUTING

* style: Reordered imports

* refactor: Moved doctr/models/data_utils to doctr/utils/data

* chore: Fixed circular import
  • Loading branch information
fg-mindee authored Nov 4, 2021
1 parent 22f3aab commit c52488c
Show file tree
Hide file tree
Showing 144 changed files with 501 additions and 380 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ jobs:
flake8 --version
flake8 ./
isort-py3:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: [3.7]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run isort
run: |
pip install isort
isort **/*.py -c -v
mypy-py3:
runs-on: ${{ matrix.os }}
strategy:
Expand Down
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ flake8 ./
```
This will read the `.flake8` setting file and let you know whether your commits need some adjustments.

#### Import order

In order to ensure there is a common import order convention, run [isort](https://github.com/PyCQA/isort) as follows:

```shell
isort **/*.py
```
This will reorder the imports of your local files.

#### Annotation typing

Additionally, to catch type-related issues and have a cleaner codebase, annotation typing are expected. After installing [mypy](https://github.com/python/mypy), you can run the verifications as follows:
Expand Down
2 changes: 1 addition & 1 deletion api/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import os
import doctr

import doctr

PROJECT_NAME: str = 'docTR API template'
PROJECT_DESCRIPTION: str = 'Template API for Optical Character Recognition'
Expand Down
7 changes: 3 additions & 4 deletions api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import time
from fastapi import FastAPI, Request
from fastapi.openapi.utils import get_openapi

from app import config as cfg
from app.routes import recognition, detection, ocr

from app.routes import detection, ocr, recognition
from fastapi import FastAPI, Request
from fastapi.openapi.utils import get_openapi

app = FastAPI(title=cfg.PROJECT_NAME, description=cfg.PROJECT_DESCRIPTION, debug=cfg.DEBUG, version=cfg.VERSION)

Expand Down
6 changes: 3 additions & 3 deletions api/app/routes/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# 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.

from fastapi import APIRouter, UploadFile, File
from typing import List

from doctr.io import decode_img_as_tensor
from app.vision import det_predictor
from app.schemas import DetectionOut
from app.vision import det_predictor
from fastapi import APIRouter, File, UploadFile

from doctr.io import decode_img_as_tensor

router = APIRouter()

Expand Down
6 changes: 3 additions & 3 deletions api/app/routes/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# 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.

from fastapi import APIRouter, UploadFile, File
from typing import List

from doctr.io import decode_img_as_tensor
from app.vision import predictor
from app.schemas import OCROut
from app.vision import predictor
from fastapi import APIRouter, File, UploadFile

from doctr.io import decode_img_as_tensor

router = APIRouter()

Expand Down
7 changes: 3 additions & 4 deletions api/app/routes/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
# 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.

from fastapi import APIRouter, UploadFile, File

from doctr.io import decode_img_as_tensor
from app.vision import reco_predictor
from app.schemas import RecognitionOut
from app.vision import reco_predictor
from fastapi import APIRouter, File, UploadFile

from doctr.io import decode_img_as_tensor

router = APIRouter()

Expand Down
3 changes: 2 additions & 1 deletion api/app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# 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.

from pydantic import BaseModel, Field
from typing import Tuple

from pydantic import BaseModel, Field


# Recognition output
class RecognitionOut(BaseModel):
Expand Down
1 change: 0 additions & 1 deletion api/app/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from doctr.models import ocr_predictor


predictor = ocr_predictor(pretrained=True)
det_predictor = predictor.det_predictor
reco_predictor = predictor.reco_predictor
3 changes: 1 addition & 2 deletions api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import pytest
import requests
from httpx import AsyncClient

from app.main import app
from httpx import AsyncClient


@pytest.fixture(scope="session")
Expand Down
3 changes: 2 additions & 1 deletion api/tests/routes/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# 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 pytest
import numpy as np
import pytest
from scipy.optimize import linear_sum_assignment

from doctr.utils.metrics import box_iou


Expand Down
3 changes: 2 additions & 1 deletion api/tests/routes/test_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# 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 pytest
import numpy as np
import pytest
from scipy.optimize import linear_sum_assignment

from doctr.utils.metrics import box_iou


Expand Down
5 changes: 3 additions & 2 deletions demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import os
import streamlit as st

import matplotlib.pyplot as plt
import streamlit as st

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

import tensorflow as tf
import cv2
import tensorflow as tf

gpu_devices = tf.config.experimental.list_physical_devices('GPU')
if any(gpu_devices):
Expand Down
5 changes: 3 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@

# -- Path setup --------------------------------------------------------------

import sphinx_rtd_theme

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
from datetime import datetime

import sphinx_rtd_theme

sys.path.insert(0, os.path.abspath('../..'))
import doctr

Expand Down
6 changes: 1 addition & 5 deletions doctr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from . import datasets, io, models, transforms, utils
from .file_utils import is_tf_available, is_torch_available
from .version import __version__ # noqa: F401
from . import datasets
from . import io
from . import transforms
from . import utils
from . import models
14 changes: 7 additions & 7 deletions doctr/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from .utils import *
from .vocabs import *
from .funsd import *
from doctr.file_utils import is_tf_available

from .classification import *
from .cord import *
from .detection import *
from .recognition import *
from .funsd import *
from .ocr import *
from .recognition import *
from .sroie import *
from .classification import *

from doctr.file_utils import is_tf_available
from .utils import *
from .vocabs import *

if is_tf_available():
from .loader import *
6 changes: 4 additions & 2 deletions doctr/datasets/classification/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
# 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.

from PIL import ImageDraw, Image, ImageFont
from typing import Any, Tuple, Optional, Callable, List
import logging
import platform
from typing import Any, Callable, List, Optional, Tuple

from PIL import Image, ImageDraw, ImageFont

from doctr.io.image import tensor_from_pil
from doctr.utils.fonts import get_font

from ..datasets import AbstractDataset


Expand Down
2 changes: 1 addition & 1 deletion doctr/datasets/classification/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

from torch.utils.data._utils.collate import default_collate
from .base import _CharacterGenerator

from .base import _CharacterGenerator

__all__ = ['CharacterGenerator']

Expand Down
2 changes: 1 addition & 1 deletion doctr/datasets/classification/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import tensorflow as tf
from .base import _CharacterGenerator

from .base import _CharacterGenerator

__all__ = ['CharacterGenerator']

Expand Down
10 changes: 6 additions & 4 deletions doctr/datasets/cord.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
# 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 os
import json
import numpy as np
import os
from pathlib import Path
from typing import List, Dict, Any, Tuple, Optional, Callable
from typing import Any, Callable, Dict, List, Optional, Tuple

import numpy as np

from .datasets import VisionDataset
from doctr.utils.geometry import fit_rbbox

from .datasets import VisionDataset

__all__ = ['CORD']


Expand Down
5 changes: 2 additions & 3 deletions doctr/datasets/datasets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import os
from pathlib import Path
from typing import Any, Callable, List, Optional, Tuple, Union
from zipfile import ZipFile
from typing import List, Any, Optional, Tuple, Callable, Union

from doctr.models.data_utils import download_from_url

from doctr.utils.data import download_from_url

__all__ = ['_AbstractDataset', '_VisionDataset']

Expand Down
5 changes: 3 additions & 2 deletions doctr/datasets/datasets/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import os
from typing import List, Any, Tuple
from typing import Any, List, Tuple

import torch

from doctr.io import read_img_as_tensor
from .base import _AbstractDataset, _VisionDataset

from .base import _AbstractDataset, _VisionDataset

__all__ = ['AbstractDataset', 'VisionDataset']

Expand Down
5 changes: 3 additions & 2 deletions doctr/datasets/datasets/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import os
from typing import List, Any, Tuple
from typing import Any, List, Tuple

import tensorflow as tf

from doctr.io import read_img_as_tensor
from .base import _AbstractDataset, _VisionDataset

from .base import _AbstractDataset, _VisionDataset

__all__ = ['AbstractDataset', 'VisionDataset']

Expand Down
8 changes: 5 additions & 3 deletions doctr/datasets/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
# 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 os
import json
import os
from typing import Any, Callable, Dict, List, Optional, Tuple

import numpy as np
from typing import List, Tuple, Dict, Any, Optional, Callable

from .datasets import AbstractDataset
from doctr.utils.geometry import fit_rbbox

from .datasets import AbstractDataset

__all__ = ["DetectionDataset"]


Expand Down
7 changes: 4 additions & 3 deletions doctr/datasets/funsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# 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 os
import json
import numpy as np
import os
from pathlib import Path
from typing import List, Dict, Any, Tuple, Optional, Callable
from typing import Any, Callable, Dict, List, Optional, Tuple

import numpy as np

from .datasets import VisionDataset

Expand Down
5 changes: 3 additions & 2 deletions doctr/datasets/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.

import math
import tensorflow as tf
from typing import Callable, Optional

import numpy as np
from typing import Optional, Callable
import tensorflow as tf

from doctr.utils.multithreading import multithread_exec

Expand Down
Loading

0 comments on commit c52488c

Please sign in to comment.