Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,43 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import numpy as np
|
5 |
-
import
|
6 |
-
|
7 |
-
import re
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Convert PIL Image to OpenCV format
|
20 |
-
opencv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
21 |
|
22 |
-
|
23 |
-
|
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 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
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)}")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import pytesseract
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
+
import re
|
8 |
|
9 |
+
# Function for OCR processing (similar to your existing code)
|
10 |
+
def process_image(image_bytes):
|
11 |
+
# Convert bytes to image and process
|
12 |
+
image = Image.open(io.BytesIO(image_bytes))
|
13 |
+
opencv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
14 |
+
text = pytesseract.image_to_string(opencv_image)
|
15 |
+
return text
|
16 |
|
17 |
+
# Function for search and highlight (similar to your existing code)
|
18 |
+
def search_and_highlight(full_text, keyword):
|
19 |
+
# Implement search and highlighting logic here
|
20 |
|
21 |
+
# Streamlit app layout
|
22 |
+
st.title("Image Text Search App")
|
23 |
+
uploaded_file = st.file_uploader("Upload an Image", type="jpg,png")
|
|
|
|
|
|
|
24 |
|
25 |
+
if uploaded_file is not None:
|
26 |
+
image_bytes = uploaded_file.read()
|
27 |
+
st.image(image_bytes)
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Perform OCR
|
30 |
+
extracted_text = process_image(image_bytes)
|
31 |
+
st.write("Extracted Text:")
|
32 |
+
st.write(extracted_text)
|
33 |
+
|
34 |
+
# Search functionality
|
35 |
+
search_keyword = st.text_input("Enter a keyword to search:")
|
36 |
+
if search_keyword:
|
37 |
+
results, highlighted_text = search_and_highlight(extracted_text, search_keyword)
|
38 |
+
if results:
|
39 |
+
st.write(f"Keyword '{search_keyword}' found in the extracted text:")
|
40 |
+
for i, result in enumerate(results, 1):
|
41 |
+
st.write(f"{i}. ...{result}...")
|
42 |
+
else:
|
43 |
+
st.write(f"Keyword '{search_keyword}' not found in the extracted text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|