
    3j%                        S SK Jr  S SKrS SKrS SKrS SKrS SKJrJr  S SK	r	S SK
Jr  S SKJr  S SKJrJr  S SKJr  / SQr\R(                  " \5      r " S	 S
\5      r      SS jr S     SS jjrSS jrg)    )annotationsN)AnyOptional)kornia_config)numpy)onnxrequests)CachedDownloader)
ONNXLoaderadd_metadataio_name_conversionc                      \ rS rSrSr\S
SS jj5       r\SSS jj5       r\SS j5       r	\SS j5       r
\SS j5       rSrg	)r   %   z_Manages ONNX models, handling local caching, downloading from Hugging Face, and loading models.c                b   UR                  S5      (       ad  U R                  " U[        R                  R	                  U5      S   4USS.UD6n[        U5       n[        R                  " U5      nUsSSS5        $ U(       d  [        SU S35      e[        SU S35      e! , (       d  f       N3= f)	a,  Load JSON config from the specified URL.

Args:
    url: The URL of the preprocessor config to load.
    download: If True, the config will be downloaded if it's not already in the local cache.
    kwargs: Additional download arguments.

Returns:
    dict[str, Any]: The loaded preprocessor config.

)zhttp:zhttps:z.jsondownloadsuffixNzFile `z)` not found. You may set `download=True`.z` not found.)	
startswithdownload_to_cacheospathsplitopenjsonloadRuntimeError)clsurlr   kwargs	file_pathf	json_datas          K/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/onnx/utils.pyload_configONNXLoader.load_config(   s     >>-..--c"2& "	
 I iA IIaL	  ! u,UVWWVC5566 !s   B  
B.c                   UR                  S5      (       a  U[        S5      S nSU S3nUR                  SS5      =(       d@    [        R                  R                  [        R                  UR                  S5      S   5      nUR                  SU05        U R                  " XQR                  S5      S   4USS	.UD6nU(       a.  SU S
3nU R                  " XR                  S5      S   4US
S	.UD6  [        R                  " U5      $ UR                  S5      (       d  UR                  S5      (       a  UR                  SS5      =(       d    [        R                  nUR                  SU05        U R                  " U[        R                  R                  U5      S   4USS	.UD6nU(       aE  USS S
-   nU R                  " U[        R                  R                  U5      S   SS S
-   4US
S	.UD6  [        R                  " U5      $ [        R                  R                  U5      (       a  [        R                  " U5      $ [        SU S35      e)a:  Load an ONNX model from the local cache or downloads it from Hugging Face if necessary.

Args:
    model_name: The name of the ONNX model or operator. For Hugging Face-hosted models,
        use the format 'hf://model_name'. Valid `model_name` can be found on
        https://huggingface.co/kornia/ONNX_models.
        Or a URL to the ONNX model.
    download: If True, the model will be downloaded from Hugging Face if it's not already in the local cache.
    cache_dir: The directory where the model should be cached.
        Defaults to None, which will use a default `{kornia_config.hub_onnx_dir}` directory.
    with_data: If True, the model will be loaded with its `.onnx_data` weights.
    **kwargs: Additional arguments to pass to the download method, if needed.

Returns:
    onnx.ModelProto: The loaded ONNX model.

zhf://Nz7https://huggingface.co/kornia/ONNX_models/resolve/main/z.onnx	cache_dir/r      r   z
.onnx_datazhttp://zhttps://r   zFile z
 not found)r   lengetr   r   joinr   hub_onnx_dirr   updater   r   r   exists
ValueError)	r   
model_namer   	with_datar    r   r(   r!   url_datas	            r$   
load_modelONNXLoader.load_modelF   sG   &   ))#CLN3JKJ<W\]C

;5 **J,<,<S,A!,D:I MM;	23--%%c*1-8@TZI TU_T``jk%%..s3A6AIR^bh 99Y''""9--1F1Fz1R1R

;5S9S9SIMM;	23--j)"- "	
 I %cr?\9%%GGMM*-b1#26E &'	
  99Y''WW^^J''99Z((5J788    c                    SU  3n[         R                  " USS9nUR                  S:X  a  UR                  5       $ [	        SUR                   35      e)zFetch the contents of the Hugging Face repository using the Hugging Face API.

Returns:
    A list of all files in the repository as dictionaries containing file details.

z?https://huggingface.co/api/models/kornia/ONNX_models/tree/main/
   )timeout   z%Failed to fetch repository contents: )r	   r-   status_coder   r2   )folderr   responses      r$   _fetch_repo_contentsONNXLoader._fetch_repo_contents   sV     PPVxX<<R03&==?"DXEYEYDZ[\\r8   c                    U R                  S5      nU Vs/ s H  o"S   PM	     nn[        R                  " U5        gs  snf )z[List all available ONNX operators in the 'operators' folder of the Hugging Face repository.	operatorsr   Nr@   pprintpp)r   repo_contentsfilerC   s       r$   list_operatorsONNXLoader.list_operators   s=     00= /<<md&\m	<		) =   =c                    U R                  S5      nU Vs/ s H  o"S   PM	     nn[        R                  " U5        gs  snf )zUList all available ONNX models in the 'models' folder of the Hugging Face repository.modelsr   NrD   )r   rG   rH   rM   s       r$   list_modelsONNXLoader.list_models   s=     00: ,99=4v,=9		& :rK    N)T)r   strr   boolr    r   returnzdict[str, Any])TF)r3   rQ   r   rR   r4   rR   rS   onnx.ModelProto)r>   rQ   rS   zlist[dict[str, Any]])rS   None)__name__
__module____qualname____firstlineno____doc__classmethodr%   r6   staticmethodr@   rI   rN   __static_attributes__rP   r8   r$   r   r   %   sj    i7 7: ;9 ;9z ] ]     r8   r   c                   [        [        U R                  R                  5      5       HM  nU R                  R                  U   R                  nX1;   d  M-  X   U R                  R                  U   l        MO     [        [        U R                  R
                  5      5       HM  nU R                  R
                  U   R                  nXA;   d  M-  X   U R                  R
                  U   l        MO     [        [        U R                  R                  5      5       GH&  n[        [        U R                  R                  U   R                  5      5       HX  nU R                  R                  U   R                  U   nXa;   d  M0  X   U R                  R                  U   R                  U'   MZ     [        [        U R                  R                  U   R
                  5      5       HX  nU R                  R                  U   R
                  U   nXq;   d  M0  X   U R                  R                  U   R
                  U'   MZ     GM)     U $ )zConvert the input and output names of an ONNX model to 'input' and 'output'.

Args:
    onnx_model: The ONNX model to convert.
    io_name_mapping: A dictionary mapping the original input and output names to the new ones.

)ranger,   graphinputnameoutputnode)
onnx_modelio_name_mappingiin_nameout_namejnode_in_namenode_out_names           r$   r   r      s    3z''--./""((+00%-<-EJ""1%* 0
 3z''../0##**1-22&.=.GJ##A&+ 1 3z'',,-.s:++00399:;A%++00399!<L.4C4Q
  %%a(..q1 <
 s:++003::;<A&,,11!4;;A>M/5D5S
  %%a(//2 = / r8   c                    Uc  / nSS[         R                  4/UQ H5  u  p#U R                  R                  5       nX$l        [        U5      Ul        M7     U $ )a  Add metadata to an ONNX model.

The metadata includes the source library (set to "kornia"), the version of kornia,
and any additional metadata provided as a list of key-value pairs.

Args:
    onnx_model: The ONNX model to add metadata to.
    additional_metadata: A list of tuples, where each tuple contains a key and a value
        for the additional metadata to add to the ONNX model.

Returns:
    The ONNX model with the added metadata.

)sourcekorniaversion)ro   __version__metadata_propsaddkeyrQ   value)re   additional_metadatart   ru   rr   s        r$   r   r      si    $ " 	F&&' 


 $22668 "5z r8   c           	     (   [         R                  [         R                  [         R                  [         R                  [         R
                  [         R                  [         R                  [         R                  S.nX;  a  [        SU  S35      eX   $ )N)ztensor(float)ztensor(float16)ztensor(double)ztensor(int32)ztensor(int64)ztensor(uint8)ztensor(int8)ztensor(bool)z
ONNX type z not understood)
npfloat32float16float64int32int64uint8int8bool_	TypeError)	onnx_typetype_mappings     r$   onnx_type_to_numpyr      sf    ::**	L $*YK?@@""r8   )re   rT   rf   zdict[str, str]rS   rT   )N)re   rT   rv   zOptional[list[tuple[str, str]]]rS   rT   )r   rQ   rS   r   )
__future__r   r   loggingr   rE   typingr   r   ro   kornia.configr   kornia.core.externalr   rx   r   r	   kornia.onnx.downloadr
   __all__	getLoggerrV   loggerr   r   r   r   rP   r8   r$   <module>r      s   $ #   	     ' , / 1
>			8	$B! BJ""#" "N <@8 >#r8   