From 2ca3928642f6b289ba2adb95bfada138db62d3f1 Mon Sep 17 00:00:00 2001 From: T2K-Felix <125863421+felixT2K@users.noreply.github.com> Date: Sun, 20 Oct 2024 17:09:17 +0200 Subject: [PATCH] [demo/docs] Update notebook docs & minor demo update / fix (#1755) --- demo/app.py | 24 ++++++++++++------------ demo/backend/pytorch.py | 4 +++- demo/backend/tensorflow.py | 4 +++- demo/packages.txt | 1 + doctr/io/elements.py | 4 ++-- doctr/models/utils/pytorch.py | 2 +- notebooks/README.rst | 27 +++++++++++++++------------ 7 files changed, 37 insertions(+), 29 deletions(-) diff --git a/demo/app.py b/demo/app.py index 60adba0fb8..d43c8eff91 100644 --- a/demo/app.py +++ b/demo/app.py @@ -71,15 +71,14 @@ def main(det_archs, reco_archs): # Only straight pages or possible rotation st.sidebar.title("Parameters") assume_straight_pages = st.sidebar.checkbox("Assume straight pages", value=True) - st.sidebar.write("\n") # Disable page orientation detection disable_page_orientation = st.sidebar.checkbox("Disable page orientation detection", value=False) - st.sidebar.write("\n") # Disable crop orientation detection disable_crop_orientation = st.sidebar.checkbox("Disable crop orientation detection", value=False) - st.sidebar.write("\n") # Straighten pages straighten_pages = st.sidebar.checkbox("Straighten pages", value=False) + # Export as straight boxes + export_straight_boxes = st.sidebar.checkbox("Export as straight boxes", value=False) st.sidebar.write("\n") # Binarization threshold bin_thresh = st.sidebar.slider("Binarization threshold", min_value=0.1, max_value=0.9, value=0.3, step=0.1) @@ -95,15 +94,16 @@ def main(det_archs, reco_archs): else: with st.spinner("Loading model..."): predictor = load_predictor( - det_arch, - reco_arch, - assume_straight_pages, - straighten_pages, - disable_page_orientation, - disable_crop_orientation, - bin_thresh, - box_thresh, - forward_device, + det_arch=det_arch, + reco_arch=reco_arch, + assume_straight_pages=assume_straight_pages, + straighten_pages=straighten_pages, + export_as_straight_boxes=export_straight_boxes, + disable_page_orientation=disable_page_orientation, + disable_crop_orientation=disable_crop_orientation, + bin_thresh=bin_thresh, + box_thresh=box_thresh, + device=forward_device, ) with st.spinner("Analyzing..."): diff --git a/demo/backend/pytorch.py b/demo/backend/pytorch.py index e3ced74d5f..548d696dde 100644 --- a/demo/backend/pytorch.py +++ b/demo/backend/pytorch.py @@ -37,6 +37,7 @@ def load_predictor( reco_arch: str, assume_straight_pages: bool, straighten_pages: bool, + export_as_straight_boxes: bool, disable_page_orientation: bool, disable_crop_orientation: bool, bin_thresh: float, @@ -51,6 +52,7 @@ def load_predictor( reco_arch: recognition architecture assume_straight_pages: whether to assume straight pages or not straighten_pages: whether to straighten rotated pages or not + export_as_straight_boxes: whether to export boxes as straight or not disable_page_orientation: whether to disable page orientation or not disable_crop_orientation: whether to disable crop orientation or not bin_thresh: binarization threshold for the segmentation map @@ -67,7 +69,7 @@ def load_predictor( pretrained=True, assume_straight_pages=assume_straight_pages, straighten_pages=straighten_pages, - export_as_straight_boxes=straighten_pages, + export_as_straight_boxes=export_as_straight_boxes, detect_orientation=not assume_straight_pages, disable_page_orientation=disable_page_orientation, disable_crop_orientation=disable_crop_orientation, diff --git a/demo/backend/tensorflow.py b/demo/backend/tensorflow.py index 6ca9614159..9fecfce3bc 100644 --- a/demo/backend/tensorflow.py +++ b/demo/backend/tensorflow.py @@ -36,6 +36,7 @@ def load_predictor( reco_arch: str, assume_straight_pages: bool, straighten_pages: bool, + export_as_straight_boxes: bool, disable_page_orientation: bool, disable_crop_orientation: bool, bin_thresh: float, @@ -50,6 +51,7 @@ def load_predictor( reco_arch: recognition architecture assume_straight_pages: whether to assume straight pages or not straighten_pages: whether to straighten rotated pages or not + export_as_straight_boxes: whether to export boxes as straight or not disable_page_orientation: whether to disable page orientation or not disable_crop_orientation: whether to disable crop orientation or not bin_thresh: binarization threshold for the segmentation map @@ -67,7 +69,7 @@ def load_predictor( pretrained=True, assume_straight_pages=assume_straight_pages, straighten_pages=straighten_pages, - export_as_straight_boxes=straighten_pages, + export_as_straight_boxes=export_as_straight_boxes, detect_orientation=not assume_straight_pages, disable_page_orientation=disable_page_orientation, disable_crop_orientation=disable_crop_orientation, diff --git a/demo/packages.txt b/demo/packages.txt index d0f1245c6f..c0e46c2d27 100644 --- a/demo/packages.txt +++ b/demo/packages.txt @@ -1 +1,2 @@ python3-opencv +fonts-freefont-ttf diff --git a/doctr/io/elements.py b/doctr/io/elements.py index b27ecb35eb..55e1d5e84a 100644 --- a/doctr/io/elements.py +++ b/doctr/io/elements.py @@ -168,7 +168,7 @@ def __init__( if geometry is None: # Check whether this is a rotated or straight box box_resolution_fn = resolve_enclosing_rbbox if len(words[0].geometry) == 4 else resolve_enclosing_bbox - geometry = box_resolution_fn([w.geometry for w in words]) # type: ignore[operator] + geometry = box_resolution_fn([w.geometry for w in words]) # type: ignore[misc] super().__init__(words=words) self.geometry = geometry @@ -232,7 +232,7 @@ def __init__( box_resolution_fn = ( resolve_enclosing_rbbox if isinstance(lines[0].geometry, np.ndarray) else resolve_enclosing_bbox ) - geometry = box_resolution_fn(line_boxes + artefact_boxes) # type: ignore[operator] + geometry = box_resolution_fn(line_boxes + artefact_boxes) # type: ignore super().__init__(lines=lines, artefacts=artefacts) self.geometry = geometry diff --git a/doctr/models/utils/pytorch.py b/doctr/models/utils/pytorch.py index 0401cdef6c..998ccb7cf1 100644 --- a/doctr/models/utils/pytorch.py +++ b/doctr/models/utils/pytorch.py @@ -157,7 +157,7 @@ def export_model_to_onnx(model: nn.Module, model_name: str, dummy_input: torch.T """ torch.onnx.export( model, - dummy_input, + dummy_input, # type: ignore[arg-type] f"{model_name}.onnx", input_names=["input"], output_names=["logits"], diff --git a/notebooks/README.rst b/notebooks/README.rst index 96f9e80edb..940e43f7b7 100644 --- a/notebooks/README.rst +++ b/notebooks/README.rst @@ -3,15 +3,18 @@ docTR Notebooks Here are some notebooks compiled for users to better leverage the library capabilities: -+--------------------------------------------------------------------------------------------------------+----------------------------------------------+---------------------------------------------------------------------------------------------------------------------+ -| Notebook | Description | Colab | -+--------------------------------------------------------------------------------------------------------+----------------------------------------------+---------------------------------------------------------------------------------------------------------------------+ -| `[Quicktour] `_ | A presentation of the main features of docTR | .. image:: https://colab.research.google.com/assets/colab-badge.svg | -| | | :target: https://colab.research.google.com/github/mindee/notebooks/blob/main/doctr/quicktour.ipynb | -+--------------------------------------------------------------------------------------------------------+----------------------------------------------+---------------------------------------------------------------------------------------------------------------------+ -| `[Export as PDF/A] `_ | Produce searchable PDFs from docTR results | .. image:: https://colab.research.google.com/assets/colab-badge.svg | -| | | :target: https://colab.research.google.com/github/mindee/notebooks/blob/main/doctr/export_as_pdfa.ipynb | -+--------------------------------------------------------------------------------------------------------+----------------------------------------------+---------------------------------------------------------------------------------------------------------------------+ -| `[Artefact detection] `_ | Object detection for artefacts in documents | .. image:: https://colab.research.google.com/assets/colab-badge.svg | -| | | :target: https://colab.research.google.com/github/mindee/notebooks/blob/main/doctr/artefact_detection.ipynb | -+--------------------------------------------------------------------------------------------------------+----------------------------------------------+---------------------------------------------------------------------------------------------------------------------+ ++--------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+ +| Notebook | Description | Colab | ++--------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+ +| `[Quicktour] `_ | A presentation of the main features of docTR | .. image:: https://colab.research.google.com/assets/colab-badge.svg | +| | | :target: https://colab.research.google.com/github/mindee/notebooks/blob/main/doctr/quicktour.ipynb | ++--------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+ +| `[Export as PDF/A] `_ | Produce searchable PDFs from docTR results | .. image:: https://colab.research.google.com/assets/colab-badge.svg | +| | | :target: https://colab.research.google.com/github/mindee/notebooks/blob/main/doctr/export_as_pdfa.ipynb | ++--------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+ +| `[Using standalone predictors] `_ | Showcase how to use detection, recognition, and orientation predictors| .. image:: https://colab.research.google.com/assets/colab-badge.svg | +| | | :target: https://colab.research.google.com/github/mindee/notebooks/blob/main/doctr/using_standalone_predictors.ipynb | ++--------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+ +| `[Dealing with rotated documents] `_ | A presentation on how to handle documents containing rotations | .. image:: https://colab.research.google.com/assets/colab-badge.svg | +| | | :target: https://colab.research.google.com/github/mindee/notebooks/blob/main/doctr/dealing_with_rotations.ipynb | ++--------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+