ocr-with-teserract / app_blocks.py
avimittal30's picture
secc
c2510e6
from typing import List
import pytesseract
from PIL import Image
import gradio as gr
def tesseract_ocr(filepath: str, languages: List[str] = None):
if not languages:
languages = ['eng']
image = Image.open(filepath)
lang_param = '+'.join(languages)
return pytesseract.image_to_string(image=image, lang=lang_param)
title = "Tesseract OCR"
description = "Gradio demo for Tesseract. Tesseract is an open source text recognition (OCR) Engine."
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>"
examples = [
["examples/weird_unicode_math_symbols.png", []],
["examples/eurotext.png", ["eng"]],
["examples/tesseract_sample.png", ["jpn", "eng"]],
["examples/chi.jpg", ["chi_sim", "chi_tra"]],
]
with gr.Blocks(title=title) as demo:
gr.Markdown(f'<h1 style="text-align: center; margin-bottom: 1rem;">{title}</h1>')
gr.Markdown(description)
with gr.Row():
with gr.Column():
image = gr.Image(type="filepath", label="Input")
language_choices = pytesseract.get_languages(config='') # <- safer
with gr.Accordion("Languages", open=False):
languages = gr.CheckboxGroup(language_choices, value=["eng"], label="Language(s)")
with gr.Row():
btn_clear = gr.ClearButton([image, languages])
btn_submit = gr.Button(value="Submit", variant="primary")
with gr.Column():
text = gr.Textbox(label="Output")
btn_submit.click(tesseract_ocr, inputs=[image, languages], outputs=text)
btn_clear.add(text)
gr.Examples(
examples=examples,
inputs=[image, languages],
outputs=[text],
fn=tesseract_ocr
)
gr.Markdown(article)
if __name__ == '__main__':
demo.queue().launch()