§
    ­¸jR  ã                  ó�   — d Z ddlmZ ddlZddlmZ ddlmZ ddlZ	ddl
mZ ddlmZ ddlmZ  e¦   «         Z G d	„ d
¦  «        ZdS )z9OpenVINO inference utilities for exported RF-DETR models.é    )ÚannotationsN)ÚPath)ÚAny)ÚNDArray)Ú_check_openvino_available)Ú
get_loggerc                  ó,   — e Zd ZdZddd„Zdd„Zdd„ZdS )ÚOpenVINOInferenceu•  Inference wrapper for OpenVINO IR models.

    Import it from its public path, :mod:`rfdetr.export.inference` â€” this module is private and its
    location is not part of the public API.

    Session-tier by design: it takes already-preprocessed NCHW tensors and returns the model's raw
    output tensors. Decoding those into detections is the caller's job (see
    :doc:`the export guide </exports/index>`).

    A single instance is safe to call from multiple threads: ``infer()`` is guarded by an
    internal lock, since OpenVINO's ``InferRequest.infer()`` is not thread-safe on a shared
    request object (concurrent calls would silently corrupt each other's output buffers).
    The lock serializes calls made through the same instance; for parallel throughput, create
    one ``OpenVINOInference`` per worker thread instead.

    Example:
        .. code-block:: python

            from rfdetr.export.inference import OpenVINOInference

            model = OpenVINOInference("output/inference_model.xml")
            # Prepare input image (NCHW format, ImageNet normalized)
            outputs = model.infer(image_array)
            boxes, labels = outputs
    ÚAUTONÚ
model_pathú
str | PathÚdeviceÚstrÚ	cache_dirú
str | NoneÚreturnÚNonec                óN  ‡ — t          ¦   «          ddl}t          |¦  «        }|                     ¦   «         st	          d|› �¦  «        ‚|                     ¦   «         }|�|                     d|i¦  «         |                     |¦  «        }|                     ||¦  «        ‰ _	        ‰ j	         
                    ¦   «         ‰ _        t          j        ¦   «         ‰ _        ‰ j	                             d¦  «        ‰ _        ˆ fd„t#          t%          ‰ j	        j        ¦  «        ¦  «        D ¦   «         ‰ _        t*                               d|› �¦  «         t*                               d‰ j        j        › �¦  «         t*                               dt%          ‰ j        ¦  «        › �¦  «         dS )	a8  Initialize OpenVINO inference session.

        Args:
            model_path: Path to the OpenVINO IR model (.xml file).
            device: Device the model is compiled for, e.g. ``"AUTO"``, ``"CPU"``, ``"GPU"`` or ``"NPU"``.
            cache_dir: Directory holding the compiled-model cache. When set, OpenVINO reuses the
                compiled kernels across process starts instead of recompiling the model every time.

        Raises:
            ImportError: If OpenVINO is not installed.
            FileNotFoundError: If the model file doesn't exist.
        r   NzModel file not found: Ú	CACHE_DIRc                óD   •— g | ]}‰j                              |¦  «        ‘ŒS © )Úcompiled_modelÚoutput©Ú.0ÚiÚselfs     €úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/rfdetr/export/_openvino/inference.pyú
