Spaces:
Sleeping
Sleeping
Upload 3 Files
Browse files- new.py +39 -0
- ocr_utils.py +21 -0
- requirements.txt +4 -0
new.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from ocr_utils import extract_text
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
# Streamlit application title
|
7 |
+
st.title("OCR and Keyword Search Application")
|
8 |
+
st.write("Upload an image containing Hindi and English text to extract and search within the text.")
|
9 |
+
|
10 |
+
# File uploader for image
|
11 |
+
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
12 |
+
|
13 |
+
if uploaded_file is not None:
|
14 |
+
# Open the uploaded image using PIL
|
15 |
+
image = Image.open(uploaded_file)
|
16 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
17 |
+
|
18 |
+
# Convert the image to a NumPy array
|
19 |
+
image_np = np.array(image)
|
20 |
+
|
21 |
+
# Perform OCR on the uploaded image using the utility function
|
22 |
+
full_text = extract_text(image_np)
|
23 |
+
|
24 |
+
# Display the extracted text
|
25 |
+
st.subheader("Extracted Text")
|
26 |
+
st.write(full_text)
|
27 |
+
|
28 |
+
# Text input for keyword search
|
29 |
+
keyword = st.text_input("Enter Keyword to Search")
|
30 |
+
|
31 |
+
# Highlight the keyword in the extracted text
|
32 |
+
if keyword:
|
33 |
+
highlighted_text = full_text.replace(
|
34 |
+
keyword, f"<mark style='background-color: yellow; color: black;'>{keyword}</mark>")
|
35 |
+
st.subheader("Highlighted Search Results")
|
36 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
37 |
+
else:
|
38 |
+
st.subheader("Highlighted Search Results")
|
39 |
+
st.write("No keyword entered for highlighting.")
|
ocr_utils.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ocr_utils.py
|
2 |
+
|
3 |
+
import easyocr
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Initialize the EasyOCR reader for Hindi and English
|
7 |
+
reader = easyocr.Reader(['hi', 'en'])
|
8 |
+
|
9 |
+
|
10 |
+
def extract_text(image_np):
|
11 |
+
"""
|
12 |
+
Extract text from a NumPy array image using EasyOCR.
|
13 |
+
|
14 |
+
Parameters:
|
15 |
+
- image_np: NumPy array representation of the image.
|
16 |
+
Returns:
|
17 |
+
- full_text: Extracted text as a single string.
|
18 |
+
"""
|
19 |
+
extracted_text = reader.readtext(image_np, detail=0)
|
20 |
+
full_text = " ".join(extracted_text)
|
21 |
+
return full_text
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
easyocr
|
2 |
+
streamlit
|
3 |
+
Pillow
|
4 |
+
numpy
|