|
import streamlit as st |
|
import easyocr |
|
import requests |
|
from PIL import Image |
|
import re |
|
from io import BytesIO |
|
|
|
|
|
reader = easyocr.Reader(['en', 'hi']) |
|
|
|
|
|
st.title("Image Text Extraction and Keyword Search using EasyOCR") |
|
|
|
|
|
image_url = st.text_input("Enter the image URL:") |
|
|
|
if image_url: |
|
try: |
|
|
|
response = requests.get(image_url) |
|
image = Image.open(BytesIO(response.content)) |
|
|
|
|
|
st.image(image, caption='Uploaded Image', use_column_width=True) |
|
|
|
|
|
with st.spinner("Extracting text..."): |
|
results = reader.readtext(image) |
|
|
|
|
|
extracted_text = " ".join([text for (_, text, _) in results]) |
|
|
|
if extracted_text: |
|
st.success("Extracted Text:") |
|
st.write(extracted_text) |
|
|
|
|
|
keyword = st.text_input("Enter a keyword to search in the extracted text:") |
|
|
|
if keyword: |
|
|
|
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.") |
|
|