
    3j$#                        S SK Jr  S SKJrJrJr  S SKrS SKJr  S SKJ	r	  S SK
Jr  S SK
Jr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJrJr  SS/r " S S\\5      r " S S5      rg)    )annotations)AnyOptionalUnionN)nn)grayscale_to_rgb)PILImage)onnx)ONNXExportMixin)	Normalize)	ModelBase)DexiNed)ResizePostProcessorResizePreProcessorEdgeDetectorEdgeDetectorBuilderc                    ^  \ rS rSr% SrSrS\S'    S         SU 4S jjjr\SS j5       r	\
R                  " 5       SS j5       r  S       SS	 jjr   S         SS
 jjr     S             SS jjrSrU =r$ )r   %   a  EdgeDetector is a module that wraps an edge detection model.

This is a high-level API that wraps edge detection models like :py:class:`kornia.models.DexiNed`.

Args:
    model: The edge detection model.
    pre_processor: Pre-processing module (e.g., ResizePreProcessor).
    post_processor: Post-processing module (e.g., ResizePostProcessor).
    name: Optional name for the detector.

Example:
    >>> from kornia.models.dexined import DexiNed
    >>> from kornia.models.processors import ResizePreProcessor, ResizePostProcessor
    >>> model = DexiNed(pretrained=True)
    >>> detector = EdgeDetector(model, ResizePreProcessor(352, 352), ResizePostProcessor())
    >>> img = torch.rand(1, 3, 320, 320)
    >>> out = detector(img)

edge_detectionstrnamec                x   > [         TU ]  5         UR                  5       U l        X l        X0l        Ub  X@l        gg)zInitialize EdgeDetector.

Args:
    model: The edge detection model.
    pre_processor: Pre-processing module (e.g., ResizePreProcessor).
    post_processor: Post-processing module (e.g., ResizePostProcessor).
    name: Optional name for the detector.

N)super__init__evalmodelpre_processorpost_processorr   )selfr   r   r   r   	__class__s        W/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/contrib/edge_detection.pyr   EdgeDetector.__init__<   s9      	ZZ\
*,I     c                    [        S5      e)a%  Build EdgeDetector from config.

This is a placeholder to satisfy the abstract method requirement.
Use EdgeDetectorBuilder.build() or instantiate EdgeDetector directly.

Args:
    config: Configuration object (not used, kept for interface compatibility).

Returns:
    EdgeDetector instance.

ztEdgeDetector.from_config() is not implemented. Use EdgeDetectorBuilder.build() or instantiate EdgeDetector directly.)NotImplementedError)configs    r!   from_configEdgeDetector.from_configS   s     "T
 	
r#   c                l    U R                  U5      u  pU R                  U5      nU R                  X25      $ )zForward pass of the edge detection model.

Args:
    images: If list of RGB images. Each image is a torch.Tensor with shape :math:`(3, H, W)`.
        If torch.Tensor, a torch.Tensor with shape :math:`(B, 3, H, W)`.

Returns:
    output torch.Tensor.

)r   r   r   )r   imagesimage_sizes
out_imagess       r!   forwardEdgeDetector.forwardf   s6     #008ZZ'
"":;;r#   c                    Uc  U R                  U5      n/ nU H   nUR                  [        U5      S   5        M"     U R                  XC[	        U[
        R                  5      S9$ )zDraw the edge detection results.

Args:
    images: input torch.Tensor.
    edge_maps: detected edges.
    output_type: type of the output.

Returns:
    output torch.Tensor.

r   )is_batch)r-   appendr   _tensor_to_type
isinstancetorchTensor)r   r*   	edge_mapsoutput_typeoutputedge_maps         r!   	visualizeEdgeDetector.visualizev   sd    " V,I!HMM*84Q78 " ##F*VUZUaUaBb#ccr#   c                h    U R                  XU5      nU R                  XSS9  U R                  XSSS9  g)zSave the edge detection results.

Args:
    images: input torch.Tensor.
    edge_maps: detected edges.
    output_type: type of the output.
    directory: where to save outputs.

Returns:
    output torch.Tensor.

