PriyankaSatish commited on
Commit
7a9b80b
1 Parent(s): e822c3d

Upload 2 files

Browse files
Files changed (2) hide show
  1. requirements2.txt +0 -0
  2. segmentation.py +81 -0
requirements2.txt ADDED
Binary file (3.93 kB). View file
 
segmentation.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import io
4
+ from dotenv import load_dotenv
5
+ import os
6
+ import openai
7
+ from openai import OpenAI
8
+
9
+ # Google Cloud Vision
10
+ from google.cloud import vision
11
+
12
+ # Load environment variables
13
+ load_dotenv()
14
+
15
+ # Set Google Cloud credentials in environment
16
+ service_account_path = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
17
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r'gcv-new-project-dd6ed833cc91.json'
18
+
19
+ # Initialize Google Vision client
20
+ vision_client = vision.ImageAnnotatorClient()
21
+
22
+ def extract_text_with_google_vision_api(image):
23
+ """Extract structured text from image using Google Cloud Vision API, with additional formatting based on bounding box analysis."""
24
+ import io
25
+ from google.cloud import vision
26
+ vision_client = vision.ImageAnnotatorClient()
27
+
28
+ img_byte_arr = io.BytesIO()
29
+ image.save(img_byte_arr, format=image.format)
30
+ image_bytes = img_byte_arr.getvalue()
31
+
32
+ image = vision.Image(content=image_bytes)
33
+ response = vision_client.document_text_detection(image=image)
34
+
35
+ structured_texts = []
36
+ for page in response.full_text_annotation.pages:
37
+ for block in page.blocks:
38
+ block_texts = []
39
+ last_paragraph_bottom = None # Store the bottom position of the last paragraph to compare spacing
40
+ for paragraph in block.paragraphs:
41
+ paragraph_text = ' '.join([''.join([symbol.text for symbol in word.symbols]) for word in paragraph.words])
42
+ paragraph_text = paragraph_text.strip()
43
+
44
+ # Example for analyzing bounding box position and size (simplified)
45
+ paragraph_bounds = paragraph.bounding_box
46
+ top_left = paragraph_bounds.vertices[0] # Example vertex
47
+
48
+ if last_paragraph_bottom is not None and (top_left.y - last_paragraph_bottom) > 10:
49
+ # Add additional break if the spacing exceeds some threshold
50
+ block_texts.append("") # This adds an extra line break to indicate a significant separation
51
+
52
+ # Update last_paragraph_bottom to the current paragraph's bottom position
53
+ last_paragraph_bottom = paragraph_bounds.vertices[2].y # Assuming 0 is top-left and going clockwise
54
+
55
+ if len(paragraph_text.split()) > 2:
56
+ block_texts.append(paragraph_text)
57
+
58
+ if block_texts:
59
+ structured_texts.append('\n'.join(block_texts))
60
+
61
+ if structured_texts:
62
+ return '\n\n'.join(structured_texts)
63
+ else:
64
+ return "No structured text found."
65
+
66
+ def main():
67
+ st.title("Article Extraction")
68
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
69
+
70
+ if uploaded_file is not None:
71
+ image = Image.open(uploaded_file)
72
+ st.image(image, caption="Uploaded Image", use_column_width=True)
73
+
74
+ extracted_text = extract_text_with_google_vision_api(image)
75
+
76
+ st.success("Analysis completed successfully!")
77
+ st.header("Extracted Text:")
78
+ st.write(extracted_text if extracted_text else "No text detected.")
79
+
80
+ if __name__ == "__main__":
81
+ main()