o
    oi                     @  s~   d dl mZ d dlmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d	gZG d
d	 d	eZdS )    )annotations)AnyOptionalUnionN)grayscale_to_rgb)Tensor)PILImage)onnx)	ModelBaseEdgeDetectorc                      sp   e Zd ZU dZdZded< e d&dd	Z	
	d'd(ddZ		
	
	d)d*ddZ
	
				
d+d, fd$d%Z  ZS )-r   z<EdgeDetector is a module that wraps an edge detection model.edge_detectionstrnameimagesUnion[Tensor, list[Tensor]]returnc                 C  s$   |  |\}}| |}| ||S )a  Forward pass of the edge detection model.

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

        Returns:
            output tensor.

        )pre_processormodelpost_processor)selfr   image_sizes
out_images r   U/home/ubuntu/.local/lib/python3.10/site-packages/kornia/models/edge_detection/base.pyforward&   s   
zEdgeDetector.forwardNtorch	edge_maps%Optional[Union[Tensor, list[Tensor]]]output_type.Union[Tensor, list[Tensor], list[Image.Image]]c                 C  sH   |du r	|  |}g }|D ]}|t|d  q| j||t|tdS )zDraw the edge detection results.

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

        Returns:
            output tensor.

        Nr   )is_batch)r   appendr   _tensor_to_type
isinstancer   )r   r   r   r   outputedge_mapr   r   r   	visualize6   s   
zEdgeDetector.visualize	directoryOptional[str]Nonec                 C  s2   |  |||}| j||dd | j||dd dS )a
  Save the edge detection results.

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

        Returns:
            output tensor.

        _src)suffix_edgeN)r&   _save_outputs)r   r   r   r'   r   outputsr   r   r   saveO   s   zEdgeDetector.save`  T	onnx_name
image_sizeOptional[int]include_pre_and_post_processorboolr/   additional_metadataOptional[list[tuple[str, str]]]kwargsr   onnx.ModelProtoc              	     sx   |du rd| j  d| d}t j|fdd|pd|pdgdd|p!d|p$dgdd|p*d|p-dg|r2| n| j||d	|S )
a9  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.

        Nkornia__z.onnx      r0   )input_shapeoutput_shapepseudo_shaper   r/   r6   )r   superto_onnxr   )r   r1   r2   r4   r/   r6   r8   	__class__r   r   rC   f   s   zEdgeDetector.to_onnx)r   r   r   r   )Nr   )r   r   r   r   r   r   r   r   )NNr   )
r   r   r   r   r'   r(   r   r   r   r)   )Nr0   TTN)r1   r(   r2   r3   r4   r5   r/   r5   r6   r7   r8   r   r   r9   )__name__
__module____qualname____doc__r   __annotations__r   inference_moder   r&   r/   rC   __classcell__r   r   rD   r   r   !   s$   
 )
__future__r   typingr   r   r   r   kornia.color.grayr   kornia.corer   kornia.core.externalr   Imager	   kornia.models.baser
   __all__r   r   r   r   r   <module>   s   