Spaces:
Running
Running
aarishshahmohsin
commited on
Commit
•
8559818
1
Parent(s):
c722a15
kash shi
Browse files
app.py
CHANGED
@@ -38,9 +38,11 @@ st.write("Upload an image for OCR processing. Using GOT-OCR for English translat
|
|
38 |
st.sidebar.header("Configuration")
|
39 |
model_choice = st.sidebar.selectbox("Select OCR Model:", ("For English + Hindi", "For English (GOT-OCR)"))
|
40 |
|
41 |
-
# Store the uploaded image in session state
|
42 |
if 'uploaded_image' not in st.session_state:
|
43 |
st.session_state.uploaded_image = None
|
|
|
|
|
44 |
|
45 |
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
46 |
|
@@ -60,37 +62,59 @@ if st.session_state.uploaded_image:
|
|
60 |
# Display a smaller preview of the uploaded image (set width to 300px)
|
61 |
col1.image(image, caption='Uploaded Image', use_column_width=False, width=300)
|
62 |
|
|
|
63 |
if predict_button and st.session_state.uploaded_image:
|
64 |
-
with col2:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
#
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
st.sidebar.header("Configuration")
|
39 |
model_choice = st.sidebar.selectbox("Select OCR Model:", ("For English + Hindi", "For English (GOT-OCR)"))
|
40 |
|
41 |
+
# Store the uploaded image and extracted text in session state
|
42 |
if 'uploaded_image' not in st.session_state:
|
43 |
st.session_state.uploaded_image = None
|
44 |
+
if 'extracted_text' not in st.session_state:
|
45 |
+
st.session_state.extracted_text = ""
|
46 |
|
47 |
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
48 |
|
|
|
62 |
# Display a smaller preview of the uploaded image (set width to 300px)
|
63 |
col1.image(image, caption='Uploaded Image', use_column_width=False, width=300)
|
64 |
|
65 |
+
# Handle predictions
|
66 |
if predict_button and st.session_state.uploaded_image:
|
67 |
+
# with col2:
|
68 |
+
with st.spinner("Processing..."):
|
69 |
+
# Save the uploaded file temporarily
|
70 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
71 |
+
temp_file.write(st.session_state.uploaded_image.getvalue())
|
72 |
+
temp_file_path = temp_file.name
|
73 |
+
|
74 |
+
image = Image.open(temp_file_path)
|
75 |
+
image = image.convert("RGB")
|
76 |
+
|
77 |
+
if model_choice == "For English + Hindi":
|
78 |
+
langs = ["en", "hi"]
|
79 |
+
predictions = run_ocr([image], [langs], det_model, det_processor, rec_model, rec_processor)
|
80 |
+
text_list = re.findall(r"text='(.*?)'", str(predictions[0]))
|
81 |
+
extracted_text = ' '.join(text_list)
|
82 |
+
|
83 |
+
st.session_state.extracted_text = extracted_text # Save extracted text in session state
|
84 |
+
|
85 |
+
# with col2:
|
86 |
+
# st.subheader("Extracted Text (Surya):")
|
87 |
+
# st.write(extracted_text)
|
88 |
+
|
89 |
+
elif model_choice == "For English (GOT-OCR)":
|
90 |
+
image_file = temp_file_path
|
91 |
+
res = got_model.chat(tokenizer, image_file, ocr_type='ocr')
|
92 |
+
|
93 |
+
st.session_state.extracted_text = res # Save extracted text in session state
|
94 |
+
|
95 |
+
# with col2:
|
96 |
+
# st.subheader("Extracted Text (GOT-OCR):")
|
97 |
+
# st.write(res)
|
98 |
+
|
99 |
+
# Delete the temporary file after processing
|
100 |
+
if os.path.exists(temp_file_path):
|
101 |
+
os.remove(temp_file_path)
|
102 |
+
|
103 |
+
# Search functionality
|
104 |
+
if st.session_state.extracted_text:
|
105 |
+
search_query = st.text_input("Search in extracted text:", key="search_query", placeholder="Type to search...")
|
106 |
+
|
107 |
+
# Create a pattern to find the search query in a case-insensitive way
|
108 |
+
if search_query:
|
109 |
+
pattern = re.compile(re.escape(search_query), re.IGNORECASE)
|
110 |
+
highlighted_text = st.session_state.extracted_text
|
111 |
+
|
112 |
+
# Replace matching text with highlighted version (bright green)
|
113 |
+
highlighted_text = pattern.sub(lambda m: f"<span style='background-color: limegreen;'>{m.group(0)}</span>", highlighted_text)
|
114 |
+
|
115 |
+
st.markdown("### Highlighted Search Results:")
|
116 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
117 |
+
else:
|
118 |
+
# If no search query, show the original extracted text
|
119 |
+
st.markdown("### Extracted Text:")
|
120 |
+
st.markdown(st.session_state.extracted_text, unsafe_allow_html=True)
|