<listcomp>z.OpenVINOInference.__init__.<locals>.<listcomp>U   s*   ø€ ÐmÐmÐmÀ˜dÔ1×8Ò8¸Ñ;Ô;ÐmÐmÐmó    zLoaded OpenVINO model from zInput shape: zNumber of outputs: )r   Úopenvinor   ÚexistsÚFileNotFoundErrorÚCoreÚset_propertyÚ
read_modelÚcompile_modelr   Úcreate_infer_requestÚinfer_requestÚ	threadingÚLockÚ_infer_lockÚinputÚinput_layerÚrangeÚlenÚoutputsÚoutput_layersÚloggerÚinfoÚpartial_shape)r   r   r   r   ÚovÚcoreÚmodels   `      r   Ú__init__zOpenVINOInference.__init__3   s‹  ø€ õ 	"Ñ#Ô#Ð#ØÐÐÐå˜*Ñ%Ô%ˆ
Ø× Ò Ñ"Ô"ð 	KÝ#Ð$I¸ZÐ$IÐ$IÑJÔJÐJð �wŠw‰yŒyˆØÐ à×Ò˜{¨IÐ6Ñ7Ô7Ð7Ø—’ 
Ñ+Ô+ˆØ"×0Ò0°¸Ñ?Ô?ˆÔØ!Ô0×EÒEÑGÔGˆÔõ %œ>Ñ+Ô+ˆÔð  Ô.×4Ò4°QÑ7Ô7ˆÔØmÐmÐmÐmÅUÍ3ÈtÔObÔOjÑKkÔKkÑElÔElÐmÑmÔmˆÔå�ŠÐ>°*Ð>Ð>Ñ?Ô?Ð?Ý�ŠÐD DÔ$4Ô$BÐDÐDÑEÔEÐEÝ�ŠÐC­#¨dÔ.@Ñ*AÔ*AÐCÐCÑDÔDÐDÐDÐDr    Ú
input_dataúNDArray[Any]útuple[NDArray[Any], ...]c           	     óŒ  ‡ — |j         t          j        k    s|j        d         s&t	          d|j         › d|j        d         › d�¦  «        ‚‰ j        5  ‰ j                             ‰ j        |i¦  «         t          ˆ fd„t          t          ‰ j        ¦  «        ¦  «        D ¦   «         ¦  «        cddd¦  «         S # 1 swxY w Y   dS )a  Run inference on input data.

        Args:
            input_data: Input tensor in NCHW format (batch, channels, height, width),
                dtype ``float32`` and C-contiguous. Should be ImageNet normalized
                [0.485, 0.456, 0.406] mean, [0.229, 0.224, 0.225] std.

        Returns:
            Tuple of output tensors (typically boxes, labels, and optionally masks/keypoints).
            Each array is a copy, so results stay valid after the next ``infer()`` call.

        Raises:
            ValueError: If *input_data* is not ``float32`` or not C-contiguous. OpenVINO
                accepts a mismatched buffer without erroring and converts to fp32 internally,
                doubling the buffer size shipped across the runtime boundary on every call.
        ÚC_CONTIGUOUSz9infer() requires a C-contiguous float32 array, got dtype=z contiguous=zc. Construct mean/std with dtype=np.float32 and finish preprocessing with np.ascontiguousarray(...).c              3  óz   •K  — | ]5}t          j        ‰j                             |¦  «        j        ¦  «        V — Œ6d S )N)ÚnpÚcopyr)   Úget_output_tensorÚdatar   s     €r   ú	<genexpr>z*OpenVINOInference.infer.<locals>.<genexpr>y   sB   øè è € ÐwÐwÐST�œ Ô!3×!EÒ!EÀaÑ!HÔ!HÔ!MÑNÔNÐwÐwÐwÐwÐwÐwr    N)Údtyper@   Úfloat32ÚflagsÚ
ValueErrorr,   r)   Úinferr.   Útupler/   r0   r2   ©r   r:   s   ` r   rI   zOpenVINOInference.infer[   sB  ø€ ð" Ô�rœzÒ)Ð)°Ô1AÀ.Ô1QÐ)Ýð\ÈJÔL\ð \ð \Ø(Ô.¨~Ô>ð\ð \ð \ñô ð ð Ôð 	xð 	xàÔ×$Ò$ dÔ&6¸
Ð%CÑDÔDÐDõ ÐwÐwÐwÐwÕX]Õ^aÐbfÔbtÑ^uÔ^uÑXvÔXvÐwÑwÔwÑwÔwð	xð 	xð 	xð 	xñ 	xô 	xð 	xð 	xð 	xð 	xð 	xð 	xøøøð 	xð 	xð 	xð 	xð 	xð 	xs   ÁAB9Â9B=Ã B=c                ó,   — |                       |¦  «        S )z<Alias for infer() to match typical model calling convention.)rI   rK   s     r   Ú__call__zOpenVINOInference.__call__{   s   € à�zŠz˜*Ñ%Ô%Ð%r    )r   N)r   r   r   r   r   r   r   r   )r:   r;   r   r<   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   rI   rM   r   r    r   r
   r
      sj   € € € € € ðð ð4&Eð &Eð &Eð &Eð &EðPxð xð xð xð@&ð &ð &ð &ð &ð &r    r
   )rQ   Ú
__future__r   r*   Úpathlibr   Útypingr   Únumpyr@   Únumpy.typingr   Ú rfdetr.export._openvino.exporterr   Úrfdetr.utilities.loggerr   r3   r
   r   r    r   ú<module>rY      sÙ   ðð @Ð ?à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  à FÐ FÐ FÐ FÐ FÐ FØ .Ð .Ð .Ð .Ð .Ð .à	ˆ‰Œ€ðe&ð e&ð e&ð e&ð e&ñ e&ô e&ð e&ð e&ð e&r    