PRIYANSHUDHAKED commited on
Commit
ece64b8
·
verified ·
1 Parent(s): e50af9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -48
app.py CHANGED
@@ -1,54 +1,96 @@
1
- # app.py
2
- import streamlit as st
3
- import cv2
4
- import numpy as np
5
- import pytesseract
6
  from PIL import Image
 
 
7
  import re
8
 
9
- # Set the title of the webpage
10
- st.title("OCR Text Extraction Tool")
11
-
12
- # Uploading an image
13
- uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
14
-
15
- if uploaded_file is not None:
16
- # Convert the uploaded file content to an image
17
- image = Image.open(uploaded_file)
18
-
19
- # Convert PIL Image to OpenCV format
20
- opencv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
21
-
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)}")
 
 
 
 
1
+ import os
2
+ import google.generativeai as genai
3
+ from google.colab import files
 
 
4
  from PIL import Image
5
+ import io
6
+ from IPython.display import HTML, display
7
  import re
8
 
9
+ # Google Gemini API Key
10
+ GOOGLE_API_KEY = os.getenv("AIzaSyD0GxR2J1JxGic807Cc89Jq6MB4aDJYgDc")
11
+
12
+ # Configure Google Gemini with your API key
13
+ genai.configure(api_key=GOOGLE_API_KEY)
14
+
15
+ # Create a GenerativeModel instance
16
+ model = genai.GenerativeModel("gemini-1.5-flash")
17
+
18
+ def extract_text_with_gemini(image):
19
+ prompt = """
20
+ Extract all text from this image. Provide the output as plain text,
21
+ maintaining the general layout and structure of the document.
22
+ Include all visible text, headings, and any important information.
23
+ """
24
+ response = model.generate_content([prompt, image])
25
+ return response.text
26
+
27
+ def search_and_highlight(full_text, keyword):
28
+ pattern = re.compile(re.escape(keyword), re.IGNORECASE)
29
+ matches = list(pattern.finditer(full_text))
30
+
31
+ if not matches:
32
+ return [], full_text
33
+
34
+ highlighted_text = full_text
35
+ html_text = full_text
36
+ results = []
37
+
38
+ for match in reversed(matches):
39
+ start, end = match.span()
40
+ context_start = max(0, start - 50)
41
+ context_end = min(len(full_text), end + 50)
42
+ context = full_text[context_start:context_end]
43
+
44
+ # Highlight for console output
45
+ highlighted_context = (
46
+ context[:start-context_start] +
47
+ '\033[43m' + context[start-context_start:end-context_start] + '\033[0m' +
48
+ context[end-context_start:]
49
+ )
50
+ results.append(highlighted_context)
51
+
52
+ # Highlight for HTML output
53
+ html_text = (
54
+ html_text[:start] +
55
+ f'<mark>{html_text[start:end]}</mark>' +
56
+ html_text[end:]
57
+ )
58
+
59
+ return results, html_text
60
+
61
+ def app():
62
+ uploaded = files.upload()
63
+
64
+ for filename, file_content in uploaded.items():
65
+ # Open and display the image
66
+ image = Image.open(io.BytesIO(file_content))
67
+ display(image)
68
+
69
+ print("Extracting text from the image...")
70
+ extracted_text = extract_text_with_gemini(image)
71
+
72
+ print("Extracted Text:")
73
+ print(extracted_text)
74
 
75
  # Search functionality
76
+ while True:
77
+ search_keyword = input("\nEnter a keyword to search (or press Enter to exit): ")
78
+ if not search_keyword:
79
+ break
80
+
81
+ results, html_text = search_and_highlight(extracted_text, search_keyword)
82
+
83
+ if results:
84
+ print(f"Keyword '{search_keyword}' found in the extracted text:")
85
+ for i, result in enumerate(results, 1):
86
+ print(f"{i}. ...{result}...")
87
+
88
+ # Display HTML with highlighted text
89
+ display(HTML(f"<p>{html_text}</p>"))
 
 
 
 
90
  else:
91
+ print(f"Keyword '{search_keyword}' not found in the extracted text.")
92
+
93
+ print("OCR and search completed.")
94
+
95
+ if __name__ == "__main__":
96
+ app()