o
    e                     @   sZ   d dl Z d dlZd dlmZ d dlZd dlZdejdejdejfddZG dd	 d	Z	dS )
    N)Optionalimagexyxyreturnc                 C   s4   t |t}|\}}}}| ||||f }|S )a  
    Crops the given image based on the given bounding box.

    Args:
        image (np.ndarray): The image to be cropped, represented as a numpy array.
        xyxy (np.ndarray): A numpy array containing the bounding box coordinates
            in the format (x1, y1, x2, y2).

    Returns:
        (np.ndarray): The cropped image as a numpy array.

    Examples:
        ```python
        >>> import supervision as sv

        >>> detection = sv.Detections(...)
        >>> with sv.ImageSink(target_dir_path='target/directory/path') as sink:
        ...     for xyxy in detection.xyxy:
        ...         cropped_image = sv.crop_image(image=image, xyxy=xyxy)
        ...         sink.save_image(image=image)
        ```
    )nproundastypeint)r   r   x1y1x2y2cropped_img r   F/var/www/myenv/lib/python3.10/site-packages/supervision/utils/image.py
crop_image	   s   r   c                   @   sR   e Zd Z		ddededefddZdd	 Zddejde	e fddZ
dd Zd
S )	ImageSinkFimage_{:05d}.pngtarget_dir_path	overwriteimage_name_patternc                 C   s   || _ || _|| _d| _dS )a:  
        Initialize a context manager for saving images.

        Args:
            target_dir_path (str): The target directory where images will be saved.
            overwrite (bool, optional): Whether to overwrite the existing directory.
                Defaults to False.
            image_name_pattern (str, optional): The image file name pattern.
                Defaults to "image_{:05d}.png".

        Examples:
            ```python
            >>> import supervision as sv

            >>> with sv.ImageSink(target_dir_path='target/directory/path',
            ...                   overwrite=True) as sink:
            ...     for image in sv.get_video_frames_generator(
            ...         source_path='source_video.mp4', stride=2):
            ...         sink.save_image(image=image)
            ```
        r   N)r   r   r   image_count)selfr   r   r   r   r   r   __init__(   s   
zImageSink.__init__c                 C   s@   t j| jr| jrt| j t | j | S t | j | S N)ospathexistsr   r   shutilrmtreemakedirs)r   r   r   r   	__enter__I   s   zImageSink.__enter__Nr   
image_namec                 C   sD   |du r| j | j}tj| j|}t|| |  jd7  _dS )a:  
        Save a given image in the target directory.

        Args:
            image (np.ndarray): The image to be saved.
            image_name (str, optional): The name to use for the saved image.
                If not provided, a name will be
                generated using the `image_name_pattern`.
        N   )	r   formatr   r   r   joinr   cv2imwrite)r   r   r"   
image_pathr   r   r   
save_imageS   s
   
zImageSink.save_imagec                 C   s   d S r   r   )r   exc_type	exc_valueexc_tracebackr   r   r   __exit__d   s   zImageSink.__exit__)Fr   r   )__name__
__module____qualname__strboolr   r!   r   ndarrayr   r)   r-   r   r   r   r   r   '   s    
!
r   )
r   r   typingr   r&   numpyr   r3   r   r   r   r   r   r   <module>   s    