saidivyesh commited on
Commit
2607270
·
verified ·
1 Parent(s): 1139f22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,16 +1,17 @@
1
  import gradio as gr
2
  import easyocr
3
  import re
 
 
4
 
5
  # Initialize the EasyOCR reader for Hindi and English
6
  reader = easyocr.Reader(['hi', 'en'])
7
 
8
- # Step 1: Function to extract text using EasyOCR for both Hindi and English
9
- import numpy as np
10
-
11
- # Step 1: Function to extract text using EasyOCR for both Hindi and English with error handling
12
  def extract_text(image):
13
  try:
 
 
14
  # Convert PIL image to NumPy array
15
  image_np = np.array(image)
16
  # Extract text using EasyOCR
@@ -21,9 +22,7 @@ def extract_text(image):
21
  # Return the error message if an exception occurs
22
  return f"Error occurred: {str(e)}"
23
 
24
-
25
-
26
- # Step 2: Function to search and highlight the keyword in the extracted text
27
  def search_keyword(extracted_text, keyword):
28
  if keyword.lower() in extracted_text.lower():
29
  # Highlight the keyword in the text using re for case-insensitive replacement
@@ -47,10 +46,10 @@ with gr.Blocks() as demo:
47
 
48
  highlighted_output = gr.Textbox(label="Result", interactive=False)
49
 
50
- # Step 1: When the user clicks the extract button, extract the text
51
  extract_button.click(fn=extract_text, inputs=image_input, outputs=extracted_text_output)
52
 
53
- # Step 2: When the user clicks the search button, search for the keyword
54
  search_button.click(fn=search_keyword, inputs=[extracted_text_output, keyword_input], outputs=highlighted_output)
55
 
56
  # Launch the web app with sharing enabled
 
1
  import gradio as gr
2
  import easyocr
3
  import re
4
+ from PIL import Image
5
+ import numpy as np
6
 
7
  # Initialize the EasyOCR reader for Hindi and English
8
  reader = easyocr.Reader(['hi', 'en'])
9
 
10
+ # Function to extract text using EasyOCR for both Hindi and English with error handling
 
 
 
11
  def extract_text(image):
12
  try:
13
+ # Resize the image to speed up processing (optional)
14
+ image = image.resize((800, 800)) # Resize to 800x800 pixels
15
  # Convert PIL image to NumPy array
16
  image_np = np.array(image)
17
  # Extract text using EasyOCR
 
22
  # Return the error message if an exception occurs
23
  return f"Error occurred: {str(e)}"
24
 
25
+ # Function to search and highlight the keyword in the extracted text
 
 
26
  def search_keyword(extracted_text, keyword):
27
  if keyword.lower() in extracted_text.lower():
28
  # Highlight the keyword in the text using re for case-insensitive replacement
 
46
 
47
  highlighted_output = gr.Textbox(label="Result", interactive=False)
48
 
49
+ # When the user clicks the extract button, extract the text
50
  extract_button.click(fn=extract_text, inputs=image_input, outputs=extracted_text_output)
51
 
52
+ # When the user clicks the search button, search for the keyword
53
  search_button.click(fn=search_keyword, inputs=[extracted_text_output, keyword_input], outputs=highlighted_output)
54
 
55
  # Launch the web app with sharing enabled