
    2Vh                     `    d dl Zd dlmZ d dlmZ d dlmZ  ed      	 	 	 	 	 	 dd       Zd Zy)	    N)backend)ops)keras_exportz+keras.visualization.draw_segmentation_masksc                    |xs t        j                         }t        j                  |       }t	        |      dk7  rt        d|       |dk(  r,t        j                  | d      } t        j                  |d      }t        j                  | d      } t        j                  |      }t        j                  |j                        s-t        j                  |j                        }	t        d|	       |1t        t        j                  t        j                  |                  }|t        |      }
nt!        |      D cg c]  }||   	 }
}t        j"                  ||      }t        j$                  |d	
      }t        j&                  ||      }|ddddf   }t        j                  |      }|}t)        j                  |d      j+                  d      }t        j                  |       j-                         }t/        ||
      D ]4  \  }}t)        j0                  ||j                        }|dddf   ||df<   6 t        j                  |      }t        j2                  |d      }|rT| d|z
  z  ||z  z   }t        j4                  |d   ||       }t        j2                  |d      }t        j                  |      }|S c c}w )a  Draws segmentation masks on images.

    The function overlays segmentation masks on the input images.
    The masks are blended with the images using the specified alpha value.

    Args:
        images: A batch of images as a 4D tensor or NumPy array. Shape
            should be (batch_size, height, width, channels).
        segmentation_masks: A batch of segmentation masks as a 3D or 4D tensor
            or NumPy array.  Shape should be (batch_size, height, width) or
            (batch_size, height, width, 1). The values represent class indices
            starting from 1 up to `num_classes`. Class 0 is reserved for
            the background and will be ignored if `ignore_index` is not 0.
        num_classes: The number of segmentation classes. If `None`, it is
            inferred from the maximum value in `segmentation_masks`.
        color_mapping: A dictionary mapping class indices to RGB colors.
            If `None`, a default color palette is generated. The keys should be
            integers starting from 1 up to `num_classes`.
        alpha: The opacity of the segmentation masks. Must be in the range
            `[0, 1]`.
        blend: Whether to blend the masks with the input image using the
            `alpha` value. If `False`, the masks are drawn directly on the
            images without blending. Defaults to `True`.
        ignore_index: The class index to ignore. Mask pixels with this value
            will not be drawn.  Defaults to -1.
        data_format: Image data format, either `"channels_last"` or
            `"channels_first"`. Defaults to the `image_data_format` value found
            in your Keras config file at `~/.keras/keras.json`. If you never
            set it, then it will be `"channels_last"`.

    Returns:
        A NumPy array of the images with the segmentation masks overlaid.

    Raises:
        ValueError: If the input `images` is not a 4D tensor or NumPy array.
        TypeError: If the input `segmentation_masks` is not an integer type.
       z;`images` must be batched 4D tensor. Received: images.shape=channels_first)r            float32)dtypezR`segmentation_masks` must be in integer dtype. Received: segmentation_masks.dtype=N)axis.r   )r
   r   r   r	   )axesboolr   ).Nuint8)r   image_data_formatr   shapelen
ValueError	transposeconvert_to_tensoris_int_dtyper   standardize_dtype	TypeErrorintconvert_to_numpymax_generate_color_paletterange	not_equalsqueezeone_hotnpastypecopyziparraycastwhere)imagessegmentation_masksnum_classescolor_mappingalphablendignore_indexdata_formatimages_shaper   colorsivalid_masksmasksimages_to_drawmaskcoloroutputss                     _/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/visualization/draw_segmentation_masks.pydraw_segmentation_masksr=      s   ` <!:!:!<K99V$L
<A&&2^5
 	
 &&v|4 ]]+=|L""6;F../AB 2 8 89))*<*B*BC227:
 	
 #..sww7I/JKL(5,1+,>?q-"??-- 2LAK++k3K%7E+CAI6--.@A ELL\299&AE))&1668N5&) 3en&:&:;$)$'NtSy!3 **>:Nhh~Y7GAI&58))K	2GVD((7'2&&w/N- @s   Kc                     t        j                  g d      }t        |       D cg c]  }||z  dz  j                          c}S c c}w )N)ii  i    )r$   r(   r    tolist)r-   paletter5   s      r<   r   r   k   s<    hh89G49+4FGqa'kS ((*GGGs   A)NNg?Tr   N)	numpyr$   	keras.srcr   r   keras.src.api_exportr   r=   r        r<   <module>rG      sH       - ;< 

_ =_DHrF   