Update utils/helpers.py
Browse files- utils/helpers.py +30 -25
utils/helpers.py
CHANGED
@@ -235,39 +235,44 @@ def draw_boxes(image: Any, predictions: List[Dict[str, Any]]) -> Any:
|
|
235 |
return image
|
236 |
|
237 |
|
238 |
-
def draw_bounding_boxes_for_textract(image, json_data):
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
# Parse the JSON data
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
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 |
-
#
|
261 |
-
|
262 |
-
|
|
|
|
|
263 |
|
264 |
-
|
265 |
-
|
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 |
|
|
|
235 |
return image
|
236 |
|
237 |
|
238 |
+
def draw_bounding_boxes_for_textract(image: Image.Image, json_data: str) -> Image.Image:
|
239 |
+
"""
|
240 |
+
Draws bounding boxes on an image based on the provided JSON data from Textract.
|
241 |
+
|
242 |
+
Args:
|
243 |
+
image_path: The path to the image on which to draw bounding boxes.
|
244 |
+
json_data: The JSON string containing the bounding box data from Textract.
|
245 |
+
|
246 |
+
Returns:
|
247 |
+
A PIL Image object with bounding boxes drawn.
|
248 |
+
"""
|
249 |
+
# Load the image from the provided path
|
250 |
+
draw = ImageDraw.Draw(image)
|
251 |
|
252 |
# Parse the JSON data
|
253 |
+
try:
|
254 |
+
data = json.loads(json_data)
|
255 |
+
blocks = json.loads(data['body']) if 'body' in data else None
|
256 |
+
except json.JSONDecodeError:
|
|
|
|
|
257 |
st.error('Invalid JSON data.')
|
258 |
+
return image
|
259 |
+
|
260 |
+
if blocks is None:
|
261 |
+
st.error('No bounding box data found.')
|
262 |
+
return image
|
263 |
|
264 |
+
# Iterate through the elements to find bounding boxes and draw them
|
265 |
for item in blocks:
|
266 |
if 'BlockType' in item and item['BlockType'] in ['LINE', 'WORD']:
|
267 |
bbox = item['Geometry']['BoundingBox']
|
268 |
# Extract coordinates and dimensions
|
269 |
left, top, width, height = bbox['Left'], bbox['Top'], bbox['Width'], bbox['Height']
|
270 |
+
# Calculate bounding box coordinates in image space
|
271 |
+
left_top = (left * image.width, top * image.height)
|
272 |
+
right_bottom = ((left + width) * image.width, (top + height) * image.height)
|
273 |
+
# Draw rectangle
|
274 |
+
draw.rectangle([left_top, right_bottom], outline='red', width=2)
|
275 |
|
276 |
+
return image
|
277 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
|