|
import easyocr |
|
import gradio as gr |
|
import re |
|
|
|
|
|
reader = easyocr.Reader(['en', 'hi']) |
|
|
|
|
|
def process_image(image, keyword): |
|
|
|
result = reader.readtext(image, detail=0) |
|
extracted_text = " ".join(result) |
|
|
|
|
|
highlight_color = "#87CEEB" |
|
if keyword: |
|
highlighted_text = re.sub(f"({re.escape(keyword)})", |
|
f"<mark style='background-color: {highlight_color};'>{keyword}</mark>", |
|
extracted_text, |
|
flags=re.IGNORECASE) |
|
else: |
|
highlighted_text = extracted_text |
|
|
|
|
|
if keyword and keyword.lower() in extracted_text.lower(): |
|
return f"Keyword '{keyword}' found in the text.", highlighted_text |
|
else: |
|
return f"Keyword '{keyword}' not found.", highlighted_text |
|
|
|
|
|
interface = gr.Interface( |
|
fn=process_image, |
|
inputs=["image", "text"], |
|
outputs=["text", "html"], |
|
title="OCR and Document Search with Highlighting", |
|
description="Upload an image, extract text, and search for keywords with highlighting." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch() |
|
|