File size: 1,642 Bytes
28fd050
a041eb6
46b1d1d
28fd050
a041eb6
46b1d1d
28fd050
a041eb6
 
28fd050
a041eb6
 
28fd050
46b1d1d
 
28fd050
46b1d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a041eb6
46b1d1d
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
42
43
44
45
46
47
48
49
50
51
52
import streamlit as st
import easyocr
import requests
from PIL import Image
import re
from io import BytesIO

# Initialize EasyOCR Reader
reader = easyocr.Reader(['en', 'hi'])

# Streamlit app title
st.title("Image Text Extraction and Keyword Search using EasyOCR")

# Input for image URL
image_url = st.text_input("Enter the image URL:")

if image_url:
    try:
        # Fetch the image from the URL
        response = requests.get(image_url)
        image = Image.open(BytesIO(response.content))

        # Display the image
        st.image(image, caption='Uploaded Image', use_column_width=True)

        # Perform OCR
        with st.spinner("Extracting text..."):
            results = reader.readtext(image)

        # Extract the text
        extracted_text = " ".join([text for (_, text, _) in results])

        if extracted_text:
            st.success("Extracted Text:")
            st.write(extracted_text)

            # Keyword search feature
            keyword = st.text_input("Enter a keyword to search in the extracted text:")

            if keyword:
                # Highlight matches
                highlighted_text = re.sub(f"({keyword})", r"<mark>\1</mark>", extracted_text, flags=re.IGNORECASE)
                st.markdown(f"**Search Results for '{keyword}':**", unsafe_allow_html=True)
                st.markdown(highlighted_text, unsafe_allow_html=True)
            else:
                st.info("Enter a keyword to search.")
        else:
            st.warning("No text detected in the image.")
    
    except Exception as e:
        st.error("Error fetching or processing the image. Please check the URL.")