§
    >²¸j‘  ã                   ód  — d dl Z 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mZ d dlmZ d dlmZ d dlmZ  eded	ef         ¬
¦  «        Zdedefd„Z eedd¬¦  «        dedefd„¦   «         Zdedefd„Zdedefd„Z eedd¬¦  «        dedefd„¦   «         Z eedd¬¦  «        dedefd„¦   «         Zdee
j        ej                 ej        z           dee
j        ej                          fd„Zdej        de
j        ej                 fd„Z de
j        ej                 dej        fd„Z!dS )é    N)ÚCallable)ÚAnyÚTypeVarÚcast)Ú
deprecatedÚvoid)ÚImage)Ú_cv2)Ú	ImageTypeÚF.)ÚboundÚannotate_funcÚreturnc                 ó´   ‡ — t          j        ‰ ¦  «        dt          dt          dt          dt          dt          f
ˆ fd„¦   «         }t	          t
          |¦  «        S )aD  
    Decorates `BaseAnnotator.annotate` implementations, converts scene to
    an image type used internally by the annotators, converts back when annotation
    is complete.

    Assumes the annotators modify the scene in-place.

    Raises:
        TypeError: If `scene` is not a `numpy.ndarray` or `PIL.Image.Image`.
    ÚselfÚsceneÚargsÚkwargsr   c                 óD  •— t          |t          j        ¦  «        r ‰| |g|¢R i |¤ŽS t          |t          j        ¦  «        r@t	          |¦  «        } ‰| |g|¢R i |¤Ž}|                     t          |¦  «        ¦  «         |S t          dt          |¦  «        › �¦  «        ‚©NzUnsupported image type: )	Ú
isinstanceÚnpÚndarrayr	   Úpillow_to_cv2ÚpasteÚcv2_to_pillowÚ	TypeErrorÚtype)r   r   r   r   Úscene_npÚannotated_npr   s         €úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/utils/conversion.pyÚwrapperz2ensure_cv2_image_for_class_method.<locals>.wrapper   s·   ø€ å�e�RœZÑ(Ô(ð 	?Ø �=  uÐ>¨tÐ>Ð>Ð>°vÐ>Ð>Ð>å�e�Uœ[Ñ)Ô)ð 	Ý$ UÑ+Ô+ˆHØ(˜=¨¨xÐI¸$ÐIÐIÐIÀ&ÐIÐIˆLØ�KŠK� lÑ3Ô3Ñ4Ô4Ð4ØˆLåÐ@µ4¸±;´;Ð@Ð@ÑAÔAÐAó    ©Ú	functoolsÚwrapsr   r   r   r   ©r   r"   s   ` r!   Ú!ensure_cv2_image_for_class_methodr(      sx   ø€ õ „_�]Ñ#Ô#ð
B•cð 
B¥)ð 
BµCð 
BÅ3ð 
BÍ3ð 
Bð 
Bð 
Bð 
Bð 
Bñ $Ô#ð
Bõ •�7ÑÔÐr#   z0.27.0z0.31.0)ÚtargetÚdeprecated_inÚ	remove_inc                 óF   — t          t          t          | ¦  «        ¦  «        S ©N©r   r   r   ©r   s    r!   Úensure_cv2_image_for_annotationr0   .   ó   € õ ••4˜Ñ&Ô&Ñ'Ô'Ð'r#   Úimage_processing_func           	      ó¦   ‡ — t          j        ‰ ¦  «        dt          dt          dt          dt          fˆ fd„¦   «         }t	          t
          |¦  «        S )a/  
    Decorates image processing functions that accept np.ndarray, converting `image` to
    np.ndarray, converts back when processing is complete.

    Assumes the annotators do NOT modify the scene in-place.

    Raises:
        TypeError: If `image` is not a `numpy.ndarray` or `PIL.Image.Image`.
    Úimager   r   r   c                 ó  •— t          | t          j        ¦  «        r ‰| g|¢R i |¤ŽS t          | t          j        ¦  «        r*t	          | ¦  «        } ‰|g|¢R i |¤Ž}t          |¦  «        S t          dt          | ¦  «        › �¦  «        ‚r   )r   r   r   r	   r   r   r   r   )r4   r   r   r   Ú	annotatedr2   s        €r!   r"   z9ensure_cv2_image_for_standalone_function.<locals>.wrapperF   s¥   ø€ å�e�RœZÑ(Ô(ð 	@Ø'Ð'¨Ð?°Ð?Ð?Ð?¸Ð?Ð?Ð?å�e�Uœ[Ñ)Ô)ð 	,Ý! %Ñ(Ô(ˆEØ,Ð,¨UÐD°TÐDÐDÐD¸VÐDÐDˆIÝ  Ñ+Ô+Ð+åÐ@µ4¸±;´;Ð@Ð@ÑAÔAÐAr#   )r%   r&   r   r   r   r   )r2   r"   s   ` r!   Ú(ensure_cv2_image_for_standalone_functionr7   9   sq   ø€ õ „_Ð)Ñ*Ô*ð	B•yð 	B­ð 	B½ð 	BÅð 	Bð 	Bð 	Bð 	Bð 	Bñ +Ô*ð	Bõ •�7ÑÔÐr#   c                 ó´   ‡ — t          j        ‰ ¦  «        dt          dt          dt          dt          dt          f
ˆ fd„¦   «         }t	          t
          |¦  «        S )a'  
    Decorates image processing functions that accept np.ndarray, converting `image` to
    PIL image, converts back when processing is complete.

    Assumes the annotators modify the scene in-place.

    Raises:
        TypeError: If `scene` is not a `numpy.ndarray` or `PIL.Image.Image`.
    r   r   r   r   r   c                 ój  •— t          |t          j        ¦  «        r@t          |¦  «        } ‰| |g|¢R i |¤Ž}t          j        |t          |¦  «        ¦  «         |S t          |t          j        ¦  «        r t          t           ‰| |g|¢R i |¤Ž¦  «        S t          dt          |¦  «        › �¦  «        ‚r   )r   r   r   r   Úcopytor   r	   r   r   r   r   )r   r   r   r   Ú	scene_pilÚannotated_pilr   s         €r!   r"   z2ensure_pil_image_for_class_method.<locals>.wrapperb   sÂ   ø€ å�e�RœZÑ(Ô(ð 	Ý% eÑ,Ô,ˆIØ)˜M¨$°	ÐK¸DÐKÐKÐKÀFÐKÐKˆMÝŒI�e�]¨=Ñ9Ô9Ñ:Ô:Ð:ØˆLå�e�Uœ[Ñ)Ô)ð 	PÝ�	 = =°°uÐ#N¸tÐ#NÐ#NÐ#NÀvÐ#NÐ#NÑOÔOÐOåÐ@µ4¸±;´;Ð@Ð@ÑAÔAÐAr#   r$   r'   s   ` r!   Ú!ensure_pil_image_for_class_methodr=   U   sx   ø€ õ „_�]Ñ#Ô#ð
