|
import gradio as gr |
|
import pytesseract |
|
from PIL import Image |
|
|
|
|
|
|
|
|
|
def extract_text_from_image(image_path): |
|
img = Image.open(image_path) |
|
text = pytesseract.image_to_string(img, lang="eng+hin") |
|
return text |
|
|
|
def ocr_and_search(image, keyword): |
|
|
|
text = extract_text_from_image(image) |
|
|
|
|
|
if keyword.lower() in text.lower(): |
|
|
|
highlighted_text = text.replace( |
|
keyword, f'<span style="color: red; font-weight: bold;">{keyword}</span>' |
|
) |
|
else: |
|
highlighted_text = "Keyword not found" |
|
|
|
return text, highlighted_text |
|
|
|
|
|
app = gr.Interface( |
|
fn=ocr_and_search, |
|
inputs=[gr.Image(type="filepath"), gr.Textbox(label="Keyword")], |
|
outputs=[gr.Textbox(label="Extracted Text"), gr.HTML(label="Search Results")] |
|
) |
|
|
|
app.launch(share=True) |
|
|