Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import easyocr
|
| 3 |
+
import re
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Initialize EasyOCR Reader
|
| 7 |
+
reader = easyocr.Reader(['en', 'hi'])
|
| 8 |
+
|
| 9 |
+
# Function to extract text and search for a keyword
|
| 10 |
+
def extract_and_search_text(image, keyword):
|
| 11 |
+
results = reader.readtext(image)
|
| 12 |
+
extracted_text = " ".join([text for (_, text, _) in results])
|
| 13 |
+
|
| 14 |
+
if not keyword:
|
| 15 |
+
return extracted_text, "Enter a keyword to search."
|
| 16 |
+
|
| 17 |
+
# Highlight the matching keyword
|
| 18 |
+
highlighted_text = re.sub(f"({keyword})", r"<mark>\1</mark>", extracted_text, flags=re.IGNORECASE)
|
| 19 |
+
|
| 20 |
+
if keyword.lower() in extracted_text.lower():
|
| 21 |
+
return extracted_text, highlighted_text
|
| 22 |
+
else:
|
| 23 |
+
return extracted_text, "No matches found for the keyword."
|
| 24 |
+
|
| 25 |
+
# Gradio interface with keyword input
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=extract_and_search_text,
|
| 28 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Enter keyword")],
|
| 29 |
+
outputs=[gr.Textbox(label="Extracted Text"), gr.HTML(label="Search Results")],
|
| 30 |
+
title="Image Text Extraction and Keyword Search using EasyOCR",
|
| 31 |
+
description="Upload an image, extract the text, and search for a keyword within the extracted text."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the Gradio interface
|
| 35 |
+
iface.launch()
|