odulcy-mindee commited on
Commit
24ca0e8
·
verified ·
1 Parent(s): 8720f09

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. app.py +19 -3
  2. backend/pytorch.py +13 -0
  3. requirements.txt +1 -0
app.py CHANGED
@@ -6,9 +6,10 @@
6
  import cv2
7
  import matplotlib.pyplot as plt
8
  import numpy as np
 
9
  import streamlit as st
10
  import torch
11
- from backend.pytorch import DET_ARCHS, RECO_ARCHS, forward_image, load_predictor
12
 
13
  from doctr.io import DocumentFile
14
  from doctr.utils.visualization import visualize_page
@@ -16,7 +17,7 @@ from doctr.utils.visualization import visualize_page
16
  forward_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
17
 
18
 
19
- def main(det_archs, reco_archs):
20
  """Build a streamlit layout"""
21
  # Wide mode
22
  st.set_page_config(layout="wide")
@@ -67,6 +68,11 @@ def main(det_archs, reco_archs):
67
  straighten_pages = st.sidebar.checkbox("Straighten pages", value=False)
68
  # Export as straight boxes
69
  export_straight_boxes = st.sidebar.checkbox("Export as straight boxes", value=False)
 
 
 
 
 
70
  st.sidebar.write("\n")
71
  # Binarization threshold
72
  bin_thresh = st.sidebar.slider("Binarization threshold", min_value=0.1, max_value=0.9, value=0.3, step=0.1)
@@ -92,6 +98,9 @@ def main(det_archs, reco_archs):
92
  bin_thresh=bin_thresh,
93
  box_thresh=box_thresh,
94
  device=forward_device,
 
 
 
95
  )
96
 
97
  with st.spinner("Analyzing..."):
@@ -117,10 +126,17 @@ def main(det_archs, reco_archs):
117
  img = out.pages[0].synthesize()
118
  cols[3].image(img, clamp=True)
119
 
 
 
 
 
 
 
 
120
  # Display JSON
121
  st.markdown("\nHere are your analysis results in JSON format:")
122
  st.json(page_export, expanded=False)
123
 
124
 
125
  if __name__ == "__main__":
126
- main(DET_ARCHS, RECO_ARCHS)
 
6
  import cv2
7
  import matplotlib.pyplot as plt
8
  import numpy as np
9
+ import pandas as pd
10
  import streamlit as st
11
  import torch
12
+ from backend.pytorch import DET_ARCHS, LAYOUT_ARCHS, RECO_ARCHS, forward_image, load_predictor
13
 
14
  from doctr.io import DocumentFile
15
  from doctr.utils.visualization import visualize_page
 
17
  forward_device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
18
 
19
 
20
+ def main(det_archs, reco_archs, layout_archs):
21
  """Build a streamlit layout"""
22
  # Wide mode
23
  st.set_page_config(layout="wide")
 
68
  straighten_pages = st.sidebar.checkbox("Straighten pages", value=False)
69
  # Export as straight boxes
70
  export_straight_boxes = st.sidebar.checkbox("Export as straight boxes", value=False)
71
+ # Layout detection
72
+ detect_layout = st.sidebar.checkbox("Detect layout", value=False)
73
+ layout_arch = st.sidebar.selectbox("Layout detection model", layout_archs, disabled=not detect_layout)
74
+ # Table detection (relies on the layout model to locate tables)
75
+ detect_tables = st.sidebar.checkbox("Detect tables", value=False)
76
  st.sidebar.write("\n")
77
  # Binarization threshold
78
  bin_thresh = st.sidebar.slider("Binarization threshold", min_value=0.1, max_value=0.9, value=0.3, step=0.1)
 
98
  bin_thresh=bin_thresh,
99
  box_thresh=box_thresh,
100
  device=forward_device,
101
+ detect_layout=detect_layout,
102
+ layout_arch=layout_arch,
103
+ detect_tables=detect_tables,
104
  )
105
 
