Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,41 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import easyocr
|
|
|
|
| 3 |
import re
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
-
# Initialize EasyOCR
|
| 7 |
reader = easyocr.Reader(['en', 'hi'])
|
| 8 |
|
| 9 |
-
# Function
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
-
return
|
| 24 |
|
| 25 |
-
# Gradio interface
|
| 26 |
-
|
| 27 |
-
fn=
|
| 28 |
-
inputs=[
|
| 29 |
-
outputs=[
|
| 30 |
-
title="
|
| 31 |
-
description="Upload an image, extract
|
| 32 |
)
|
| 33 |
|
| 34 |
-
# Launch the
|
| 35 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import easyocr
|
| 2 |
+
import gradio as gr
|
| 3 |
import re
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize EasyOCR reader
|
| 6 |
reader = easyocr.Reader(['en', 'hi'])
|
| 7 |
|
| 8 |
+
# Function for OCR and search functionality
|
| 9 |
+
def process_image(image, keyword):
|
| 10 |
+
# Perform OCR on the image
|
| 11 |
+
result = reader.readtext(image, detail=0)
|
| 12 |
+
extracted_text = " ".join(result)
|
| 13 |
+
|
| 14 |
+
# Highlight the keyword in the extracted text
|
| 15 |
+
highlight_color = "#87CEEB" # Soft Sky Blue
|
| 16 |
+
if keyword:
|
| 17 |
+
highlighted_text = re.sub(f"({re.escape(keyword)})",
|
| 18 |
+
f"<mark style='background-color: {highlight_color};'>{keyword}</mark>",
|
| 19 |
+
extracted_text,
|
| 20 |
+
flags=re.IGNORECASE)
|
| 21 |
+
else:
|
| 22 |
+
highlighted_text = extracted_text
|
| 23 |
+
|
| 24 |
+
# Check if the keyword is in the text
|
| 25 |
+
if keyword and keyword.lower() in extracted_text.lower():
|
| 26 |
+
return f"Keyword '{keyword}' found in the text.", highlighted_text
|
| 27 |
else:
|
| 28 |
+
return f"Keyword '{keyword}' not found.", highlighted_text
|
| 29 |
|
| 30 |
+
# Gradio interface
|
| 31 |
+
interface = gr.Interface(
|
| 32 |
+
fn=process_image,
|
| 33 |
+
inputs=["image", "text"],
|
| 34 |
+
outputs=["text", "html"],
|
| 35 |
+
title="OCR and Document Search with Highlighting",
|
| 36 |
+
description="Upload an image, extract text, and search for keywords with highlighting."
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Launch the app
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
interface.launch()
|