Shabdobhedi commited on
Commit
5560167
·
verified ·
1 Parent(s): 8ab0729

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -128
app.py CHANGED
@@ -1,138 +1,37 @@
1
- '''import streamlit as st
2
- from ocr_utils import initialize_reader, read_text_from_image, join_extracted_text
3
-
4
- def main():
5
- """Main function to handle Streamlit application logic."""
6
-
7
- # Initialize OCR reader
8
- reader = initialize_reader()
9
-
10
- # Streamlit app setup
11
- st.title("OCR and Keyword Search Application")
12
- st.write("Upload an image containing Hindi and English text to extract and search within the text.")
13
-
14
- # File uploader and image display
15
- uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
16
- if uploaded_file is not None:
17
- image = Image.open(uploaded_file)
18
- st.image(image, caption='Uploaded Image', use_column_width=True) 
19
-  
20
-
21
-
22
- # Keyword input and text extraction/highlighting
23
- keyword = st.text_input("Enter Keyword to Search")
24
-
25
- if uploaded_file is not None:
26
- try:
27
- extracted_text = read_text_from_image(uploaded_file.name, reader)
28
- full_text = join_extracted_text(extracted_text)
29
-
30
- st.subheader("Extracted Text")
31
- st.write(full_text)
32
-
33
- if keyword:
34
- highlighted_text = full_text.replace(keyword, f"<mark>{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.")
40
- except FileNotFoundError as e:
41
- st.error(str(e))
42
-
43
- if __name__ == "__main__":
44
- main()
45
- import streamlit as st
46
- from PIL import Image
47
- from ocr_utils import initialize_reader, read_text_from_image, join_extracted_text
48
-
49
-
50
- def main():
51
- """Main function to handle Streamlit application logic."""
52
-
53
- # Initialize OCR reader
54
- reader = initialize_reader()
55
-
56
- # Streamlit app setup
57
- st.title("OCR and Keyword Search Application")
58
- st.write("Upload an image containing Hindi and English text to extract and search within the text.")
59
-
60
- # File uploader and image display
61
- uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
62
- if uploaded_file is not None:
63
- image = Image.open(uploaded_file)
64
- st.image(image, caption="Uploaded Image", use_column_width=True) # Corrected caption
65
-
66
- # Keyword input and text extraction/highlighting
67
- keyword = st.text_input("Enter Keyword to Search")
68
-
69
- if uploaded_file is not None:
70
- try:
71
- extracted_text = read_text_from_image(uploaded_file.name, reader)
72
- full_text = join_extracted_text(extracted_text)
73
-
74
- st.subheader("Extracted Text")
75
- st.write(full_text)
76
-
77
- if keyword:
78
- highlighted_text = full_text.replace(keyword, f"<mark>{keyword}</mark>")
79
- st.subheader("Highlighted Search Results")
80
- st.markdown(highlighted_text, unsafe_allow_html=True)
81
- else:
82
- st.subheader("Highlighted Search Results")
83
- st.write("No keyword entered for highlighting.")
84
- except FileNotFoundError as e:
85
- st.error(str(e))
86
-
87
-
88
- if __name__ == "__main__":
89
- main()'''
90
  import streamlit as st
91
  from PIL import Image
92
- from ocr_utils import initialize_reader, read_text_from_image, join_extracted_text
93
-
94
- def main():
95
- """Main function to handle Streamlit application logic."""
96
 
97
- # Initialize OCR reader
98
- reader = initialize_reader()
 
99
 
100
- # Streamlit app setup
101
- st.title("OCR and Keyword Search Application")
102
- st.write("Upload an image containing Hindi and English text to extract and search within the text.")
103
 
104
- # File uploader and image display
105
- uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
106
- if uploaded_file is not None:
107
- image = Image.open(uploaded_file.name)
108
- st.image(image, caption="Uploaded Image", use_column_width=True)
109
 
110
- # Print the file path for debugging
111
- print("File path:", uploaded_file.name)
112
 
113
- # Keyword input and text extraction/highlighting
114
- keyword = st.text_input("Enter Keyword to Search")
115
 
116
- if uploaded_file is not None:
117
- try:
118
- extracted_text = read_text_from_image(uploaded_file.name, reader)
119
- full_text = join_extracted_text(extracted_text)
120
-
121
- st.subheader("Extracted Text")
122
- st.write(full_text)
123
-
124
- if keyword:
125
- highlighted_text = full_text.replace(keyword, f"<mark>{keyword}</mark>")
126
- st.subheader("Highlighted Search Results")
127
- st.markdown(highlighted_text, unsafe_allow_html=True)
128
- else:
129
- st.subheader("Highlighted Search Results")
130
- st.write("No keyword entered for highlighting.")
131
- except FileNotFoundError as e:
132
- st.error(f"Image file not found: {str(e)}")
133
- except Exception as e: # Catch other potential exceptions
134
- st.error(f"An error occurred: {str(e)}")
135
 
 
 
136
 
137
- if __name__ == "__main__":
138
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  from PIL import Image
3
+ from ocr_utils import extract_text
 
 
 
4
 
5
+ # Streamlit application title
6
+ st.title("OCR and Keyword Search Application")
7
+ st.write("Upload an image containing Hindi and English text to extract and search within the text.")
8
 
9
+ # File uploader for image
10
+ uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
 
11
 
12
+ if uploaded_file is not None:
13
+ # Open the uploaded image using PIL
14
+ image = Image.open(uploaded_file)
15
+ st.image(image, caption='Uploaded Image', use_column_width=True)
 
16
 
17
+ # Convert the image to a NumPy array
18
+ image_np = np.array(image)
19
 
20
+ # Perform OCR on the uploaded image using the utility function
21
+ full_text = extract_text(image_np)
22
 
23
+ # Display the extracted text
24
+ st.subheader("Extracted Text")
25
+ st.write(full_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # Text input for keyword search
28
+ keyword = st.text_input("Enter Keyword to Search")
29
 
30
+ # Highlight the keyword in the extracted text
31
+ if keyword:
32
+ highlighted_text = full_text.replace(keyword, f"<mark>{keyword}</mark>")
33
+ st.subheader("Highlighted Search Results")
34
+ st.markdown(highlighted_text, unsafe_allow_html=True)
35
+ else:
36
+ st.subheader("Highlighted Search Results")
37
+ st.write("No keyword entered for highlighting.")