Ocr / app.py
Rick7799's picture
Create app.py
cec2f8f verified
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