§
    ß©ºj¿  ã                  ój   — d Z ddlmZ ddlZddlmZmZ ddlmZ g d¢Z	dd
„Z
dd„Zdd„Zdd„Zdd„ZdS )uŸ  COCO-JSON normalizers shared by the shim and the array grid.

These are the small, fiddly conversions between a COCO dictionary
built for `pycocotools` and the COCO JSON vernier's Rust core parses.
They exist because `pycocotools.cocoeval.COCOeval` reads less of the
dataset than vernier's schema requires â€” it never touches an image's
size unless a mask on that image is converted â€” so a dataset assembled
for `COCOeval` can legitimately omit sizes vernier insists on, and can
carry `bytes` RLE counts straight out of `pycocotools.mask.encode`
that :func:`json.dumps` refuses.

Re-exported from :mod:`vernier.adapters`, which is the canonical
import path. See ADR-0055.

This module is a dependency-free leaf that both the drop-in in
:mod:`vernier._compat` and the :mod:`vernier.adapters` package sit on;
it imports nothing from either. It is not a cycle workaround to be
"fixed" by inlining it back into one of them: :mod:`vernier.adapters`
already imports :mod:`vernier._compat`, so these conversions cannot
live in ``adapters`` (that direction *is* the cycle), and putting them
in ``_compat`` would file published, shim-independent helpers under the
pycocotools-shaped shim.
é    )ÚannotationsN)ÚIterableÚMapping)ÚAny)Úcoco_json_defaultÚdetection_image_sizesÚto_coco_jsonÚwith_mask_image_sizesÚwith_placeholder_image_sizesÚdatasetúMapping[str, Any]Úreturnc                óˆ   — |                       dg ¦  «        }t          d„ |D ¦   «         ¦  «        r| S i | ¥dd„ |D ¦   «         i¥S )uI  Fill every missing image ``width`` / ``height`` with ``0``.

    For the IoU types that never read an image size â€” ``bbox`` and
    ``keypoints``, whose kernels work in annotation coordinates â€”
    `pycocotools` reads ``images[].width`` / ``height`` only from
    ``annToRLE``, so a dataset assembled for those types routinely
    omits them (TorchMetrics' ``_get_coco_format`` is one such
    producer). vernier's GT schema requires both on every image, and a
    ``0`` placeholder is inert for these kernels.

    Do not use this for ``segm`` or ``boundary``: a ``0x0`` size
    rasterizes a polygon to nothing and would score silently. Use
    :func:`with_mask_image_sizes` there.

    Returns ``dataset`` unchanged when every image is already sized;
    otherwise a shallow copy. The caller's dictionaries are never
    mutated.
    Úimagesc              3  ó&   K  — | ]}d |v od|v V — ŒdS ©ÚwidthÚheightN© ©Ú.0Úimages     úb/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/vernier/_coco_json.pyú	<genexpr>z/with_placeholder_image_sizes.<locals>.<genexpr><   ó1   è è € Ð
