File size: 1,497 Bytes
cec2f8f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import gradio as gr
import pytesseract
from PIL import Image
# Function to extract text from the uploaded image
def extract_text(image):
text = pytesseract.image_to_string(image, lang='eng+hin') # Extract text using Tesseract
return text
# Function to extract text and perform keyword search
def ocr_and_search(image, keyword):
extracted_text = extract_text(image) # Get the extracted text
if keyword:
# Search for the keyword in the extracted text
matching_lines = [line for line in extracted_text.splitlines() if keyword.lower() in line.lower()]
return extracted_text, matching_lines # Return both extracted text and matching lines
else:
return extracted_text, [] # If no keyword, return empty list for matches
# Create a Gradio interface
interface = gr.Interface(
fn=ocr_and_search,
inputs=[
gr.Image(type="pil", label="Upload Image"), # Input for image upload
gr.Textbox(label="Enter Keyword") # Input for keyword search
],
outputs=[
gr.Textbox(label="Extracted Text"), # Output for extracted text
gr.Textbox(label="Matching Lines") # Output for matching lines based on keyword search
],
title="OCR with Keyword Search",
description="Upload an image and enter a keyword to search within the extracted text."
)
# Launch the app with share=True to create a public link
if __name__ == "__main__":
interface.launch(share=True) # Set share=True to generate a public URL |