o
    2h                     @   sV   d dl Zd dlmZ d dlmZ d dlmZ ed						dd	d
Zdd ZdS )    N)backend)ops)keras_exportz+keras.visualization.draw_segmentation_masks皙?Tc                    s  |pt  }t| }t|dkrtd| |dkr(t| d} t|d}tj| dd} t|}t |j	sGt 
|j	}	td|	 |du rUttt|} du r^t|}
n fd	d
t|D }
t||}tj|dd}t||}|ddddf }t|}|}tj|ddd}t|  }t||
D ]\}}tj||j	d}|dddf ||df< qt|}tj|dd}|r| d|  ||  }t|d || }tj|dd}t|}|S )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=Nc                    s   g | ]} | qS  r   .0icolor_mappingr   j/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/visualization/draw_segmentation_masks.py
<listcomp>R   s    z+draw_segmentation_masks.<locals>.<listcomp>r   )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_classesr   alphablendignore_indexdata_formatimages_shaper   colorsvalid_masksmasksimages_to_drawmaskcoloroutputsr   r   r   draw_segmentation_masks   sX   0





rA   c                    s$   t g d  fddt| D S )N)ii  i c                    s   g | ]
}|  d    qS )   )tolistr   paletter   r   r   m   s    z+_generate_color_palette.<locals>.<listcomp>)r+   r/   r'   )r4   r   rD   r   r&   k   s   r&   )NNr   Tr   N)	numpyr+   	keras.srcr   r   keras.src.api_exportr   rA   r&   r   r   r   r   <module>   s    b