|
import gradio as gr |
|
import pytesseract |
|
from PIL import Image |
|
|
|
|
|
def extract_text(image): |
|
text = pytesseract.image_to_string(image, lang='eng+hin') |
|
return text |
|
|
|
|
|
def ocr_and_search(image, keyword): |
|
extracted_text = extract_text(image) |
|
if keyword: |
|
|
|
matching_lines = [line for line in extracted_text.splitlines() if keyword.lower() in line.lower()] |
|
return extracted_text, matching_lines |
|
else: |
|
return extracted_text, [] |
|
|
|
|
|
interface = gr.Interface( |
|
fn=ocr_and_search, |
|
inputs=[ |
|
gr.Image(type="pil", label="Upload Image"), |
|
gr.Textbox(label="Enter Keyword") |
|
], |
|
outputs=[ |
|
gr.Textbox(label="Extracted Text"), |
|
gr.Textbox(label="Matching Lines") |
|
], |
|
title="OCR with Keyword Search", |
|
description="Upload an image and enter a keyword to search within the extracted text." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
interface.launch(share=True) |