FÐ
F°eˆ7�eÐÐ1 ¨EÐ 1Ð
FÐ
FÐ
FÐ
FÐ
FÐ
Fó    c                ó   — g | ]	}d d dœ|¥‘Œ
S )r   ©r   r   r   r   s     r   ú
<listcomp>z0with_placeholder_image_sizes.<locals>.<listcomp>@   s%   € ÐJÐJÐJ¸%˜Q¨!Ð5Ð5¨uÐ5ÐJÐJÐJr   ©ÚgetÚall)r   r   s     r   r   r   (   sk   € ð& �[Š[˜ 2Ñ&Ô&€FÝ
Ð
FÐ
F¸vÐ
FÑ
FÔ
FÑFÔFð ØˆðØ
ðàÐJÐJÀ6ÐJÑJÔJðð ð r   Údetection_sizesú$Mapping[Any, tuple[int, int] | None]c                óà   ‡‡‡— |                       dg ¦  «        }t          d„ |D ¦   «         ¦  «        r| S d„ |                       dg ¦  «        D ¦   «         Šd
ˆˆfd„Ši | ¥dˆfd	„|D ¦   «         i¥S )u)  Fill missing image sizes for a mask dataset, where it is safe to.

    Under ``segm`` / ``boundary``, `pycocotools`' ``_prepare`` calls
    ``annToRLE`` on every ground-truth annotation against ``cocoGt.imgs``
    and on every detection against ``cocoDt.imgs``, and ``annToRLE``
    reads the image's ``height`` / ``width`` before it looks at the
    segmentation. A size is therefore read only for an image some
    annotation points at, and from the side that annotation belongs to.
    Producers that mirror that â€” TorchMetrics sizes an image only when
    *that* side has masks on it â€” leave gaps `pycocotools` never
    notices.

    vernier's GT schema needs a size on every image and checks each
    detection's RLE size against it, so this fills a gap only where
    `pycocotools` would not have failed:

    - an image nothing points at, on either side, becomes ``0x0`` â€” no
      kernel reads it;
    - an image only detections point at takes the size the detection
      side knows;
    - every other gap is left in place, and surfaces as vernier's schema
      error exactly where `pycocotools` raises ``KeyError``.

    ``detection_sizes`` is keyed by the image ids the *detections* point
    at â€” that key set is what distinguishes the first two cases â€” and
    maps each to that side's ``(height, width)``, or to ``None`` when the
    detection side does not know it either.

    For a `pycocotools`-shaped detection dataset,
    :func:`detection_image_sizes` builds it; that is the pairing the
    drop-in itself uses::

        from vernier.adapters import (
            detection_image_sizes,
            with_mask_image_sizes,
        )

        gt = with_mask_image_sizes(coco_gt.dataset, detection_image_sizes(coco_dt.dataset))

    A caller driving the array grid, with detection RLEs but no
    ``cocoDt`` dataset, builds the same mapping from the first detection
    mask's ``size`` per image instead.

    Returns ``dataset`` unchanged when every image is already sized;
    otherwise a shallow copy. The caller's dictionaries are never
    mutated.
    r   c              3  ó&   K  — | ]}d |v od|v V — ŒdS r   r   r   s     r   r   z(with_mask_image_sizes.<locals>.<genexpr>x   r   r   c                ó   — h | ]
}|d          ’ŒS )Úimage_idr   ©r   Úanns     r   ú	<setcomp>z(with_mask_image_sizes.<locals>.<setcomp>z   s   € ÐKÐKÐK S��Z”ÐKÐKÐKr   r   r   r   r   c                óz   •— | d         }d| v rd| v s|‰v r| S |‰v r‰|         }|€| S |\  }}i | ¥||dœ¥S dddœ| ¥S )NÚidr   r   )r   r   r   r   r   )r   r(   Úsizer   r   Ú	annotatedr#   s        €€r   Úsizedz$with_mask_image_sizes.<locals>.sized|   sƒ   ø€ Ø˜”;ˆØ�uÐÐ ¨UÐ!2Ð!2°xÀ9Ð7LÐ7LØˆLØ�Ð&Ð&Ø" 8Ô,ˆDØˆ|Ø�Ø ‰MˆF�EØ>�eÐ> v¸Ð>Ð>Ð>Ð>Ø aÐ1Ð1¨5Ð1Ð1r   c                ó&   •— g | ]} ‰|¦  «        ‘ŒS r   r   )r   r   r0   s     €r   r   z)with_mask_image_sizes.<locals>.<listcomp>ˆ   s!   ø€ Ð!CÐ!CÐ!C°5 % %¨¡,¤,Ð!CÐ!CÐ!Cr   )r   r   r   r   r    )r   r#   r   r/   r0   s    ` @@r   r
   r
   D   s°   øøø€ ðf �[Š[˜ 2Ñ&Ô&€FÝ
Ð
FÐ
F¸vÐ
FÑ
FÔ
FÑFÔFð ØˆØKÐK¨G¯KªK¸ÀrÑ,JÔ,JÐKÑKÔK€Ið
2ð 
2ð 
2ð 
2ð 
2ð 
2ð 
2ð EˆgÐD�xÐ!CÐ!CÐ!CÐ!C¸FÐ!CÑ!CÔ!CÐDÐDÐDr   ú!dict[Any, tuple[int, int] | None]c                óÐ   ‡— d„ |                       dg ¦  «        D ¦   «         Št                               d„ |                       dg ¦  «        D ¦   «         ¦  «        }ˆfd„|D ¦   «         S )uc  The ``detection_sizes`` mapping :func:`with_mask_image_sizes` reads.

    Takes a `pycocotools`-shaped *detection* dataset (a ``COCO``
    object's ``.dataset``) and returns
    ``{image_id: (height, width) | None}`` over exactly the images its
    annotations point at â€” the key set that tells
    :func:`with_mask_image_sizes` "a detection points at this image".
    The value is ``None`` where the detection side carries no size
    either, which is where `pycocotools`' ``annToRLE`` raises
    ``KeyError`` and vernier's schema error stands in.

    See :func:`with_mask_image_sizes` for the pairing.
    c                óL   — i | ]!}d |v ¯d|v ¯
