MikkoLipsanen commited on
Commit
f00417c
·
verified ·
1 Parent(s): eee8423

Update app.py code to support new onnx text revognition model

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -1,6 +1,6 @@
1
  from optimum.onnxruntime import ORTModelForVision2Seq
 
2
  from transformers import TrOCRProcessor
3
- from huggingface_hub import login
4
  import gradio as gr
5
  import numpy as np
6
  import onnxruntime
@@ -16,8 +16,11 @@ from onnx_text_recognition import TextRecognition
16
 
17
  LINE_MODEL_PATH = "Kansallisarkisto/multicentury-textline-detection"
18
  REGION_MODEL_PATH = "Kansallisarkisto/court-records-region-detection"
19
- TROCR_PROCESSOR_PATH = "Kansallisarkisto/multicentury-htr-model-onnx"
20
- TROCR_MODEL_PATH = "Kansallisarkisto/multicentury-htr-model-onnx"
 
 
 
21
 
22
  # Allowed source paths for input images
23
  ALLOWED_SOURCES = ('https://astia.narc.fi', '/tmp/gradio')
@@ -51,7 +54,6 @@ def get_recognizer():
51
  """Initialize text recognition class."""
52
  try:
53
  recognizer = TextRecognition(
54
- processor_path = TROCR_PROCESSOR_PATH,
55
  model_path = TROCR_MODEL_PATH,
56
  device = 'cuda:0',
57
  half_precision = True,
@@ -79,9 +81,8 @@ def merge_lines(segment_predictions):
79
  def get_text_predictions(image, segment_predictions, recognizer):
80
  """Collects text prediction data into dicts based on detected text regions."""
81
  img_lines = merge_lines(segment_predictions)
82
- height, width = segment_predictions[0]['img_shape']
83
  # Process all lines of an image
84
- texts = recognizer.process_lines(img_lines, image, height, width)
85
  return texts
86
 
87
  def is_allowed_source(file_path):
@@ -123,7 +124,7 @@ with gr.Blocks(theme=gr.themes.Monochrome(), title="Multicentury HTR Demo") as d
123
  gr.Markdown("# Multicentury HTR Demo")
124
  gr.Markdown("""The HTR pipeline contains three components: text region detection, textline detection and handwritten text recognition.
125
  The components run machine learning models that have been trained at the National Archives of Finland using mostly handwritten documents
126
- from 17th, 18th, 19th and 20th centuries.
127
 
128
  Input image can be uploaded using the *Input image* window in the *Text content* tab, and the predicted text content will appear to the window
129
  on the right side of the image. Results of text region and text line detection can be viewed in the *Text regions* and *Text lines* tabs.
@@ -131,17 +132,17 @@ with gr.Blocks(theme=gr.themes.Monochrome(), title="Multicentury HTR Demo") as d
131
 
132
  Please note that this is a demo. 24/7 functionality is not quaranteed.
133
 
134
- # Monen vuosisadan käsialantunnistus malli
135
 
136
- Käsialantunnistus putkessa on kolme mallia: Tekstialueen tunnistus, tekstirivien tunnistus ja tekstintunnistus. Mallit on koulutettu pääosin
137
- käsinkirjoitetulla Kansallisarkiston aineistolla, joka ajoittuu 1600-luvulta 1900-luvulle.
138
 
139
  Tunnistettavan kuvan voi ladata *Input image* nimiseen laatikkoon *Text content* välilehdellä. Prosessointi käynnistetään *Process image*
140
- painikkeesta ja kuva on prosessoitu tunnistettu teksti ilmaantuu oikeaan laatikkoon nimeltä *Predicted text content*. Tekstialueen ja
141
  tekstirivien tunnistuksia voi tarkastella *Text regions* ja *Text lines* välilehdiltä. Parhaimman lopputuloksen saa hyvälaatuisilla kuvilla,
142
  joissa on normaalin kirjan mukainen taitto.
143
 
144
- Huom! Tämä on demo sovellus. Ympärivuorokautista toimivuutta ei luvata.
145
  """)
146
 
147
  with gr.Tab("Text content"):
 
1
  from optimum.onnxruntime import ORTModelForVision2Seq
2
+ from huggingface_hub import login, snapshot_download
3
  from transformers import TrOCRProcessor
 
4
  import gradio as gr
5
  import numpy as np
6
  import onnxruntime
 
16
 
17
  LINE_MODEL_PATH = "Kansallisarkisto/multicentury-textline-detection"
18
  REGION_MODEL_PATH = "Kansallisarkisto/court-records-region-detection"
19
+
20
+ # Download repository to cache
21
+ TROCR_MODEL_PATH = snapshot_download(
22
+ repo_id="Kansallisarkisto/multicentury-htr-model-small-onnx"
23
+ )
24
 
25
  # Allowed source paths for input images
26
  ALLOWED_SOURCES = ('https://astia.narc.fi', '/tmp/gradio')
 
54
  """Initialize text recognition class."""
55
  try:
56
  recognizer = TextRecognition(
 
57
  model_path = TROCR_MODEL_PATH,
58
  device = 'cuda:0',
59
  half_precision = True,
 
81
  def get_text_predictions(image, segment_predictions, recognizer):
82
  """Collects text prediction data into dicts based on detected text regions."""
83
  img_lines = merge_lines(segment_predictions)
 
84
  # Process all lines of an image
85
+ texts = recognizer.process_lines(img_lines, image)
86
  return texts
87
 
88
  def is_allowed_source(file_path):
 
124
  gr.Markdown("# Multicentury HTR Demo")
125
  gr.Markdown("""The HTR pipeline contains three components: text region detection, textline detection and handwritten text recognition.
126
  The components run machine learning models that have been trained at the National Archives of Finland using mostly handwritten documents
127
+ from 16th, 17th, 18th, 19th and 20th centuries.
128
 
129
  Input image can be uploaded using the *Input image* window in the *Text content* tab, and the predicted text content will appear to the window
130
  on the right side of the image. Results of text region and text line detection can be viewed in the *Text regions* and *Text lines* tabs.
 
132
 
133
  Please note that this is a demo. 24/7 functionality is not quaranteed.
134
 
135
+ # Monen vuosisadan käsialantunnistusmalli
136
 
137
+ Käsialantunnistusputkessa on kolme mallia: Tekstialueen tunnistus, tekstirivien tunnistus ja tekstintunnistus. Mallit on koulutettu pääosin
138
+ käsinkirjoitetulla Kansallisarkiston aineistolla, joka ajoittuu 1500-luvulta 1900-luvulle.
139
 
140
  Tunnistettavan kuvan voi ladata *Input image* nimiseen laatikkoon *Text content* välilehdellä. Prosessointi käynnistetään *Process image*
141
+ painikkeesta, ja kun kuva on prosessoitu, tunnistettu teksti ilmaantuu oikeaan laatikkoon nimeltä *Predicted text content*. Tekstialueen ja
142
  tekstirivien tunnistuksia voi tarkastella *Text regions* ja *Text lines* välilehdiltä. Parhaimman lopputuloksen saa hyvälaatuisilla kuvilla,
143
  joissa on normaalin kirjan mukainen taitto.
144
 
145
+ Huom! Tämä on demosovellus. Ympärivuorokautista toimivuutta ei luvata.
146
  """)
147
 
148
  with gr.Tab("Text content"):