File size: 1,113 Bytes
66f6eb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
import gradio as gr
import cv2
import easyocr

# Initialize EasyOCR Reader
reader = easyocr.Reader(['en', 'hi'])

# Function to process the image
def preprocess_image(image):
    # Convert the image to BGR format for EasyOCR
    return cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

def ocr_and_search(image, keyword):
    processed_image = preprocess_image(image)

    # Extract text in both Hindi and English
    results = reader.readtext(processed_image)

    # Combine extracted text into a single string
    extracted_text = "\n".join([result[1] for result in results])

    # Search for the keyword in the extracted text (case insensitive)
    search_results = [line for line in extracted_text.split('\n') if keyword.lower() in line.lower()]
    
    return extracted_text, search_results

# Create Gradio interface
iface = gr.Interface(
    fn=ocr_and_search,
    inputs=[gr.Image(type="numpy"), gr.Textbox(label="Keyword")],
    outputs=["text", "text"],
    title="OCR and Keyword Search",
    description="Upload an image with text and search for a keyword."
)

# Launch the interface
iface.launch(share=True)