
    khz                     l    d Z ddlZddlmZmZmZ ej                  j                   G d de             Z	y)a  Bounding Box List definition.

BoxList represents a list of bounding boxes as tensorflow
tensors, where each bounding box is represented as a row of 4 numbers,
[y_min, x_min, y_max, x_max].  It is assumed that all bounding boxes
within a given list correspond to a single image.  See also
box_list.py for common box related operations (such as area, iou, etc).

Optionally, users can add additional related fields (such as weights).
We assume the following things to be true about fields:
* they correspond to boxes in the box_list along the 0th dimension
* they have inferable rank at graph construction time
* all dimensions except for possibly the 0th can be inferred
  (i.e., not None) at graph construction time.

Some other notes:
    * Following tensorflow conventions, we use height, width ordering,
        and correspondingly, y,x (or ymin, xmin, ymax, xmax) ordering
    * Tensors are always provided as (flat) [N, 4] tensors.
    N)OptionalListDictc                       e Zd ZU dZeeej                  f   ed<   d Z	d Z
d Zd Zdedej                  fd	Zdefd
Zd Zd ZdefdZdedej                  fdZd Zd Zddeee      fdZd Zy)BoxListzBox collection.datac                     t        |j                        dk7  s|j                  d   dk7  rt        d      |j                  t        j
                  k7  rt        d      d|i| _        y)zConstructs box collection.

        Args:
            boxes: a tensor of shape [N, 4] representing box corners

        Raises:
            ValueError: if invalid dimensions for bbox data or if bbox data is not in float32 format.
               Invalid dimensions for box data.z)Invalid tensor type: should be tf.float32boxesN)lenshape
ValueErrordtypetorchfloat32r   selfr   s     \/var/www/teggl/fontify/venv/lib/python3.12/site-packages/effdet/object_detection/box_list.py__init__zBoxList.__init__,   sX     u{{q EKKOq$8?@@;;%--'HIIe$	    c                 :    | j                   d   j                  d   S )zReturns number of boxes held in collection.

        Returns:
          a tensor representing the number of boxes held in the collection.
        r   r   )r   r   r   s    r   	num_boxeszBoxList.num_boxes;   s     yy!''**r   c                 6    | j                   j                         S )zReturns all fields.)r   keysr   s    r   get_all_fieldszBoxList.get_all_fieldsC   s    yy~~r   c                 v    g }| j                   j                         D ]  }|dk7  s	|j                  |        |S )z@Returns all non-box fields (i.e., everything not named 'boxes').r   )r   r   append)r   extraks      r   get_extra_fieldszBoxList.get_extra_fieldsG   s<     ! 	 AG|Q	  r   field
field_datac                 "    || j                   |<   y)a  Add field to box list.

        This method can be used to add related box data such as weights/labels, etc.

        Args:
            field: a string key to access the data via `get`
            field_data: a tensor containing the data to store in the BoxList
        Nr   )r   r%   r&   s      r   	add_fieldzBoxList.add_fieldP   s     &		%r   c                     || j                   v S Nr(   r   r%   s     r   	has_fieldzBoxList.has_field[   s    		!!r   c                 $    | j                  d      S )zConvenience function for accessing box coordinates.

        Returns:
            a tensor with shape [N, 4] representing box coordinates.
        r   )	get_fieldr   s    r   r   zBoxList.boxes_   s     ~~g&&r   c                     t        |j                        dk7  s|j                  d   dk7  rt        d      || j                  d<   y)zConvenience function for setting box coordinates.

        Args:
            boxes: a tensor of shape [N, 4] representing box corners

        Raises:
            ValueError: if invalid dimensions for bbox data
        r
   r   r   r   r   N)r   r   r   r   r   s     r   	set_boxeszBoxList.set_boxesh   s>     u{{q EKKOq$8?@@"		'r   c                 `    | j                  |      st        d| d      | j                  |   S )a  Accesses a box collection and associated fields.

        This function returns specified field with object; if no field is specified,
        it returns the box coordinates.

        Args:
            field: this optional string parameter can be used to specify a related field to be accessed.

        Returns:
            a tensor representing the box collection or an associated field.

        Raises:
            ValueError: if invalid field
        field  does not existr-   r   r   r,   s     r   r/   zBoxList.get_fieldu   s3     ~~e$veWO<==yyr   valuec                 b    | j                  |      st        d| d      || j                  |<   y)a8  Sets the value of a field.

        Updates the field of a box_list with a given value.

        Args:
            field: (string) name of the field to set value.
            value: the value to assign to the field.

        Raises:
            ValueError: if the box_list does not have specified field.
        r3   r4   Nr5   )r   r%   r6   s      r   	set_fieldzBoxList.set_field   s2     ~~e$veWO<== 		%r   c                     | j                         }|j                         j                         \  }}}}||z
  }||z
  }||dz  z   }||dz  z   }	||	||gS )zComputes the center coordinates, height and width of the boxes.

        Returns:
            a list of 4 1-D tensors [ycenter, xcenter, height, width].
        g       @)r   tunbind)
r   box_cornersyminxminymaxxmaxwidthheightycenterxcenters
             r    get_center_coordinates_and_sizesz(BoxList.get_center_coordinates_and_sizes   si     jjl!,!7!7!9dD$t"$#&%00r   c                     | j                         j                  dd      \  }}}}| j                  t        j                  ||||gd             y)z?Transpose the coordinate representation in a boxlist.

        r      )dimN)r   chunkr1   r   cat)r   y_minx_miny_maxx_maxs        r   transpose_coordinateszBoxList.transpose_coordinates   sH     &*ZZ\%7%7q%7%A"ueUuyy%u!=qABr   Nfieldsc                     i }|| j                         }|D ]2  }| j                  |      st        d      | j                  |      ||<   4 |S )a  Retrieves specified fields as a dictionary of tensors.

        Args:
            fields: (optional) list of fields to return in the dictionary.
                If None (default), all fields are returned.

        Returns:
            tensor_dict: A dictionary of tensors specified by fields.

        Raises:
            ValueError: if specified field is not contained in boxlist.
        z)boxlist must contain all specified fields)r   r-   r   r/   )r   rP   tensor_dictr%   s       r   as_tensor_dictzBoxList.as_tensor_dict   s^     >((*F 	7E>>%( !LMM!%!6K	7 r   c                 4    | j                   d   j                  S )Nr   )r   devicer   s    r   rU   zBoxList.device   s    yy!(((r   r+   )__name__
__module____qualname____doc__r   strr   Tensor__annotations__r   r   r   r$   r)   r-   r   r1   r/   r8   rE   rO   r   r   rS   rU    r   r   r   r   '   s    
sELL 
!!%+ 	&s 	& 	&"s "'# s  &!s !5<< ! 1CXd3i%8 .)r   r   )
rY   r   typingr   r   r   jitscriptobjectr   r]   r   r   <module>rb      s=   (  ' ' ])f ]) ])r   