import gradio as gr import easyocr import re from PIL import Image # Initialize EasyOCR Reader reader = easyocr.Reader(['en', 'hi']) # Function to extract text and search for a keyword 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." # Highlight the matching keyword highlighted_text = re.sub(f"({keyword})", r"\1", 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." # Gradio interface with keyword input 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." ) # Launch the Gradio interface iface.launch()