106
  with st.spinner("Analyzing..."):
 
126
  img = out.pages[0].synthesize()
127
  cols[3].image(img, clamp=True)
128
 
129
+ # Display extracted tables (if any)
130
+ if out.pages[0].tables:
131
+ st.markdown("\nExtracted tables:")
132
+ for idx, table in enumerate(out.pages[0].tables):
133
+ st.markdown(f"**Table {idx + 1}** ({table.num_rows} x {table.num_cols})")
134
+ st.dataframe(pd.DataFrame(table.to_grid()))
135
+
136
  # Display JSON
137
  st.markdown("\nHere are your analysis results in JSON format:")
138
  st.json(page_export, expanded=False)
139
 
140
 
141
  if __name__ == "__main__":
142
+ main(DET_ARCHS, RECO_ARCHS, LAYOUT_ARCHS)
backend/pytorch.py CHANGED
@@ -31,6 +31,10 @@ RECO_ARCHS = [
31
  "parseq",
32
  "viptr_tiny",
33
  ]
 
 
 
 
34
 
35
 
36
  def load_predictor(
@@ -44,6 +48,9 @@ def load_predictor(
44
  bin_thresh: float,
45
  box_thresh: float,
46
  device: torch.device,
 
 
 
47
  ) -> OCRPredictor:
48
  """Load a predictor from doctr.models
49
 
@@ -58,6 +65,9 @@ def load_predictor(
58
  bin_thresh: binarization threshold for the segmentation map
59
  box_thresh: minimal objectness score to consider a box
60
  device: torch.device, the device to load the predictor on
 
 
 
61
 
62
  Returns:
63
  instance of OCRPredictor
@@ -72,6 +82,9 @@ def load_predictor(
72
  detect_orientation=not assume_straight_pages,
73
  disable_page_orientation=disable_page_orientation,
74
  disable_crop_orientation=disable_crop_orientation,
 
 
 
75
  ).to(device)
76
  predictor.det_predictor.model.postprocessor.bin_thresh = bin_thresh
77
  predictor.det_predictor.model.postprocessor.box_thresh = box_thresh
 
31
  "parseq",
32
  "viptr_tiny",
33
  ]
34
+ LAYOUT_ARCHS = [
35
+ "lw_detr_s",
36
+ "lw_detr_m",
37
+ ]
38
 
39
 
40
  def load_predictor(
 
48
  bin_thresh: float,
49
  box_thresh: float,
50
  device: torch.device,
51
+ detect_layout: bool,
52
+ layout_arch: str,
53
+ detect_tables: bool,
54
  ) -> OCRPredictor:
55
  """Load a predictor from doctr.models
56
 
 
65
  bin_thresh: binarization threshold for the segmentation map
66
  box_thresh: minimal objectness score to consider a box
67
  device: torch.device, the device to load the predictor on
68
+ detect_layout: whether to run a layout detection model and attach the regions to each page
69
+ layout_arch: layout architecture to use when detect_layout is True
70
+ detect_tables: whether to detect tables (via the layout model), structure them and attach them to each page
71
 
72
  Returns:
73
  instance of OCRPredictor
 
82
  detect_orientation=not assume_straight_pages,
83
  disable_page_orientation=disable_page_orientation,
84
  disable_crop_orientation=disable_crop_orientation,
85
+ detect_layout=detect_layout,
86
+ layout_arch=layout_arch,
87
+ detect_tables=detect_tables,
88
  ).to(device)
89
  predictor.det_predictor.model.postprocessor.bin_thresh = bin_thresh
90
  predictor.det_predictor.model.postprocessor.box_thresh = box_thresh
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  -e "python-doctr[viz] @ git+https://github.com/mindee/doctr.git"
2
  streamlit>=1.0.0
 
 
1
  -e "python-doctr[viz] @ git+https://github.com/mindee/doctr.git"
2
  streamlit>=1.0.0
3
+ pandas>=2.0.0