Arch10 commited on
Commit
42bde06
·
verified ·
1 Parent(s): c1ccc22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -26
app.py CHANGED
@@ -1,35 +1,41 @@
1
- import gradio as gr
2
  import easyocr
 
3
  import re
4
- from PIL import Image
5
 
6
- # Initialize EasyOCR Reader
7
  reader = easyocr.Reader(['en', 'hi'])
8
 
9
- # Function to extract text and search for a keyword
10
- def extract_and_search_text(image, keyword):
11
- results = reader.readtext(image)
12
- extracted_text = " ".join([text for (_, text, _) in results])
13
-
14
- if not keyword:
15
- return extracted_text, "Enter a keyword to search."
16
-
17
- # Highlight the matching keyword
18
- highlighted_text = re.sub(f"({keyword})", r"<mark>\1</mark>", extracted_text, flags=re.IGNORECASE)
19
-
20
- if keyword.lower() in extracted_text.lower():
21
- return extracted_text, highlighted_text
 
 
 
 
 
 
22
  else:
23
- return extracted_text, "No matches found for the keyword."
24
 
25
- # Gradio interface with keyword input
26
- iface = gr.Interface(
27
- fn=extract_and_search_text,
28
- inputs=[gr.Image(type="pil"), gr.Textbox(label="Enter keyword")],
29
- outputs=[gr.Textbox(label="Extracted Text"), gr.HTML(label="Search Results")],
30
- title="Image Text Extraction and Keyword Search using EasyOCR",
31
- description="Upload an image, extract the text, and search for a keyword within the extracted text."
32
  )
33
 
34
- # Launch the Gradio interface
35
- iface.launch()
 
 
 
1
  import easyocr
2
+ import gradio as gr
3
  import re
 
4
 
5
+ # Initialize EasyOCR reader
6
  reader = easyocr.Reader(['en', 'hi'])
7
 
8
+ # Function for OCR and search functionality
9
+ def process_image(image, keyword):
10
+ # Perform OCR on the image
11
+ result = reader.readtext(image, detail=0)
12
+ extracted_text = " ".join(result)
13
+
14
+ # Highlight the keyword in the extracted text
15
+ highlight_color = "#87CEEB" # Soft Sky Blue
16
+ if keyword:
17
+ highlighted_text = re.sub(f"({re.escape(keyword)})",
18
+ f"<mark style='background-color: {highlight_color};'>{keyword}</mark>",
19
+ extracted_text,
20
+ flags=re.IGNORECASE)
21
+ else:
22
+ highlighted_text = extracted_text
23
+
24
+ # Check if the keyword is in the text
25
+ if keyword and keyword.lower() in extracted_text.lower():
26
+ return f"Keyword '{keyword}' found in the text.", highlighted_text
27
  else:
28
+ return f"Keyword '{keyword}' not found.", highlighted_text
29
 
30
+ # Gradio interface
31
+ interface = gr.Interface(
32
+ fn=process_image,
33
+ inputs=["image", "text"],
34
+ outputs=["text", "html"],
35
+ title="OCR and Document Search with Highlighting",
36
+ description="Upload an image, extract text, and search for keywords with highlighting."
37
  )
38
 
39
+ # Launch the app
40
+ if __name__ == "__main__":
41
+ interface.launch()