Spaces:
Sleeping
Sleeping
gaurav2004
commited on
Commit
•
bf9b80f
1
Parent(s):
40fd7eb
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
3 |
+
from PIL import Image
|
4 |
+
import pytesseract
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Function to extract text and search for a keyword
|
8 |
+
def ocr_with_search(image, keyword):
|
9 |
+
# Extract text using OCR (supports Hindi and English)
|
10 |
+
|
11 |
+
text = pytesseract.image_to_string(image, lang='hin+eng')
|
12 |
+
|
13 |
+
# Split text into lines and search for keyword
|
14 |
+
matched_lines = [line for line in text.split('\n') if keyword.lower() in line.lower()]
|
15 |
+
|
16 |
+
# Return extracted text and matching lines
|
17 |
+
return text, '\n'.join(matched_lines)
|
18 |
+
|
19 |
+
# Gradio interface to upload image and search for keywords
|
20 |
+
def create_interface():
|
21 |
+
# Create Gradio interface
|
22 |
+
iface = gr.Interface(
|
23 |
+
fn=ocr_with_search, # Function for OCR and search
|
24 |
+
inputs=[
|
25 |
+
gr.Image(type="pil", label="Upload Image"), # Image upload
|
26 |
+
gr.Textbox(placeholder="Enter keyword to search", label="Keyword Search") # Keyword search input
|
27 |
+
],
|
28 |
+
outputs=[
|
29 |
+
gr.Textbox(label="Extracted Text"), # Full extracted text
|
30 |
+
gr.Textbox(label="Search Results") # Matching keyword results
|
31 |
+
],
|
32 |
+
title="Hindi and English OCR with Keyword Search",
|
33 |
+
description="Upload an image containing Hindi and English text. Extract the text and search for keywords."
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the interface
|
37 |
+
iface.launch(share=True)
|
38 |
+
|
39 |
+
# Run the Gradio interface
|
40 |
+
if __name__ == "__main__":
|
41 |
+
create_interface()
|