baudm commited on
Commit
596254c
·
1 Parent(s): 4900d6a

Show raw output with confidence values + fix layout

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -23,7 +23,7 @@ import gradio as gr
23
 
24
  class App:
25
 
26
- title = 'Scene Text Recognition with Permuted Autoregressive Sequence Models'
27
  models = ['parseq', 'parseq_tiny', 'abinet', 'crnn', 'trba', 'vitstr']
28
 
29
  def __init__(self):
@@ -49,19 +49,22 @@ class App:
49
  image = self._preprocess(image.convert('RGB')).unsqueeze(0)
50
  # Greedy decoding
51
  pred = model(image).softmax(-1)
52
- label, confidence = model.tokenizer.decode(pred)
53
- return label[0]
 
 
 
 
54
 
55
 
56
  def main():
57
-
58
  app = App()
59
 
60
- with gr.Blocks(analytics_enabled=False, title=app.title) as demo:
61
- gr.Markdown("""
62
  <div align="center">
63
 
64
- # Scene Text Recognition with<br/>Permuted Autoregressive Sequence Models
65
  [![GitHub](https://img.shields.io/badge/baudm-parseq-blue?logo=github)](https://github.com/baudm/parseq)
66
 
67
  </div>
@@ -71,20 +74,22 @@ def main():
71
  2. Upload your own image, choose from the examples below, or draw on the canvas.
72
  3. Click **Read Image** or **Read Drawing**.
73
  """)
74
- model_name = gr.Radio(app.models, value=app.models[0], label='Select STR model to use')
75
- with gr.Row():
76
- image_upload = gr.Image(type='pil', source='upload', label='Image')
77
- image_canvas = gr.Image(type='pil', source='canvas', label='Drawing')
78
  with gr.Row():
79
- read_upload = gr.Button('Read Image')
80
- read_canvas = gr.Button('Read Drawing')
 
 
 
 
81
 
82
  output = gr.Textbox(max_lines=1, label='Model output')
 
83
 
84
  gr.Examples(glob.glob('demo_images/*.*'), inputs=image_upload)
85
 
86
- read_upload.click(app, inputs=[model_name, image_upload], outputs=output)
87
- read_canvas.click(app, inputs=[model_name, image_canvas], outputs=output)
88
 
89
  demo.launch()
90
 
 
23
 
24
  class App:
25
 
26
+ title = 'Scene Text Recognition with<br/>Permuted Autoregressive Sequence Models'
27
  models = ['parseq', 'parseq_tiny', 'abinet', 'crnn', 'trba', 'vitstr']
28
 
29
  def __init__(self):
 
49
  image = self._preprocess(image.convert('RGB')).unsqueeze(0)
50
  # Greedy decoding
51
  pred = model(image).softmax(-1)
52
+ label, _ = model.tokenizer.decode(pred)
53
+ raw_label, raw_confidence = model.tokenizer.decode(pred, raw=True)
54
+ # Format confidence values
55
+ max_len = 25 if model_name == 'crnn' else len(label[0]) + 1
56
+ conf = list(map('{:0.1f}'.format, raw_confidence[0][:max_len].tolist()))
57
+ return label[0], [raw_label[0][:max_len], conf]
58
 
59
 
60
  def main():
 
61
  app = App()
62
 
63
+ with gr.Blocks(analytics_enabled=False, title=app.title.replace('<br/>', ' ')) as demo:
64
+ gr.Markdown(f"""
65
  <div align="center">
66
 
67
+ # {app.title}
68
  [![GitHub](https://img.shields.io/badge/baudm-parseq-blue?logo=github)](https://github.com/baudm/parseq)
69
 
70
  </div>
 
74
  2. Upload your own image, choose from the examples below, or draw on the canvas.
75
  3. Click **Read Image** or **Read Drawing**.
76
  """)
77
+ model_name = gr.Radio(app.models, value=app.models[0], label='The STR model to use')
 
 
 
78
  with gr.Row():
79
+ with gr.Column():
80
+ image_upload = gr.Image(type='pil', source='upload', label='Image')
81
+ read_upload = gr.Button('Read Image')
82
+ with gr.Column():
83
+ image_canvas = gr.Image(type='pil', source='canvas', label='Drawing')
84
+ read_canvas = gr.Button('Read Drawing')
85
 
86
  output = gr.Textbox(max_lines=1, label='Model output')
87
+ raw_output = gr.Dataframe(row_count=2, col_count=0, label='Raw output with confidence values (interval: [0, 1], [B]: BOS or BLANK token, [E]: EOS token)')
88
 
89
  gr.Examples(glob.glob('demo_images/*.*'), inputs=image_upload)
90
 
91
+ read_upload.click(app, inputs=[model_name, image_upload], outputs=[output, raw_output])
92
+ read_canvas.click(app, inputs=[model_name, image_canvas], outputs=[output, raw_output])
93
 
94
  demo.launch()
95