Update utils/helpers.py
Browse files- utils/helpers.py +41 -0
utils/helpers.py
CHANGED
@@ -14,6 +14,9 @@ from langchain.text_splitter import (
|
|
14 |
RecursiveCharacterTextSplitter,
|
15 |
SentenceTransformersTokenTextSplitter,
|
16 |
)
|
|
|
|
|
|
|
17 |
import chromadb
|
18 |
from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction
|
19 |
|
@@ -230,3 +233,41 @@ def draw_boxes(image: Any, predictions: List[Dict[str, Any]]) -> Any:
|
|
230 |
|
231 |
# Return the annotated image
|
232 |
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
RecursiveCharacterTextSplitter,
|
15 |
SentenceTransformersTokenTextSplitter,
|
16 |
)
|
17 |
+
import matplotlib.pyplot as plt
|
18 |
+
import matplotlib.patches as patches
|
19 |
+
import streamlit as st
|
20 |
import chromadb
|
21 |
from chromadb.utils.embedding_functions import SentenceTransformerEmbeddingFunction
|
22 |
|
|
|
233 |
|
234 |
# Return the annotated image
|
235 |
return image
|
236 |
+
|
237 |
+
|
238 |
+
def draw_bounding_boxes_for_textract(image, json_data):
|
239 |
+
# Load the image
|
240 |
+
# image: image = Image.open(image)
|
241 |
+
fig, ax = plt.subplots(1)
|
242 |
+
ax.imshow(image)
|
243 |
+
|
244 |
+
# Parse the JSON data
|
245 |
+
data = json.loads(json_data)
|
246 |
+
|
247 |
+
# Check if 'body' key exists and load its content
|
248 |
+
if 'body' in data:
|
249 |
+
blocks = json.loads(data['body'])
|
250 |
+
else:
|
251 |
+
st.error('Invalid JSON data.')
|
252 |
+
return
|
253 |
+
|
254 |
+
# Iterate through the elements to find bounding boxes
|
255 |
+
for item in blocks:
|
256 |
+
if 'BlockType' in item and item['BlockType'] in ['LINE', 'WORD']:
|
257 |
+
bbox = item['Geometry']['BoundingBox']
|
258 |
+
# Extract coordinates and dimensions
|
259 |
+
left, top, width, height = bbox['Left'], bbox['Top'], bbox['Width'], bbox['Height']
|
260 |
+
# Convert to image coordinates (may need adjustment based on your image dimensions)
|
261 |
+
rect = patches.Rectangle((left * image.width, top * image.height), width * image.width, height * image.height, linewidth=1, edgecolor='r', facecolor='none')
|
262 |
+
ax.add_patch(rect)
|
263 |
+
|
264 |
+
# Hide axes
|
265 |
+
ax.axis('off')
|
266 |
+
# Save the annotated image to a temporary file
|
267 |
+
temp_file = "temp_annotated_image.png"
|
268 |
+
plt.savefig(temp_file, bbox_inches='tight', pad_inches=0)
|
269 |
+
plt.close()
|
270 |
+
|
271 |
+
# Display the annotated image in Streamlit
|
272 |
+
st.image(temp_file)
|
273 |
+
|