§
    ­¸jÌ  ã                  ó|   — U d Z ddlmZ ddlZddlZddlmZ ddlm	Z
 ddlmZ g d¢Zded	<   g d
¢Zded<   	 ddd„ZdS )aÁ  Image preprocessing shared by the export inference helpers.

Every exported format consumes the same tensor: ImageNet-normalised float32 at the model's spatial resolution, resized
with :meth:`rfdetr.detr.RFDETR.predict`'s exact convention. Only the memory layout differs -- ONNX, OpenVINO and
ExecuTorch take NCHW, while TFLite takes NHWC because ``onnx2tf`` transposes at export time. That transpose is the
caller's job; this module produces NCHW.
é    )ÚannotationsN)ÚNDArray)ÚImage)Ú_bilinear_resize_half_pixel)g
×£p=
ß?gÉv¾Ÿ/Ý?g–C‹lçûÙ?zlist[float]ÚIMAGENET_MEAN)gZd;ßOÍ?gyé&1¬Ì?gÍÌÌÌÌÌÌ?ÚIMAGENET_STDé   ÚimageúPILImage.ImageÚheightÚintÚwidthÚchannelsÚreturnúNDArray[np.float32]c                óþ  — |dk    rdnd}|                       |¦  «        }d„ t          |¦  «        D ¦   «         }d„ t          |¦  «        D ¦   «         }t          j        t          ¦  «        5  ddl}ddlmc m}	 | 	                    ¦   «         5  |	 
                    |¦  «        }
|	                     |
||gd¬	¦  «        }
|	                     |
||¦  «        }
ddd¦  «         n# 1 swxY w Y   t          j        |
                     d¦  «                             ¦   «                              ¦   «         t          j        ¬
¦  «        cddd¦  «         S # 1 swxY w Y   t          j        |t          j        ¬
¦  «        dz  }|j        dk    r|dd…dd…t          j        f         }t+          |                     ddd¦  «        ||¦  «        }t          j        |t          j        ¬
¦  «        dd…t          j        t          j        f         }t          j        |t          j        ¬
¦  «        dd…t          j        t          j        f         }||z
  |z  }t          j        |d¬¦  «                             t          j        ¦  «        S )aÅ  Resize and normalise a PIL image to a ``(1, C, H, W)`` float32 NCHW tensor.

    Resizes with ``RFDETR.predict()``'s exact convention -- bilinear, half-pixel centers,
    ``antialias=False`` -- via ``torchvision`` when importable (bit-exact parity) or the pure-NumPy
    :func:`~rfdetr.export._resize._bilinear_resize_half_pixel` otherwise (float32 op-order noise only). PIL resize is
    not used: both its BILINEAR and BICUBIC filters apply adaptive antialiasing when downscaling and diverge from
    predict(), shifting confidence scores. Normalises with ImageNet statistics: ``mean=[0.485, 0.456, 0.406]``,
    ``std=[0.229, 0.224, 0.225]``.

    Args:
        image: Input PIL image; any mode -- converted to ``"RGB"`` (3-channel) or ``"L"`` (1-channel) internally.
        height: Target spatial height expected by the model.
        width: Target spatial width expected by the model.
        channels: Number of channels the model expects (``1`` for grayscale, ``3`` for RGB).

    Returns:
        Float32 ndarray of shape ``(1, channels, height, width)``.

    Examples:
        >>> from PIL import Image
        >>> preprocess_to_nchw(Image.new("RGB", (8, 8)), height=4, width=4).shape
        (1, 3, 4, 4)
    é   ÚLÚRGBc                ó.   — g | ]}t           |d z           ‘ŒS ©r	   )r   ©Ú.0Úis     úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/export/_runtime/preprocess.pyú
