minnuhh commited on
Commit
26e6db8
·
verified ·
1 Parent(s): 40007c0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pip install easyocr gradio
2
+
3
+ import easyocr
4
+ import gradio as gr
5
+ import re
6
+
7
+ reader = easyocr.Reader(['en', 'hi'])
8
+
9
+ def ocr_text(image):
10
+ """
11
+ Performs OCR on the given image and returns the extracted text.
12
+
13
+ Args:
14
+ image: The image to perform OCR on.
15
+
16
+ Returns:
17
+ The extracted text and the original image.
18
+ """
19
+ result = reader.readtext(image)
20
+ full_text = ' '.join([text for bbox, text, conf in result])
21
+ return full_text, image
22
+
23
+
24
+ def highlight_text(full_text, search_term):
25
+ """
26
+ Highlights the search term in the given text.
27
+
28
+ Args:
29
+ full_text: The text to search in.
30
+ search_term: The term to search for.
31
+
32
+ Returns:
33
+ The text with the search term highlighted.
34
+ """
35
+ if search_term:
36
+ highlighted_text = highlighted_text = re.sub(f"({search_term})", r"<mark>\1</mark>", full_text, flags=re.IGNORECASE)
37
+ return highlighted_text
38
+ return
39
+ with gr.Blocks() as demo:
40
+ image_input = gr.Image(type="filepath")
41
+ extracted_text = gr.Textbox(lines=5, placeholder="Extracted text will appear here...")
42
+ ocr_button = gr.Button("Extract Text")
43
+ ocr_button.click(fn=ocr_text, inputs=image_input, outputs=[extracted_text, image_input])
44
+ search_term = gr.Textbox(lines=1, placeholder="Enter search term")
45
+ highlighted_text = gr.HTML()
46
+
47
+ search_button = gr.Button("Search")
48
+ search_button.click(fn=highlight_text, inputs=[extracted_text, search_term], outputs=highlighted_text)
49
+
50
+ demo.launch(share=True)