File size: 1,212 Bytes
e9ef579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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"<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."

# 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()