B•cð 
B¥)ð 
BµCð 
BÅ3ð 
BÍ3ð 
Bð 
Bð 
Bð 
Bð 
Bñ $Ô#ð
Bõ •�7ÑÔÐr#   c                 óF   — t          t          t          | ¦  «        ¦  «        S r-   r.   r/   s    r!   Úensure_pil_image_for_annotationr?   r   r1   r#   c                 óF   — t          t          t          | ¦  «        ¦  «        S r-   r.   )r2   s    r!   Úensure_cv2_image_for_processingrA   }   s   € õ ••4Ð,Ñ-Ô-Ñ.Ô.Ð.r#   Úimagesc                 ó¸   — g }| D ]T}t          |t          j        ¦  «        r#|                     t          |¦  «        ¦  «         Œ?|                     |¦  «         ŒU|S )zö
    Converts images provided either as Pillow images or OpenCV
    images into OpenCV format.

    Args:
        images: Images to be converted

    Returns:
        List of input images in OpenCV format
            (with order preserved).

    )r   r	   Úappendr   )rB   Úresultr4   s      r!   Úimages_to_cv2rF   ˆ   sc   € ð +-€FØð !ð !ˆÝ�e�Uœ[Ñ)Ô)ð 	!Ø�MŠM�-¨Ñ.Ô.Ñ/Ô/Ð/Ð/à�MŠM˜%Ñ Ô Ð Ð Ø€Mr#   r4   c                 óâ  — | j         dk    r|                      d¦  «        } t          j        | ¦  «        }|j        dk    rIt          t          j        t          j                 | 	                    t          j        d¬¦  «        ¦  «        S t          j        |t          j        ¦  «        }t          t          j        t          j                 | 	                    t          j        d¬¦  «        ¦  «        S )aý  
    Converts Pillow image into OpenCV image, handling RGB -> BGR
    conversion. Palette images are first expanded to RGB so palette indices are
    resolved to their actual colors. RGBA images are converted to BGR, matching
    OpenCV by dropping the alpha channel.

    Args:
        image: Pillow image in RGB, RGBA, grayscale, or palette mode.

    Returns:
        Input image converted to OpenCV format.

    Examples:
        ```pycon
        >>> from PIL import Image
        >>> from supervision.utils.conversion import pillow_to_cv2
        >>> image = Image.new("RGB", (10, 10), color=(255, 0, 0))
        >>> scene = pillow_to_cv2(image)
        >>> scene.shape
        (10, 10, 3)
        >>> scene[0, 0].tolist()
        [0, 0, 255]

        ```
    ÚPÚRGBé   F)Úcopy)ÚmodeÚconvertr   ÚarrayÚndimr   ÚnptÚNDArrayÚuint8ÚastypeÚcv2ÚcvtColorÚCOLOR_RGB2BGR)r4   r   s     r!   r   r       s¦   € ð4 „z�SÒÐØ—’˜eÑ$Ô$ˆåŒH�U‰OŒO€EØ„z�Q‚€Ý•C”K¥¤Ô)¨5¯<ª<½¼Àu¨<Ñ+MÔ+MÑNÔNÐNÝŒL˜¥Ô 1Ñ2Ô2€Eõ •”�BœHÔ% u§|¢|µB´HÀ5 |Ñ'IÔ'IÑJÔJÐJr#   c                 óÊ  — | j         dk    r&t          j        t          j        | ¦  «        ¦  «        S | j         dk    rD| j        d         dk    r3t          j        | t          j        ¦  «        }t          j        |¦  «        S | j         dk    rA| j        d         dk    r0t          j        t          j        | dg d¢f         ¦  «        ¦  «        S t          d| j        › d�¦  «        ‚)ul  
    Converts an OpenCV image into a Pillow image, reordering channels from
    OpenCV's BGR(A) convention to Pillow's RGB(A).

    Args:
        image: OpenCV image. Accepted shapes:
            - `(H, W)` â€” grayscale, passed through unchanged.
            - `(H, W, 3)` â€” BGR, converted to RGB.
            - `(H, W, 4)` â€” BGRA, converted to RGBA.

    Returns:
        Input image converted to Pillow format.

    Raises:
        ValueError: If `image` is not 2-D or 3-D with 3 or 4 channels.

    Examples:
        ```pycon
        >>> import numpy as np
        >>> from supervision.utils.conversion import cv2_to_pillow
        >>> scene = np.zeros((10, 10, 3), dtype=np.uint8)
        >>> scene[:, :, 2] = 255
        >>> image = cv2_to_pillow(scene)
        >>> image.size
        (10, 10)
        >>> image.getpixel((0, 0))
        (255, 0, 0)

        ```
    rJ   é   é   .)rJ   é   r   rX   z/Expected shape (H,W), (H,W,3), or (H,W,4), got ú.)
