from typing import List import pytesseract from PIL import Image import gradio as gr # def tesseract_ocr(filepath: str, languages: List[str]=None, psm: int = 7): # image = Image.open(filepath) # return pytesseract.image_to_string(image=image, lang=', '.join(languages) if languages else None, config='--psm 7') # return pytesseract.image_to_string(image=image, lang=', '.join(languages) if languages else None, config='--psm ' + str(psm)) def tesseract_ocr(filepath: str, languages: List[str] = None, psm: int = 7): try: image = Image.open(filepath) lang = ','.join(languages) if languages else None config = f'--psm {psm}' return pytesseract.image_to_string(image, lang=lang, config=config) except Exception as e: return f"Error: {str(e)}" title = "Tesseract OCR" description = "Gradio demo for Tesseract. Tesseract is an open source text recognition (OCR) Engine." article = "

Tesseract documentation | Github Repo

" examples = [ ["examples/weird_unicode_math_symbols.png", []], ["examples/eurotext.png", ["eng"]], ["examples/tesseract_sample.png", ["jpn", "eng"]], ["examples/chi.jpg", ["HanS", "HanT"]], ] with gr.Blocks(title=title) as demo: gr.Markdown(f'

{title}

') gr.Markdown(description) with gr.Row(): with gr.Column(): image = gr.Image(type="filepath", label="Input") language_choices = pytesseract.get_languages() with gr.Accordion("Languages", open=False): languages = gr.CheckboxGroup(language_choices, type="value", value=["eng"], label='language') psm_slider = gr.Slider(minimum=0, maximum=13, step=1, value=7, label="PSM level") 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, psm_slider], outputs=text, api_name="tesseract-ocr") btn_clear.add(text) gr.Examples( examples=examples, inputs=[image, languages], ) gr.Markdown(article) if __name__ == '__main__': demo.launch()