PRIYANSHUDHAKED commited on
Commit
dbf63c3
·
verified ·
1 Parent(s): f23a44e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
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
- # Perform OCR
26
- text = pytesseract.image_to_string(opencv_image)
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
- if matches:
38
- st.markdown("### Keyword Found:")
39
- for match in matches:
40
- start, end = match.span()
41
- context_start = max(0, start - 50)
42
- context_end = min(len(text), end + 50)
43
- context = text[context_start:context_end]
44
- highlighted_text = (
45
- context[:start-context_start] +
46
- f"<span style='background-color: yellow;'>{context[start-context_start:end-context_start]}</span>" +
47
- context[end-context_start:]
48
- )
49
- st.markdown(f"...{highlighted_text}...")
50
- else:
51
- st.write(f"Keyword '{search_keyword}' not found in the extracted text.")
 
 
 
 
 
 
 
 
 
 
 
 
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)}")