|d         |d         |d          f“Œ"S )r   r   r-   r   r   s     r   ú
<dictcomp>z)detection_image_sizes.<locals>.<dictcomp>™   sJ   € ð ð ð àØ�eÐÐ ¨EÐ 1Ð 1ð 	ˆdŒ�e˜H”o u¨W¤~Ð6à 1Ð 1Ð 1r   r   c              3  ó&   K  — | ]}|d          V — ŒdS )r(   Nr   r)   s     r   r   z(detection_image_sizes.<locals>.<genexpr>¢   s&   è è € ÐXÐX°#˜c *œoÐXÐXÐXÐXÐXÐXr   r   c                ó<   •— i | ]}|‰                      |¦  «        “ŒS r   )r!   )r   r(   Úknowns     €r   r5   z)detection_image_sizes.<locals>.<dictcomp>£   s'   ø€ ÐDÐDÐD¨hˆH�e—i’i Ñ)Ô)ÐDÐDÐDr   )r!   ÚdictÚfromkeys)r   r/   r8   s     @r   r   r   ‹   sz   ø€ ðð à—[’[ ¨2Ñ.Ô.ðñ ô €Eõ —’ÐXÐX¸¿ºÀ]ÐTVÑ9WÔ9WÐXÑXÔXÑXÔX€IØDÐDÐDÐD¸)ÐDÑDÔDÐDr   Úobjr   c                óæ   — t          | t          ¦  «        r|                      d¦  «        S t          | d¦  «        r|                      ¦   «         S t          dt          | ¦  «        j        › �¦  «        ‚)aó  ``json.dumps(default=...)`` hook for `pycocotools`-shaped values.

    Handles the two non-JSON types a COCO dictionary assembled against
    `pycocotools` carries:

    - ``bytes`` RLE ``counts``, straight out of
      ``pycocotools.mask.encode``. COCO RLE is ASCII (quirk **K3**), so
      they decode as ASCII.
    - NumPy scalars, which ``pycocotools.mask.area`` and ``loadRes``
      leave on ``area`` / ``score`` fields.

    Anything else raises :class:`TypeError`, as :mod:`json` would.
    ÚasciiÚitemznot JSON-serializable: )Ú
isinstanceÚbytesÚdecodeÚhasattrr>   Ú	TypeErrorÚtypeÚ__name__©r;   s    r   r   r   ¦   sh   € õ �#•uÑÔð #Ø�zŠz˜'Ñ"Ô"Ð"Ýˆs�FÑÔð Ø�xŠx‰zŒzÐÝ
ÐB­d°3©i¬iÔ.@ÐBÐBÑ
CÔ
CÐCr   ú!Mapping[str, Any] | Iterable[Any]r@   c                ó\   — t          j        | t          ¬¦  «                             ¦   «         S )u  Serialize a COCO dataset or annotation list to the bytes vernier parses.

    :func:`json.dumps` with :func:`coco_json_default` and a UTF-8
    encode â€” the one-liner every caller handing a `pycocotools`-shaped
    dictionary to a vernier grid would otherwise write itself.
    )Údefault)ÚjsonÚdumpsr   ÚencoderF   s    r   r	   r	   »   s&   € õ Œ:�cÕ#4Ð5Ñ5Ô5×<Ò<Ñ>Ô>Ð>r   )r   r   r   r   )r   r   r#   r$   r   r   )r   r   r   r2   )r;   r   r   r   )r;   rG   r   r@   )Ú__doc__Ú
__future__r   rJ   Úcollections.abcr   r   Útypingr   Ú__all__r   r
   r   r   r	   r   r   r   ú<module>rR      sæ   ððð ð0 #Ð "Ð "Ð "Ð "Ð "à €€€Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ø Ð Ð Ð Ð Ð ðð ð €ðð ð ð ð8DEð DEð DEð DEðNEð Eð Eð Eð6Dð Dð Dð Dð*?ð ?ð ?ð ?ð ?ð ?r   