Felix92 commited on
Commit
332b9fc
·
verified ·
1 Parent(s): 93312f4

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +36 -20
  2. requirements.txt +1 -1
app.py CHANGED
@@ -9,7 +9,7 @@ from matplotlib.figure import Figure
9
  from PIL import Image
10
 
11
  from onnxtr.io import DocumentFile
12
- from onnxtr.models import from_hub, ocr_predictor
13
  from onnxtr.models.predictor import OCRPredictor
14
  from onnxtr.utils.visualization import visualize_page
15
 
@@ -43,6 +43,7 @@ CUSTOM_RECO_ARCHS: List[str] = [
43
  def load_predictor(
44
  det_arch: str,
45
  reco_arch: str,
 
46
  assume_straight_pages: bool,
47
  straighten_pages: bool,
48
  export_as_straight_boxes: bool,
@@ -59,6 +60,7 @@ def load_predictor(
59
  ----
60
  det_arch: detection architecture
61
  reco_arch: recognition architecture
 
62
  assume_straight_pages: whether to assume straight pages or not
63
  disable_crop_orientation: whether to disable crop orientation or not
64
  disable_page_orientation: whether to disable page orientation or not
@@ -73,6 +75,11 @@ def load_predictor(
73
  -------
74
  instance of OCRPredictor
75
  """
 
 
 
 
 
76
  predictor = ocr_predictor(
77
  det_arch=det_arch,
78
  reco_arch=reco_arch if reco_arch not in CUSTOM_RECO_ARCHS else from_hub(reco_arch),
@@ -84,6 +91,9 @@ def load_predictor(
84
  detect_orientation=not assume_straight_pages,
85
  disable_crop_orientation=disable_crop_orientation,
86
  disable_page_orientation=disable_page_orientation,
 
 
 
87
  )
88
  predictor.det_predictor.model.postprocessor.bin_thresh = bin_thresh
89
  predictor.det_predictor.model.postprocessor.box_thresh = box_thresh
@@ -134,6 +144,7 @@ def analyze_page(
134
  page_idx: int,
135
  det_arch: str,
136
  reco_arch: str,
 
137
  assume_straight_pages: bool,
138
  disable_crop_orientation: bool,
139
  disable_page_orientation: bool,
@@ -152,6 +163,7 @@ def analyze_page(
152
  page_idx: index of the page to analyze
153
  det_arch: detection architecture
154
  reco_arch: recognition architecture
 
155
  assume_straight_pages: whether to assume straight pages or not
156
  disable_crop_orientation: whether to disable crop orientation or not
157
  disable_page_orientation: whether to disable page orientation or not
@@ -183,6 +195,7 @@ def analyze_page(
183
  predictor = load_predictor(
184
  det_arch=det_arch,
185
  reco_arch=reco_arch,
 
186
  assume_straight_pages=assume_straight_pages,
187
  straighten_pages=straighten_pages,
188
  export_as_straight_boxes=export_as_straight_boxes,
@@ -215,27 +228,28 @@ def analyze_page(
215
 
216
 
217
  with gr.Blocks(fill_height=True) as demo:
218
- gr.Markdown(
219
  """
220
- <p align="center">
221
- <img src="https://github.com/felixdittrich92/OnnxTR/raw/main/docs/images/logo.jpg" width="15%">
222
- </p>
223
-
224
- <div align="center">
225
-
226
- # OnnxTR OCR Demo
227
-
228
- [![GitHub OnnxTR](https://img.shields.io/badge/GitHub-blue?logo=github)](https://github.com/felixdittrich92/OnnxTR)
229
-
230
- [![PyPI](https://img.shields.io/pypi/v/onnxtr?color=blue)](https://pypi.org/project/onnxtr/)
231
-
 
 
 
232
  </div>
233
-
234
- ## To use this interactive demo for OnnxTR:
235
-
236
- ### 1. Upload a document (PDF, JPG, or PNG)
237
- ### 2. Select the model architectures for text detection and recognition you want to use
238
- ### 3. Press the "Analyze page" button to process the uploaded document
239
  """
240
  )
241
  with gr.Row():
@@ -246,6 +260,7 @@ with gr.Blocks(fill_height=True) as demo:
246
  reco_model = gr.Dropdown(
247
  choices=RECO_ARCHS + CUSTOM_RECO_ARCHS, value=RECO_ARCHS[0], label="Text recognition model"
248
  )
 
249
  assume_straight = gr.Checkbox(value=True, label="Assume straight pages")
250
  disable_crop_orientation = gr.Checkbox(value=False, label="Disable crop orientation")
251
  disable_page_orientation = gr.Checkbox(value=False, label="Disable page orientation")
@@ -276,6 +291,7 @@ with gr.Blocks(fill_height=True) as demo:
276
  page_selection,
277
  det_model,
278
  reco_model,
 
279
  assume_straight,
280
  disable_crop_orientation,
281
  disable_page_orientation,
 
9
  from PIL import Image
10
 
11
  from onnxtr.io import DocumentFile
12
+ from onnxtr.models import EngineConfig, from_hub, ocr_predictor
13
  from onnxtr.models.predictor import OCRPredictor
14
  from onnxtr.utils.visualization import visualize_page
15
 
 
43
  def load_predictor(
44
  det_arch: str,
45
  reco_arch: str,
46
+ use_gpu: bool,
47
  assume_straight_pages: bool,
48
  straighten_pages: bool,
49
  export_as_straight_boxes: bool,
 
60
  ----
61
  det_arch: detection architecture
62
  reco_arch: recognition architecture
63
+ use_gpu: whether to use the GPU or not
64
  assume_straight_pages: whether to assume straight pages or not
65
  disable_crop_orientation: whether to disable crop orientation or not
66
  disable_page_orientation: whether to disable page orientation or not
 
75
  -------
76
  instance of OCRPredictor
77
  """
78
+ engine_cfg = (
79
+ EngineConfig()
80
+ if use_gpu
81
+ else EngineConfig(providers=[("CPUExecutionProvider", {"arena_extend_strategy": "kSameAsRequested"})])
82
+ )
83
  predictor = ocr_predictor(
84
  det_arch=det_arch,
85
  reco_arch=reco_arch if reco_arch not in CUSTOM_RECO_ARCHS else from_hub(reco_arch),
 
91
  detect_orientation=not assume_straight_pages,
92
  disable_crop_orientation=disable_crop_orientation,
93
  disable_page_orientation=disable_page_orientation,
94
+ det_engine_cfg=engine_cfg,
95
+ reco_engine_cfg=engine_cfg,
96
+ clf_engine_cfg=engine_cfg,
97
  )
98
  predictor.det_predictor.model.postprocessor.bin_thresh = bin_thresh
99
  predictor.det_predictor.model.postprocessor.box_thresh = box_thresh
 
144
  page_idx: int,
145
  det_arch: str,
146
  reco_arch: str,
147
+ use_gpu: bool,
148
  assume_straight_pages: bool,
149
  disable_crop_orientation: bool,
150
  disable_page_orientation: bool,
 
163
  page_idx: index of the page to analyze
164
  det_arch: detection architecture
165
  reco_arch: recognition architecture
166
+ use_gpu: whether to use the GPU or not
167
  assume_straight_pages: whether to assume straight pages or not
168
  disable_crop_orientation: whether to disable crop orientation or not
169
  disable_page_orientation: whether to disable page orientation or not
 
195
  predictor = load_predictor(
196
  det_arch=det_arch,
197
  reco_arch=reco_arch,
198
+ use_gpu=use_gpu,
199
  assume_straight_pages=assume_straight_pages,
200
  straighten_pages=straighten_pages,
201
  export_as_straight_boxes=export_as_straight_boxes,
 
228
 
229
 
230
  with gr.Blocks(fill_height=True) as demo:
231
+ gr.HTML(
232
  """
233
+ <div style="text-align: center;">
234
+ <p style="display: flex; justify-content: center;">
235
+ <img src="https://github.com/felixdittrich92/OnnxTR/raw/main/docs/images/logo.jpg" width="15%">
236
+ </p>
237
+
238
+ <h1>OnnxTR OCR Demo</h1>
239
+
240
+ <p style="display: flex; justify-content: center; gap: 10px;">
241
+ <a href="https://github.com/felixdittrich92/OnnxTR" target="_blank">
242
+ <img src="https://img.shields.io/badge/GitHub-blue?logo=github" alt="GitHub OnnxTR">
243
+ </a>
244
+ <a href="https://pypi.org/project/onnxtr/" target="_blank">
245
+ <img src="https://img.shields.io/pypi/v/onnxtr?color=blue" alt="PyPI">
246
+ </a>
247
+ </p>
248
  </div>
249
+ <h2>To use this interactive demo for OnnxTR:</h2>
250
+ <h3> 1. Upload a document (PDF, JPG, or PNG)</h3>
251
+ <h3> 2. Select the model architectures for text detection and recognition you want to use</h3>
252
+ <h3> 3. Press the "Analyze page" button to process the uploaded document</h3>
 
 
253
  """
254
  )
255
  with gr.Row():
 
260
  reco_model = gr.Dropdown(
261
  choices=RECO_ARCHS + CUSTOM_RECO_ARCHS, value=RECO_ARCHS[0], label="Text recognition model"
262
  )
263
+ use_gpu = gr.Checkbox(value=True, label="Use GPU")
264
  assume_straight = gr.Checkbox(value=True, label="Assume straight pages")
265
  disable_crop_orientation = gr.Checkbox(value=False, label="Disable crop orientation")
266
  disable_page_orientation = gr.Checkbox(value=False, label="Disable page orientation")
 
291
  page_selection,
292
  det_model,
293
  reco_model,
294
+ use_gpu,
295
  assume_straight,
296
  disable_crop_orientation,
297
  disable_page_orientation,
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- -e git+https://github.com/felixdittrich92/OnnxTR.git#egg=onnxtr[cpu-headless,viz]
2
  gradio>=4.37.1,<6.0.0
 
1
+ -e git+https://github.com/felixdittrich92/OnnxTR.git#egg=onnxtr[gpu-headless,viz]
2
  gradio>=4.37.1,<6.0.0