<listcomp>z&preprocess_to_nchw.<locals>.<listcomp>;   s!   € Ð?Ð?Ð?¨!•˜q 1™uÔ%Ð?Ð?Ð?ó    c                ó.   — g | ]}t           |d z           ‘ŒS r   )r   r   s     r   r   z&preprocess_to_nchw.<locals>.<listcomp><   s!   € Ð=Ð=Ð=¨•˜Q ™UÔ#Ð=Ð=Ð=r   r   NF)Ú	antialias)Údtypeg     ào@é   )Úaxis)ÚconvertÚrangeÚ
contextlibÚsuppressÚImportErrorÚtorchÚ!torchvision.transforms.functionalÚ
transformsÚ
functionalÚno_gradÚ	to_tensorÚresizeÚ	normalizeÚnpÚasarrayÚ	unsqueezeÚcpuÚnumpyÚfloat32ÚndimÚnewaxisr   Ú	transposeÚarrayÚexpand_dimsÚastype)r
   r   r   r   Úpil_modeÚpil_imgÚ	mean_listÚstd_listr(   Ú_FÚtÚarrÚchwÚmeanÚstds                  r   Úpreprocess_to_nchwrF      sÂ  € ð:  !’m�mˆsˆs¨€HØ�mŠm˜HÑ%Ô%€GØ?Ð?­u°X©¬Ð?Ñ?Ô?€IØ=Ð=­U°8©_¬_Ð=Ñ=Ô=€Hå	Ô	�[Ñ	)Ô	)ð 
Jð 
Jð 	ˆˆˆØ6Ð6Ð6Ð6Ð6Ð6Ð6Ð6Ð6à�]Š]‰_Œ_ð 	5ð 	5Ø—’˜WÑ%Ô%ˆAØ—	’	˜!˜f e˜_¸�	Ñ>Ô>ˆAØ—’˜Q 	¨8Ñ4Ô4ˆAð	5ð 	5ð 	5ñ 	5ô 	5ð 	5ð 	5ð 	5ð 	5ð 	5ð 	5øøøð 	5ð 	5ð 	5ð 	5õ Œz˜!Ÿ+š+ a™.œ.×,Ò,Ñ.Ô.×4Ò4Ñ6Ô6½b¼jÐIÑIÔIð
Jð 
Jð 
Jð 
Jñ 
Jô 
Jð 
Jð 
Jð 
Jð 
Jð 
Jð 
Jøøøð 
Jð 
Jð 
Jð 
Jõ Œ*�W¥B¤JÐ
/Ñ
/Ô
/°%Ñ
7€CØ
„x�1‚}€}Ø�!�!�!�Q�Q�Q�œ
Ð"Ô#ˆÝ
% c§m¢m°A°q¸!Ñ&<Ô&<¸fÀeÑ
LÔ
L€CÝŒ8�I¥R¤ZÐ0Ñ0Ô0°°°µB´JÅÄ
Ð1JÔK€DÝ
Œ(�8¥2¤:Ð
.Ñ
.Ô
.¨q¨q¨qµ"´*½b¼jÐ/HÔ
I€CØ�‰:˜Ñ
€CÝŒ>˜# AÐ&Ñ&Ô&×-Ò-­b¬jÑ9Ô9Ð9s8   Á+"EÂAC ÃEÃ C$	Ã$EÃ'C$	Ã(AEÅEÅEr   )
r
   r   r   r   r   r   r   r   r   r   )Ú__doc__Ú
__future__r   r%   r4   r0   Únumpy.typingr   ÚPILr   ÚPILImageÚrfdetr.export._resizer   r   Ú__annotations__r   rF   © r   r   ú<module>rO      sÌ   ððð ð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð à Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ø !Ð !Ð !Ð !Ð !Ð !à =Ð =Ð =Ð =Ð =Ð =à2Ð2Ð2€Ð 2Ð 2Ð 2Ñ 2Ø1Ð1Ð1€Ð 1Ð 1Ð 1Ñ 1ð ð	6:ð 6:ð 6:ð 6:ð 6:ð 6:ð 6:r   