Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -22,30 +22,33 @@ if uploaded_file is not None:
|
|
22 |
# Display the image
|
23 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
st.subheader("Extracted Text:")
|
29 |
-
st.write(text)
|
30 |
-
|
31 |
-
# Search functionality
|
32 |
-
search_keyword = st.text_input("Enter a keyword to search in the extracted text:")
|
33 |
-
if search_keyword:
|
34 |
-
pattern = re.compile(re.escape(search_keyword), re.IGNORECASE)
|
35 |
-
matches = list(pattern.finditer(text))
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)}")
|