rO   r	   Ú	fromarrayr   ÚascontiguousarrayÚshaperT   rU   ÚCOLOR_BGR2RGBÚ
ValueError)r4   Ú	rgb_images     r!   r   r   Æ   sÉ   € ð> „z�Q‚€ÝŒ�rÔ3°EÑ:Ô:Ñ;Ô;Ð;Ø„z�Q‚€˜5œ; qœ>¨QÒ.Ð.Ý”L ­Ô(9Ñ:Ô:ˆ	ÝŒ˜yÑ)Ô)Ð)Ø„z�Q‚€˜5œ; qœ>¨QÒ.Ð.ÝŒ�rÔ3°E¸#¸|¸|¸|Ð:KÔ4LÑMÔMÑNÔNÐNÝ
ÐUÀuÄ{ÐUÐUÐUÑ
VÔ
VÐVr#   )"r%   Úcollections.abcr   Útypingr   r   r   Únumpyr   Únumpy.typingrP   Ú	deprecater   r   ÚPILr	   Úsupervisionr
   rT   Úsupervision.draw.baser   r   r(   r0   r7   r=   r?   rA   ÚlistrQ   rR   rF   r   r   © r#   r!   ú<module>rl      s×  ðØ Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ø %Ð %Ð %Ð %Ð %Ð %Ð %Ð %Ð %Ð %à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ø Ð Ð Ð Ð Ð à #Ð #Ð #Ð #Ð #Ð #Ø +Ð +Ð +Ð +Ð +Ð +à€GˆC�x  S Ô)Ð*Ñ*Ô*€ðØðàðð ð ð ð< €Ø,ØØðñ ô ð
(Øð(àð(ð (ð (ñô ð
(ðØðàðð ð ð ð8Øðàðð ð ð ð: €Ø,ØØðñ ô ð
(Øð(àð(ð (ð (ñô ð
(ð €Ø3ØØðñ ô ð
/Øð/àð/ð /ð /ñô ð
/ðØ�”˜RœXÔ&¨¬Ñ4Ô5ðà	ˆ#Œ+�b”hÔ
Ô ðð ð ð ð0#K˜œð #K¨¬°R´XÔ)>ð #Kð #Kð #Kð #KðL&W˜œ R¤XÔ.ð &W°5´;ð &Wð &Wð &Wð &Wð &Wð &Wr#   