o
    2h                     @   s   d dl Zd dlmZ d dlmZ d dlmZ d dlmZ zd dl	Z	W n e
y-   dZ	Y nw ed						
	dddZdd ZdS )    N)backend)ops)keras_export)convert_formatz'keras.visualization.draw_bounding_boxes   r   r               ?c	           !      C   sV  t du rtd|pi }|p|}|pt }t| }	t|	dkr(td|	 t|t	s9t
d| dt| d|vsAd|vrHtd	| |d
krQd}
d}nd}
d}|	|
 }|	| }| }t||d||}t| d} t|d }t|d }d|v rt|d }nd}g }| jd }t|D ]}| | }|| }|| }t|jd D ]p}|| d\}}}}|| d}|dkrqt|t|t|t|f\}}}}t|}t |||f||f|| ||v r|| }|dur|| | }| d|d}t||||\}} t j|||| ft j|||d q|| qtj|ddS )a!
  Draws bounding boxes on images.

    This function draws bounding boxes on a batch of images.  It supports
    different bounding box formats and can optionally display class labels
    and confidences.

    Args:
        images: A batch of images as a 4D tensor or NumPy array. Shape should be
            `(batch_size, height, width, channels)`.
        bounding_boxes: A dictionary containing bounding box data.  Should have
            the following keys:
            - `boxes`: A tensor or array of shape `(batch_size, num_boxes, 4)`
               containing the bounding box coordinates in the specified format.
            - `labels`: A tensor or array of shape `(batch_size, num_boxes)`
              containing the class labels for each bounding box.
            - `confidences` (Optional): A tensor or array of shape
               `(batch_size, num_boxes)` containing the confidence scores for
               each bounding box.
        bounding_box_format: A string specifying the format of the bounding
            boxes. Refer [keras-io](TODO)
        class_mapping: A dictionary mapping class IDs (integers) to class labels
            (strings).  Used to display class labels next to the bounding boxes.
            Defaults to None (no labels displayed).
        color: A tuple or list representing the RGB color of the bounding boxes.
            For example, `(255, 0, 0)` for red. Defaults to `(128, 128, 128)`.
        line_thickness: An integer specifying the thickness of the bounding box
            lines. Defaults to `2`.
        text_thickness: An integer specifying the thickness of the text labels.
            Defaults to `1`.
        font_scale: A float specifying the scale of the font used for text
            labels. Defaults to `1.0`.
        data_format: A string, either `"channels_last"` or `"channels_first"`,
            specifying the order of dimensions in the input images. 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 annotated images with the bounding boxes drawn.
        The array will have the same shape as the input `images`.

    Raises:
        ValueError: If `images` is not a 4D tensor/array, if `bounding_boxes` is
            not a dictionary, or if `bounding_boxes` does not contain `"boxes"`
            and `"labels"` keys.
        TypeError: If `bounding_boxes` is not a dictionary.
        ImportError: If `cv2` (OpenCV) is not installed.
    Nz|The `draw_bounding_boxes` function requires the `cv2` package  (OpenCV). Please install it with `pip install opencv-python`.   z;`images` must be batched 4D tensor. Received: images.shape=z<`bounding_boxes` should be a dict. Received: bounding_boxes=z	 of type boxeslabelsza`bounding_boxes` should be a dict containing 'boxes' and 'labels' keys. Received: bounding_boxes=channels_lastxyxyuint8confidencesr   int32z | z.2f)imgtextorgfontFace	fontScalecolor	thickness)axis)cv2ImportErrorr   image_data_formatr   shapelen
ValueError
isinstancedict	TypeErrortypecopyr   convert_to_numpyastyperangeint	rectangle_find_text_locationputTextFONT_HERSHEY_SIMPLEXappendnpstack)!imagesbounding_boxesbounding_box_formatclass_mappingr   line_thicknesstext_thickness
font_scaledata_formatimages_shapeh_axisw_axisheightwidthr   r   r   result
batch_sizei_image_box_classbox_ix1y1x2y2clabelconffont_x1font_y1 rQ   f/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/visualization/draw_bounding_boxes.pydraw_bounding_boxes   s   =



$

	rS   c                 C   sT   t |d }|d }|d|  dkr| |d fS |}d}| | |d|  | | fS )N      r   r      )r,   )xyr:   r   font_heighttarget_yline_offsetstatic_offsetrQ   rQ   rR   r.      s   r.   )Nr   r   r	   r
   N)numpyr2   	keras.srcr   r   keras.src.api_exportr   Lkeras.src.layers.preprocessing.image_preprocessing.bounding_boxes.convertersr   r   r   rS   r.   rQ   rQ   rQ   rR   <module>   s(     