|
import gradio as gr |
|
import easyocr |
|
import re |
|
from PIL import Image |
|
|
|
|
|
reader = easyocr.Reader(['en', 'hi']) |
|
|
|
|
|
def extract_and_search_text(image, keyword): |
|
results = reader.readtext(image) |
|
extracted_text = " ".join([text for (_, text, _) in results]) |
|
|
|
if not keyword: |
|
return extracted_text, "Enter a keyword to search." |
|
|
|
|
|
highlighted_text = re.sub(f"({keyword})", r"<mark>\1</mark>", extracted_text, flags=re.IGNORECASE) |
|
|
|
if keyword.lower() in extracted_text.lower(): |
|
return extracted_text, highlighted_text |
|
else: |
|
return extracted_text, "No matches found for the keyword." |
|
|
|
|
|
iface = gr.Interface( |
|
fn=extract_and_search_text, |
|
inputs=[gr.Image(type="pil"), gr.Textbox(label="Enter keyword")], |
|
outputs=[gr.Textbox(label="Extracted Text"), gr.HTML(label="Search Results")], |
|
title="Image Text Extraction and Keyword Search using EasyOCR", |
|
description="Upload an image, extract the text, and search for a keyword within the extracted text." |
|
) |
|
|
|
|
|
iface.launch() |
|
|