code: def masks_to_boxes(
      masks: Tensor, anomaly_maps: Tensor | None = None
  ) -> tuple[list[Tensor], list[Tensor]]:
      """Convert a batch of segmentation masks to bounding box coordinates.
  
      Args:
          masks (Tensor): Input tensor of shape (B, 1, H, W), (B, H, W) or (H, W)
          anomaly_maps (Tensor | None, optional): Anomaly maps of shape (B, 1, H, W), (B, H, W) or (H, W) which are
              used to determine an anomaly score for the converted bounding boxes.
  
      Returns:
          list[Tensor]: A list of length B where each element is a tensor of shape (N, 4) containing the bounding box
              coordinates of the objects in the masks in xyxy format.
          list[Tensor]: A list of length B where each element is a tensor of length (N) containing an anomaly score for
              each of the converted boxes.
      """
      height, width = masks.shape[-2:]
      masks = masks.view(
          (-1, 1, height, width)
      ).float()  # reshape to (B, 1, H, W) and cast to float
      if anomaly_maps is not None:
          anomaly_maps = anomaly_maps.view((-1,) + masks.shape[-2:])
  
      if masks.is_cuda:
          batch_comps = connected_components_gpu(masks).squeeze(1)
      else:
          batch_comps = connected_components_cpu(masks).squeeze(1)
  
      batch_boxes = []
      batch_scores = []
      for im_idx, im_comps in enumerate(batch_comps):
          labels = torch.unique(im_comps)
          im_boxes = []
          im_scores = []
          for label in labels[labels != 0]:
              y_loc, x_loc = torch.where(im_comps == label)
              # add box
              box = Tensor(
                  [
                      torch.min(x_loc),
                      torch.min(y_loc),
                      torch.max(x_loc),
                      torch.max(y_loc),
                  ]
              ).to(masks.device)
              im_boxes.append(box)
              if anomaly_maps is not None:
                  im_scores.append(
                      torch.max(anomaly_maps[im_idx, y_loc, x_loc])
                  )
          batch_boxes.append(
              torch.stack(im_boxes)
              if im_boxes
              else torch.empty((0, 4), device=masks.device)
          )
          batch_scores.append(
              torch.stack(im_scores)
              if im_scores
              else torch.empty(0, device=masks.device)
          )
  
      return batch_boxes, batch_scores
  
file_path: anomalib/src/anomalib/data/utils/boxes.py
imports: from __future__ import annotations
  import torch
  from torch import Tensor
  from anomalib.utils.cv import connected_components_cpu, connected_components_gpu
label_desc: Use image node_id 1 for calling a global function with example usage: masks_to_boxes(masks, anomaly_maps) and returns: batch_boxes,  batch_scores