eagle0504 commited on
Commit
08a61a8
·
verified ·
1 Parent(s): 3ff955e

Update utils/helpers.py

Browse files
Files changed (1) hide show
  1. 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
- # 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
 
 
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