Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,54 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import re
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# Streamlit app layout
|
| 57 |
-
st.title("Image Text Search App")
|
| 58 |
-
|
| 59 |
-
uploaded_file = st.file_uploader("Upload an Image (JPG or PNG)", type=["jpg", "jpeg", "png"])
|
| 60 |
-
|
| 61 |
-
if uploaded_file is not None:
|
| 62 |
-
image_bytes = uploaded_file.read()
|
| 63 |
-
st.image(image_bytes)
|
| 64 |
-
|
| 65 |
-
# Perform OCR
|
| 66 |
-
extracted_text = process_image(image_bytes)
|
| 67 |
-
st.write("Extracted Text:")
|
| 68 |
-
st.write(extracted_text)
|
| 69 |
-
|
| 70 |
-
# Search functionality
|
| 71 |
-
search_keyword = st.text_input("Enter a keyword to search:")
|
| 72 |
-
if search_keyword:
|
| 73 |
-
results, highlighted_text = search_and_highlight(extracted_text, search_keyword)
|
| 74 |
-
if results:
|
| 75 |
-
st.write(f"Keyword '{search_keyword}' found in the extracted text:")
|
| 76 |
-
for i, result in enumerate(results, 1):
|
| 77 |
-
st.write(f"{i}. ...{result}...")
|
| 78 |
-
else:
|
| 79 |
-
st.write(f"Keyword '{search_keyword}' not found in the extracted text.")
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytesseract
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
# Set the title of the webpage
|
| 10 |
+
st.title("OCR Text Extraction Tool")
|
| 11 |
+
|
| 12 |
+
# Uploading an image
|
| 13 |
+
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
| 14 |
+
|
| 15 |
+
if uploaded_file is not None:
|
| 16 |
+
# Convert the uploaded file content to an image
|
| 17 |
+
image = Image.open(uploaded_file)
|
| 18 |
+
|
| 19 |
+
# Convert PIL Image to OpenCV format
|
| 20 |
+
opencv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 21 |
+
|
| 22 |
+
# Display the image
|
| 23 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Perform OCR
|
| 27 |
+
text = pytesseract.image_to_string(opencv_image)
|
| 28 |
+
|
| 29 |
+
st.subheader("Extracted Text:")
|
| 30 |
+
st.write(text)
|
| 31 |
+
|
| 32 |
+
# Search functionality
|
| 33 |
+
search_keyword = st.text_input("Enter a keyword to search in the extracted text:")
|
| 34 |
+
if search_keyword:
|
| 35 |
+
pattern = re.compile(re.escape(search_keyword), re.IGNORECASE)
|
| 36 |
+
matches = list(pattern.finditer(text))
|
| 37 |
+
|
| 38 |
+
if matches:
|
| 39 |
+
st.markdown("### Keyword Found:")
|
| 40 |
+
for match in matches:
|
| 41 |
+
start, end = match.span()
|
| 42 |
+
context_start = max(0, start - 50)
|
| 43 |
+
context_end = min(len(text), end + 50)
|
| 44 |
+
context = text[context_start:context_end]
|
| 45 |
+
highlighted_text = (
|
| 46 |
+
context[:start-context_start] +
|
| 47 |
+
f"<span style='background-color: yellow;'>{context[start-context_start:end-context_start]}</span>" +
|
| 48 |
+
context[end-context_start:]
|
| 49 |
+
)
|
| 50 |
+
st.markdown(f"...{highlighted_text}...")
|
| 51 |
+
else:
|
| 52 |
+
st.write(f"Keyword '{search_keyword}' not found in the extracted text.")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
st.error(f"An error occurred while processing the image: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|