_src)suffix_edgeN)r:   _save_outputs)r   r*   r6   	directoryr7   outputss         r!   saveEdgeDetector.save   s:    & ..K@6V<7g>r#   c           
        Uc  SU R                    SU S3n[        R                  " U U4SSU=(       d    SU=(       d    S/SSU=(       d    SU=(       d    S/SSU=(       d    SU=(       d    S/U(       a  U OU R                  UUS.UD6$ )	a  Export the current edge detection model to an ONNX model file.

Args:
    onnx_name:
        The name of the output ONNX file. If not provided, a default name in the
        format "Kornia-<ClassName>.onnx" will be used.
    image_size:
        The size to which input images will be resized during preprocessing.
        If None, image_size will be dynamic. For DexiNed, recommended scale is 352.
    include_pre_and_post_processor:
        Whether to include the pre-processor and post-processor in the exported model.
    save:
        If to save the model or load it.
    additional_metadata:
        Additional metadata to add to the ONNX model.
    kwargs: Additional arguments to convert to onnx.

kornia__z.onnx      `  )input_shapeoutput_shapepseudo_shaper   rC   additional_metadata)r   r   to_onnxr   )r   	onnx_name
image_sizeinclude_pre_and_post_processorrC   rO   kwargss          r!   rP   EdgeDetector.to_onnx   s    6 !$))Aj\?I&&

 Q
 0b*2BCa!1r:3CDQ
 1c:3DE8$djj 3

 

 
	
r#   )r   r   r   r   )N)
r   torch.nn.Moduler   rV   r   rV   r   Optional[str]returnNone)r&   r   rX   r   )r*   'Union[torch.Tensor, list[torch.Tensor]]rX   rZ   )Nr4   )r*   rZ   r6   1Optional[Union[torch.Tensor, list[torch.Tensor]]]r7   r   rX   z:Union[torch.Tensor, list[torch.Tensor], list[Image.Image]])NNr4   )
r*   rZ   r6   r[   rA   rW   r7   r   rX   rY   )NrK   TTN)rQ   rW   rR   zOptional[int]rS   boolrC   r\   rO   zOptional[list[tuple[str, str]]]rT   r   rX   zonnx.ModelProto)__name__
__module____qualname____firstlineno____doc__r   __annotations__r   staticmethodr'   r4   inference_moder-   r:   rC   rP   __static_attributes____classcell__)r    s   @r!   r   r   %   sb   ( !D#  # ' (	
  
 . 
 
$ < <$ HL"	d7d Ed 	d
 
Dd8 HL#'"?7? E? !	?
 ? 
?2 $($'/3?C(
 (
 "(
 )-	(

 (
 =(
 (
 
(
 (
r#   c                  0    \ rS rSrSr\SSS jj5       rSrg)r      a'  EdgeDetectorBuilder is a class that builds an edge detection model.

This is a high-level API that builds edge detection models like :py:class:`kornia.models.DexiNed`
and wraps them with :py:class:`EdgeDetector`.

Note:
    To use this model, load image tensors and call ``model.save(images)``.
c                \   U R                  5       S:X  am  [        [        R                  " / SQ/5      [        R                  " S/S-  /5      S9n[        R
                  " U[        US9[        R                  " 5       5      nO[        SU  S35      e[        U[        X"5      [        5       SS	9$ )
a  Build an edge detection model.

Args:
    model_name: Name of the model to build. Currently only "dexined" is supported.
    pretrained: If True, loads pretrained weights.
    image_size: Size to which input images will be resized during preprocessing.

Returns:
    EdgeDetector instance configured with the specified model.

Example:
    >>> detector = EdgeDetectorBuilder.build(pretrained=True, image_size=352)
    >>> img = torch.rand(1, 3, 320, 320)
    >>> out = detector(img)

dexined)g
ףp=
?gv/?gCl?gp?rI   )meanstd)
pretrainedzModel z) not found. Please choose from 'dexined'.)r   )lowerr   r4   tensorr   
Sequentialr   Sigmoid
ValueErrorr   r   r   )
model_namerm   rR   normr   s        r!   buildEdgeDetectorBuilder.build   s    $ *%,,0E/F"GU\\\g[hkl[lZmMnoDMM$:(F

UEvj\1Z[\\z6!	
 	
r#    N)rj   TrK   )rs   r   rm   r\   rR   intrX   r   )r]   r^   r_   r`   ra   rc   ru   re   rw   r#   r!   r   r      s     
 
r#   )
__future__r   typingr   r   r   r4   r   kornia.color.grayr   kornia.core.externalr	   Imager
   kornia.core.mixin.onnxr   kornia.enhance.normalizer   kornia.models.baser   kornia.models.dexinedr   kornia.models.processorsr   r   __all__r   r   rw   r#   r!   <module>r      sT   $ # ' '   . 2 % 2 . ( ) L0
1i
9o i
X(
 (
r#   