Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,37 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import easyocr
|
| 4 |
+
|
| 5 |
+
# Initialize EasyOCR Reader
|
| 6 |
+
reader = easyocr.Reader(['en', 'hi'])
|
| 7 |
+
|
| 8 |
+
# Function to process the image
|
| 9 |
+
def preprocess_image(image):
|
| 10 |
+
# Convert the image to BGR format for EasyOCR
|
| 11 |
+
return cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 12 |
+
|
| 13 |
+
def ocr_and_search(image, keyword):
|
| 14 |
+
processed_image = preprocess_image(image)
|
| 15 |
+
|
| 16 |
+
# Extract text in both Hindi and English
|
| 17 |
+
results = reader.readtext(processed_image)
|
| 18 |
+
|
| 19 |
+
# Combine extracted text into a single string
|
| 20 |
+
extracted_text = "\n".join([result[1] for result in results])
|
| 21 |
+
|
| 22 |
+
# Search for the keyword in the extracted text (case insensitive)
|
| 23 |
+
search_results = [line for line in extracted_text.split('\n') if keyword.lower() in line.lower()]
|
| 24 |
+
|
| 25 |
+
return extracted_text, search_results
|
| 26 |
+
|
| 27 |
+
# Create Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=ocr_and_search,
|
| 30 |
+
inputs=[gr.Image(type="numpy"), gr.Textbox(label="Keyword")],
|
| 31 |
+
outputs=["text", "text"],
|
| 32 |
+
title="OCR and Keyword Search",
|
| 33 |
+
description="Upload an image with text and search for a keyword."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the interface
|
| 37 |
+
iface.launch(share=True)
|