Arch10 commited on
Commit
46b1d1d
·
verified ·
1 Parent(s): 87fc8ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -31
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
- # File uploader for image input
13
- uploaded_image = st.file_uploader("Upload an image", type=['png', 'jpg', 'jpeg'])
14
 
15
- if uploaded_image is not None:
16
- # Load the uploaded image
17
- image = Image.open(uploaded_image)
18
-
19
- # Display the image
20
- st.image(image, caption='Uploaded Image', use_column_width=True)
21
-
22
- # Perform OCR
23
- with st.spinner("Extracting text..."):
24
- results = reader.readtext(image)
25
-
26
- # Extract the text
27
- extracted_text = " ".join([text for (_, text, _) in results])
28
-
29
- if extracted_text:
30
- st.success("Extracted Text:")
31
- st.write(extracted_text)
32
-
33
- # Keyword search feature
34
- keyword = st.text_input("Enter a keyword to search in the extracted text:")
35
-
36
- if keyword:
37
- # Highlight matches
38
- highlighted_text = re.sub(f"({keyword})", r"<mark>\1</mark>", extracted_text, flags=re.IGNORECASE)
39
- st.markdown(f"**Search Results for '{keyword}':**", unsafe_allow_html=True)
40
- st.markdown(highlighted_text, unsafe_allow_html=True)
 
 
 
 
41
  else:
42
- st.info("Enter a keyword to search.")
43
- else:
44
- st.warning("No text detected in the image.")
 
 
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.")