Added keyword highlighter
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import easyocr
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
reader = easyocr.Reader(['en', 'hi'], gpu=False)
|
| 7 |
|
|
@@ -30,10 +31,19 @@ def extract_text(image_path):
|
|
| 30 |
|
| 31 |
def search_text(image_path, keyword):
|
| 32 |
extracted_text = extract_text(image_path)
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
-
return f"Keyword '{keyword}' not found in the extracted text.",
|
| 37 |
|
| 38 |
def create_interface():
|
| 39 |
interface = gr.Interface(
|
|
@@ -44,7 +54,7 @@ def create_interface():
|
|
| 44 |
],
|
| 45 |
outputs=[
|
| 46 |
gr.Textbox(label="Search Result"),
|
| 47 |
-
gr.
|
| 48 |
],
|
| 49 |
title="OCR and Keyword Search Application",
|
| 50 |
description="Upload an image containing text in English or Hindi. Enter a keyword to search within the extracted text.",
|
|
|
|
| 2 |
import easyocr
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
reader = easyocr.Reader(['en', 'hi'], gpu=False)
|
| 8 |
|
|
|
|
| 31 |
|
| 32 |
def search_text(image_path, keyword):
|
| 33 |
extracted_text = extract_text(image_path)
|
| 34 |
+
|
| 35 |
+
escaped_keyword = re.escape(keyword);
|
| 36 |
+
highlighted_text = re.sub(
|
| 37 |
+
f'({escaped_keyword})',
|
| 38 |
+
r'<mark style="background-color: lightblue; color: black;">\1</mark>',
|
| 39 |
+
extracted_text,
|
| 40 |
+
flags=re.IGNORECASE
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if re.search(escaped_keyword, extracted_text, re.IGNORECASE):
|
| 44 |
+
return f"Keyword '{keyword}' found in the extracted text.", highlighted_text
|
| 45 |
else:
|
| 46 |
+
return f"Keyword '{keyword}' not found in the extracted text.", highlighted_text
|
| 47 |
|
| 48 |
def create_interface():
|
| 49 |
interface = gr.Interface(
|
|
|
|
| 54 |
],
|
| 55 |
outputs=[
|
| 56 |
gr.Textbox(label="Search Result"),
|
| 57 |
+
gr.HTML(label="Extracted Text")
|
| 58 |
],
|
| 59 |
title="OCR and Keyword Search Application",
|
| 60 |
description="Upload an image containing text in English or Hindi. Enter a keyword to search within the extracted text.",
|