Spaces:
Running
Running
Aumkeshchy2003
commited on
Commit
•
4012737
1
Parent(s):
12d5872
Create app_block.py
Browse files- app_block.py +48 -0
app_block.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
import pytesseract
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
def tesseract_ocr(filepath: str, languages: List[str]=None):
|
9 |
+
image = Image.open(filepath)
|
10 |
+
return pytesseract.image_to_string(image=image, lang=', '.join(languages) if languages else None)
|
11 |
+
|
12 |
+
title = "Tesseract OCR"
|
13 |
+
description = "Gradio demo for Tesseract. Tesseract is an open source text recognition (OCR) Engine."
|
14 |
+
article = "<p style='text-align: center'><a href='https://tesseract-ocr.github.io/' target='_blank'>Tesseract documentation</a> | <a href='https://github.com/tesseract-ocr/tesseract' target='_blank'>Github Repo</a></p>"
|
15 |
+
examples = [
|
16 |
+
["examples/weird_unicode_math_symbols.png", []],
|
17 |
+
["examples/eurotext.png", ["eng"]],
|
18 |
+
["examples/tesseract_sample.png", ["jpn", "eng"]],
|
19 |
+
["examples/chi.jpg", ["HanS", "HanT"]],
|
20 |
+
]
|
21 |
+
|
22 |
+
with gr.Blocks(title=title) as demo:
|
23 |
+
gr.Markdown(f'<h1 style="text-align: center; margin-bottom: 1rem;">{title}</h1>')
|
24 |
+
gr.Markdown(description)
|
25 |
+
with gr.Row():
|
26 |
+
with gr.Column():
|
27 |
+
image = gr.Image(type="filepath", label="Input")
|
28 |
+
language_choices = pytesseract.get_languages()
|
29 |
+
with gr.Accordion("Languages", open=False):
|
30 |
+
languages = gr.CheckboxGroup(language_choices, type="value", value=["eng"], label='language')
|
31 |
+
with gr.Row():
|
32 |
+
btn_clear = gr.ClearButton([image, languages])
|
33 |
+
btn_submit = gr.Button(value="Submit", variant="primary")
|
34 |
+
with gr.Column():
|
35 |
+
text = gr.Textbox(label="Output")
|
36 |
+
|
37 |
+
btn_submit.click(tesseract_ocr, inputs=[image, languages], outputs=text, api_name="tesseract-ocr")
|
38 |
+
btn_clear.add(text)
|
39 |
+
|
40 |
+
gr.Examples(
|
41 |
+
examples=examples,
|
42 |
+
inputs=[image, languages],
|
43 |
+
)
|
44 |
+
|
45 |
+
gr.Markdown(article)
|
46 |
+
|
47 |
+
if __name__ == '__main__':
|
48 |
+
demo.launch()
|