o
    e'                     @  s   d Z ddlmZ ddlmZ ddlZddlmZm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZ e	d e  e G dd deeeZdS )zgr.AnnotatedImage() component.    )annotations)LiteralN)documentset_documentation_group)JSONSerializable)Image)utils)IOComponent	_Keywords)warn_style_method_deprecation)EventListenerMethod
Selectable	componentc                   @  s   e Zd ZdZ	d-ddddddddddddddd.ddZdd  Zeejddddddddddfd/d"d#Z	d0d(d)Z
dddd*d1d+d,ZdS )2AnnotatedImageaq  
    Displays a base image and colored subsections on top of that image. Subsections can take the from of rectangles (e.g. object detection) or masks (e.g. image segmentation).
    Preprocessing: this component does *not* accept input.
    Postprocessing: expects a {Tuple[numpy.ndarray | PIL.Image | str, List[Tuple[numpy.ndarray | Tuple[int, int, int, int], str]]]} consisting of a base image and a list of subsections, that are either (x1, y1, x2, y2) tuples identifying object boundaries, or 0-1 confidence masks of the same shape as the image. A label is provided for each subsection.

    Demos: image_segmentation
    NT   )show_legendheightwidth	color_maplabelevery
show_label	containerscale	min_widthvisibleelem_idelem_classesvaluegtuple[np.ndarray | _Image.Image | str, list[tuple[np.ndarray | tuple[int, int, int, int], str]]] | Noner   boolr   
int | Noner   r   dict[str, str] | Noner   
str | Noner   float | Noner   bool | Noner   r   r   intr   r   r   list[str] | str | Nonec                K  sJ   || _ || _|| _|| _|  	 tj| f||||	|
|||||d
| dS )a  
        Parameters:
            value: Tuple of base image and list of (subsection, label) pairs.
            show_legend: If True, will show a legend of the subsections.
            height: Height of the displayed image.
            width: Width of the displayed image.
            color_map: A dictionary mapping labels to colors. The colors must be specified as hex codes.
            label: component name in interface.
            every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
            show_label: if True, will display label.
            container: If True, will place the component in a container - providing some extra padding around the border.
            scale: relative width compared to adjacent Components in a Row. For example, if Component A has scale=2, and Component B has scale=1, A will be twice as wide as B. Should be an integer.
            min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
            visible: If False, component will be hidden.
            elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
            elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
        )
r   r   r   r   r   r   r   r   r   r   N)r   r   r   r   r	   __init__)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   kwargs r+   QD:\Projects\ConvertPro\env\Lib\site-packages\gradio/components/annotated_image.pyr(   #   s,   (
zAnnotatedImage.__init__c                 C  s(   | j | j| j| j| j| jdt| S )N)r   r   r   r   r   
selectable)r   r   r   r   r   r-   r	   
get_config)r)   r+   r+   r,   r.   d   s   zAnnotatedImage.get_config~tuple[np.ndarray | _Image.Image | str, list[tuple[np.ndarray | tuple[int, int, int, int], str]]] | Literal[_Keywords.NO_VALUE]c                 C  s"   |||||||||	|
| dd}|S )Nupdate)r   r   r   r   r   r   r   r   r   r   r   Z__type__r+   )r   r   r   r   r   r   r   r   r   r   r   Zupdated_configr+   r+   r,   r0   o   s   zAnnotatedImage.updatey`tuple[np.ndarray | _Image.Image | str, list[tuple[np.ndarray | tuple[int, int, int, int], str]]]return*tuple[dict, list[tuple[dict, str]]] | Nonec                 C  s  |du rdS |d }t |tr|}tt|}n5t |tjr0| j|| jd}tt	
|}nt |tjrK| j|| jd}tt	
|}t|}ntd| j| g }| jp[i }dd }|d D ]\}}	t|jd |jd f}
t |tjr~|}
nB|\}}}}d}d	|
||||f< d|
||||| f< d|
|||| |f< d|
||| ||f< d|
|| |||f< |	|v r|||	 }ng d
}t|jd |jd df}t|
}d||dk< |d | |dddddf< |d | |dddddf< |d | |dddddf< |
d |dddddf< t|tj}| j|| jd}tt	
|}| j| ||ddd|	f qd|ddd|fS )a  
        Parameters:
            y: Tuple of base image and list of subsections, with each subsection a two-part tuple where the first element is a 4 element bounding box or a 0-1 confidence mask, and the second element is the label.
        Returns:
            Tuple of base image file and list of subsections, with each subsection a two-part tuple where the first element image path of the mask, and the second element is the label.
        Nr   )dirzUAnnotatedImage only accepts filepaths, PIL images or numpy arrays for the base image.c                   s2    dt  fddtd  d D S )N#c                   s&   g | ]}t || d    dqS )      )r&   ).0ilvr   r+   r,   
<listcomp>   s   & zBAnnotatedImage.postprocess.<locals>.hex_to_rgb.<locals>.<listcomp>r   r7   )lstriplenrange)r   r+   r;   r,   
hex_to_rgb   s   
 z.AnnotatedImage.postprocess.<locals>.hex_to_rgb   r7   g      ?)   r   r         rC   T)namedatais_file)
isinstancestrnparray_ImageopenZndarrayZimg_array_to_temp_fileZDEFAULT_TEMP_DIRr   abspathr   Zpil_to_temp_file
ValueErrorZ
temp_filesaddr   ZzerosshapecopyZ	fromarrayZastypeZuint8append)r)   r1   Zbase_imgZbase_img_pathZ	base_filesectionsr   rA   maskr   Z
mask_arrayx1y1Zx2y2Zborder_widthZ	rgb_colorZcolored_maskZ
solid_maskZcolored_mask_imgZ	mask_fileZmask_file_pathr+   r+   r,   postprocess   sf   


zAnnotatedImage.postprocess)r   r   r   c                K  s4   t   |dur
|| _|dur|| _|dur|| _| S )zc
        This method is deprecated. Please set these arguments in the constructor instead.
        N)r   r   r   r   )r)   r   r   r   r*   r+   r+   r,   style   s   zAnnotatedImage.style)N)r   r   r   r    r   r!   r   r!   r   r"   r   r#   r   r$   r   r%   r   r    r   r!   r   r&   r   r    r   r#   r   r'   )r   r/   r   r%   r   r!   r   r!   r   r"   r   r#   r   r%   r   r%   r   r!   r   r!   r   r%   )r1   r2   r3   r4   )r   r!   r   r!   r   r"   )__name__
__module____qualname____doc__r(   r.   staticmethodr
   ZNO_VALUEr0   rZ   r[   r+   r+   r+   r,   r      sJ    A
!Sr   )r_   
__future__r   typingr   numpyrK   Zgradio_client.documentationr   r   Zgradio_client.serializingr   ZPILr   rM   Zgradior   Zgradio.components.baser	   r
   Zgradio.deprecationr   Zgradio.eventsr   r   initr   r+   r+   r+   r,   <module>   s    