Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
import streamlit as st
|
2 |
import easyocr
|
|
|
3 |
from PIL import Image
|
4 |
import re
|
|
|
5 |
|
6 |
# Initialize EasyOCR Reader
|
7 |
reader = easyocr.Reader(['en', 'hi'])
|
@@ -9,36 +11,41 @@ reader = easyocr.Reader(['en', 'hi'])
|
|
9 |
# Streamlit app title
|
10 |
st.title("Image Text Extraction and Keyword Search using EasyOCR")
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
|
15 |
-
if
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
else:
|
42 |
-
st.
|
43 |
-
|
44 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
import easyocr
|
3 |
+
import requests
|
4 |
from PIL import Image
|
5 |
import re
|
6 |
+
from io import BytesIO
|
7 |
|
8 |
# Initialize EasyOCR Reader
|
9 |
reader = easyocr.Reader(['en', 'hi'])
|
|
|
11 |
# Streamlit app title
|
12 |
st.title("Image Text Extraction and Keyword Search using EasyOCR")
|
13 |
|
14 |
+
# Input for image URL
|
15 |
+
image_url = st.text_input("Enter the image URL:")
|
16 |
|
17 |
+
if image_url:
|
18 |
+
try:
|
19 |
+
# Fetch the image from the URL
|
20 |
+
response = requests.get(image_url)
|
21 |
+
image = Image.open(BytesIO(response.content))
|
22 |
+
|
23 |
+
# Display the image
|
24 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
25 |
+
|
26 |
+
# Perform OCR
|
27 |
+
with st.spinner("Extracting text..."):
|
28 |
+
results = reader.readtext(image)
|
29 |
+
|
30 |
+
# Extract the text
|
31 |
+
extracted_text = " ".join([text for (_, text, _) in results])
|
32 |
+
|
33 |
+
if extracted_text:
|
34 |
+
st.success("Extracted Text:")
|
35 |
+
st.write(extracted_text)
|
36 |
+
|
37 |
+
# Keyword search feature
|
38 |
+
keyword = st.text_input("Enter a keyword to search in the extracted text:")
|
39 |
+
|
40 |
+
if keyword:
|
41 |
+
# Highlight matches
|
42 |
+
highlighted_text = re.sub(f"({keyword})", r"<mark>\1</mark>", extracted_text, flags=re.IGNORECASE)
|
43 |
+
st.markdown(f"**Search Results for '{keyword}':**", unsafe_allow_html=True)
|
44 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
45 |
+
else:
|
46 |
+
st.info("Enter a keyword to search.")
|
47 |
else:
|
48 |
+
st.warning("No text detected in the image.")
|
49 |
+
|
50 |
+
except Exception as e:
|
51 |
+
st.error("Error fetching or processing the image. Please check the URL.")
|