Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import easyocr
|
3 |
+
import re
|
4 |
+
|
5 |
+
|
6 |
+
r = easyocr.Reader(['en', 'hi'])
|
7 |
+
|
8 |
+
def process_image(image, keyword):
|
9 |
+
|
10 |
+
result = r.readtext(image, detail=0)
|
11 |
+
extracted_text = " ".join(result)
|
12 |
+
|
13 |
+
|
14 |
+
color = "#228B22"
|
15 |
+
if keyword:
|
16 |
+
highlighted_text = re.sub(f"({re.escape(keyword)})",
|
17 |
+
f"<mark style='background-color: {color};'>{keyword}</mark>",
|
18 |
+
extracted_text,
|
19 |
+
flags=re.IGNORECASE)
|
20 |
+
else:
|
21 |
+
highlighted_text = extracted_text
|
22 |
+
|
23 |
+
|
24 |
+
if keyword and keyword.lower() in extracted_text.lower():
|
25 |
+
return f"Keyword '{keyword}' found in the text.", highlighted_text
|
26 |
+
else:
|
27 |
+
return f"Keyword '{keyword}' not found.", highlighted_text
|
28 |
+
|
29 |
+
# Gradio interface
|
30 |
+
interface = gr.Interface(
|
31 |
+
fn=process_image,
|
32 |
+
inputs=["image", "text"],
|
33 |
+
outputs=["text", "html"],
|
34 |
+
title=" π TextFinder OCR π",
|
35 |
+
description="Input your image, get the text π and search for keywords π.....:)"
|
36 |
+
)
|
37 |
+
|
38 |
+
# Launch the app
|
39 |
+
if __name__ == "__main__":
|
40 |
+
interface.launch()
|