§
    >²¸juY  ã                  ón  — d dl mZ d dl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mZmZ d dlmZ d dl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  ee ¦  «        Z!e
 G d„ d¦  «        ¦   «         Z" G d„ d¦  «        Z#	 d2d3d„Z$	 	 	 	 	 d4d5d!„Z%d5d"„Z&dd#d#dd$dd%œd6d/„Z' G d0„ d1¦  «        Z(dS )7é    )ÚannotationsN)Údeque)ÚCallableÚ	Generator)Ú	dataclass)ÚEmptyÚFullÚQueue)ÚTracebackType)Úcast)Útqdm)Ú_cv2)Ú
_mux_audio)Ú_get_loggerc                  óp   — e Zd ZU dZded<   ded<   ded<   dZded	<   edd„¦   «         Zedd„¦   «         Z	dS )Ú	VideoInfoaõ  
    A class to store video information, including width, height, fps and
        total number of frames.

    Attributes:
        width: width of the video in pixels
        height: height of the video in pixels
        fps: frames per second of the video as a float. Common values include
            23.976, 24.0, 25.0, 29.97, 30.0, 59.94, and 60.0.
        total_frames: total number of frames in the video,
            default is None

    Examples:
        ```python
        import supervision as sv

        video_info = sv.VideoInfo.from_video_path(video_path="<SOURCE_VIDEO_FILE>")

        video_info
        # VideoInfo(width=3840, height=2160, fps=25.0, total_frames=538)

        video_info.resolution_wh
        # (3840, 2160)
        ```
    ÚintÚwidthÚheightÚfloatÚfpsNú
int | NoneÚtotal_framesÚ
video_pathÚstrÚreturnc                ó"  — t          j        |¦  «        }|                     ¦   «         st          d|› �¦  «        ‚t	          |                     t           j        ¦  «        ¦  «        }t	          |                     t           j        ¦  «        ¦  «        }t          |                     t           j	        ¦  «        ¦  «        }t	          |                     t           j
        ¦  «        ¦  «        }|                     ¦   «          t          ||||¦  «        S )aÑ  Read video metadata from a file path.

        Args:
            video_path: Path to the video file.

        Returns:
            A `VideoInfo` instance with width, height, fps, and total_frames.

        Examples:
            ```python
            import supervision as sv

            video_info = sv.VideoInfo.from_video_path(video_path="<SOURCE_VIDEO_FILE>")
            # VideoInfo(width=3840, height=2160, fps=25.0, total_frames=538)
            ```
        úCould not open video at )Úcv2ÚVideoCaptureÚisOpenedÚ	Exceptionr   ÚgetÚCAP_PROP_FRAME_WIDTHÚCAP_PROP_FRAME_HEIGHTr   ÚCAP_PROP_FPSÚCAP_PROP_FRAME_COUNTÚreleaser   )Úclsr   Úvideor   r   r   r   s          úg/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/utils/video.pyÚfrom_video_pathzVideoInfo.from_video_path8   sÏ   € õ$ Ô  Ñ,Ô,ˆØ�~Š~ÑÔð 	EÝÐC°zÐCÐCÑDÔDÐDå�E—I’I�cÔ6Ñ7Ô7Ñ8Ô8ˆÝ�U—Y’Y�sÔ8Ñ9Ô9Ñ:Ô:ˆÝ�E—I’I�cÔ.Ñ/Ô/Ñ0Ô0ˆÝ˜5Ÿ9š9¥SÔ%=Ñ>Ô>Ñ?Ô?ˆØ�Š‰ŒˆÝ˜ ¨¨\Ñ:Ô:Ð:ó    útuple[int, int]c                ó   — | j         | j        fS )N)r   r   ©Úselfs    r+   Úresolution_whzVideoInfo.resolution_whU   s   € àŒz˜4œ;Ð&Ð&r-   )r   r   r   r   )r   r.   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Úclassmethodr,   Úpropertyr2   © r-   r+   r   r      sŒ   € € € € € € ðð ð4 €J€J�JØ€K€K�KØ€J€J�JØ#€LÐ#Ð#Ð#Ñ#àð;ð ;ð ;ñ „[ð;ð8 ð'ð 'ð 'ñ „Xð'ð 'ð 'r-   r   c                  ó6   — e Zd ZdZ	 ddd
„Zdd„Zdd„Zdd„ZdS )Ú	VideoSinkaØ  
    Context manager that saves video frames to a file using OpenCV.

    Attributes:
        target_path: The path to the output file where the video will be saved.
        video_info: Information about the video resolution, fps,
            and total frame count.
        codec: FOURCC code for video format

    Example:
        ```python
        import supervision as sv

        video_info = sv.VideoInfo.from_video_path("<SOURCE_VIDEO_PATH>")
        frames_generator = sv.get_video_frames_generator("<SOURCE_VIDEO_PATH>")

        with sv.VideoSink(target_path="<TARGET_VIDEO_PATH>", video_info=video_info) as sink:
            for frame in frames_generator:
                sink.write_frame(frame=frame)
        ```
    Úmp4vÚtarget_pathr   Ú
video_infor   Úcodecr   ÚNonec                óL   — || _         || _        || _        d| _        d | _        d S )Nr   )r>   r?   Ú_VideoSink__codecÚ_VideoSink__fourccÚ_VideoSink__writer)r1   r>   r?   r@   s       r+   Ú__init__zVideoSink.__init__q   s,   € ð 'ˆÔØ$ˆŒØˆŒØˆŒØ04ˆŒˆˆr-   c                óœ  — t          t          t          t          t          t          gt          f         t	          t
          d¦  «        ¦  «        }	 t           || j        Ž ¦  «        | _        nV# t          $ rI}t           
                    dt          |¦  «        ¦  «         t           |dŽ ¦  «        | _        Y d}~nd}~ww xY wt          j        | j        | j        | j        j        | j        j        ¦  «        | _        | j                             ¦   «         s7| j                             ¦   «          d| _        t'          d| j        › �¦  «        ‚| S )zBOpen the underlying video writer for context-managed frame output.ÚVideoWriter_fourccz%s. Defaulting to mp4v...r=   Nz Could not open video writer for )r   r   r   r   Úgetattrr   rC   rD   Ú	TypeErrorÚloggerÚwarningÚVideoWriterr>   r?   r   r2   rE   r!   r(   ÚRuntimeError)r1   Ú	fourcc_fnÚes      r+   Ú	__enter__zVideoSink.__enter__z   s0  € åÝ•c�3¥¥SÐ)­3Ð.Ô/µ½Ð>RÑ1SÔ1Sñ
ô 
ˆ	ð	4Ý 	 	¨4¬<Ð 8Ñ9Ô9ˆDŒMˆMøÝð 	4ð 	4ð 	4Ý�NŠNÐ6½¸A¹¼Ñ?Ô?Ð?Ý 	 	¨6Ð 2Ñ3Ô3ˆDŒMˆMˆMˆMˆMˆMøøøøð	4øøøõ œØÔØŒMØŒOÔØŒOÔ)ñ	
ô 
ˆŒð Œ}×%Ò%Ñ'Ô'ð 	VØŒM×!Ò!Ñ#Ô#Ð#Ø ˆDŒMÝÐTÀ$ÔBRÐTÐTÑUÔUÐUØˆs   ÁA, Á,
B?Á6?B:Â:B?Úframeúnpt.NDArray[np.uint8]c                óf   — | j         €t          d¦  «        ‚| j                              |¦  «         dS )zË
        Writes a single video frame to the target video file.

        Args:
            frame: The video frame to be written to the file. The frame
                must be in BGR color format.
        Nz/write_frame requires an open VideoSink context.)rE   rN   Úwrite)r1   rR   s     r+   Úwrite_framezVideoSink.write_frame‘   s7   € ð Œ=Ð ÝÐPÑQÔQÐQØŒ×Ò˜EÑ"Ô"Ð"Ð"Ð"r-   Úexc_typeútype[BaseException] | NoneÚ	exc_valueúBaseException | NoneÚexc_tracebackúTracebackType | Nonec                óX   — | j         �"| j                              ¦   «          d| _         dS dS )z=Release the underlying video writer when leaving the context.N)rE   r(   )r1   rW   rY   r[   s       r+   Ú__exit__zVideoSink.__exit__ž   s3   € ð Œ=Ð$ØŒM×!Ò!Ñ#Ô#Ð#Ø ˆDŒMˆMˆMð %Ð$r-   N)r=   )r>   r   r?   r   r@   r   r   rA   )r   r<   )rR   rS   r   rA   )rW   rX   rY   rZ   r[   r\   r   rA   )r3   r4   r5   r6   rF   rQ   rV   r^   r:   r-   r+   r<   r<   Z   sz   € € € € € ðð ð. EKð5ð 5ð 5ð 5ð 5ðð ð ð ð.#ð #ð #ð #ð	!ð 	!ð 	!ð 	!ð 	!ð 	!r-   r<   FÚsource_pathr   Ústartr   Úendr   Úiterative_seekÚboolr   ú!tuple[cv2.VideoCapture, int, int]c                óô  — t          j        | ¦  «        }|                     ¦   «         st          d| › �¦  «        ‚t	          |                     t           j        ¦  «        ¦  «        }|�||k    rt          d¦  «        ‚t          |d¦  «        }|�t          ||¦  «        n|}|r+t          |¦  «        D ]}| 
                    ¦   «         }|s nŒn&|dk    r |                     t           j        |¦  «         |||fS )zKOpen a video, position it at `start`, and return it with the clamped range.r   NzRequested frames are outboundr   )r   r    r!   r"   r   r#   r'   ÚmaxÚminÚrangeÚgrabÚsetÚCAP_PROP_POS_FRAMES)r_   r`   ra   rb   r*   r   Ú_Úsuccesss           r+   Ú_validate_and_setup_videorn   ª   s	  € õ Ô˜[Ñ)Ô)€EØ�>Š>ÑÔð BÝÐ@°;Ð@Ð@ÑAÔAÐAÝ�u—y’y¥Ô!9Ñ:Ô:Ñ;Ô;€LØ
€˜3 Ò-Ð-ÝÐ7Ñ8Ô8Ð8Ý��q‰MŒM€EØ$' O�#ˆc�<Ñ
 Ô
 Ð
 ¸€Càð 2õ �u‘”ð 	ð 	ˆAØ—j’j‘l”lˆGØð Ø�ðøà	�ŠˆØ�	Š	•#Ô)¨5Ñ1Ô1Ð1à�%˜ÐÐr-   é   ÚstrideÚprefetchú,Generator[npt.NDArray[np.uint8], None, None]c              #  ó  K  — |dk     rt          d|›�¦  «        ‚|dk    rt          | |||||¬¦  «        E d{V —† dS t          | |||¦  «        \  }}}|}	 	 |                     ¦   «         \  }}	|r||k    rna|	�,t	          t
          j        t          j                 |	¦  «        V — t          |dz
  ¦  «        D ]}
| 
                    ¦   «         }|s nŒ||z  }Œ�	 |                     ¦   «          dS # |                     ¦   «          w xY w)u   
    Get a generator that yields the frames of the video.

    Args:
        source_path: The path of the video file.
        stride: Indicates the interval at which frames are returned,
            skipping stride - 1 frames between each.
        start: Indicates the starting position from which
            video should generate frames
        end: Indicates the ending position at which video
            should stop generating frames. If None, video will be read to the end.
        iterative_seek: If True, the generator will seek to the
            `start` frame by grabbing each frame, which is much slower. This is a
            workaround for videos that don't open at all when you set the `start` value.
        prefetch: If > 0, decode frames in a background thread and buffer up to
            this many frames in a bounded queue. Useful when the consumer (e.g.
            CPU inference) is the bottleneck and can overlap with decode I/O.
            This works best when the consumer releases the GIL during frame
            processing (common for numpy/PyTorch/ONNX C-extension calls). Pure
            Python per-frame consumers that hold the GIL (for example, heavy
            Python loops, PIL usage, or pandas `apply`) usually see little
            speedup.
            Default 0 keeps the original synchronous behaviour unchanged. Note:
            each buffered frame occupies width x height x 3 bytes of uncompressed
            memory; use `sv.VideoInfo.from_video_path()` to size appropriately.

    Returns:
        A generator that yields the frames of the video.

    Raises:
        ValueError: If `prefetch` is negative.
        RuntimeError: If `prefetch` is greater than 0 and the background reader
            thread encounters a decode/open error, raised as
            `RuntimeError(f"Reader thread raised: {item!r}") from item`. Errors are
            drained after buffered frames, so when `prefetch` > 0 the consumer may
            yield up to `prefetch` additional good frames before the exception is
            raised.

    Note:
        For live camera streams, use `cv2.VideoCapture` with an integer device
        index directly. `get_video_frames_generator` is designed for file-based
        sources; `cv2.VideoCapture` must be released by the caller when done.
        This requires OpenCV to be installed â€” the PyAV-based fallback used
        when OpenCV is unavailable only supports file paths, not webcam device
        indexes; passing an integer source to it always leaves the capture
        closed (`isOpened()` returns `False`):

        ```python
        from supervision import _cv2 as cv2

        cap = cv2.VideoCapture(0)  # 0 = default webcam
        try:
            while cap.isOpened():
                success, frame = cap.read()
                if not success:
                    break
                ...  # process frame
        finally:
            cap.release()
        ```

    Examples:
        ```python
        import supervision as sv

        for frame in sv.get_video_frames_generator(source_path="<SOURCE_VIDEO_PATH>"):
            ...

        # Prefetch frames in a background thread to overlap I/O with CPU inference:
        for frame in sv.get_video_frames_generator(
            source_path="<SOURCE_VIDEO_PATH>", prefetch=8
        ):
            ...
        ```
    r   zprefetch must be >= 0, got ©r_   rp   r`   ra   rb   rq   NTro   )Ú
ValueErrorÚ_prefetched_frames_generatorrn   Úreadr   ÚnptÚNDArrayÚnpÚuint8rh   ri   r(   )r_   rp   r`   ra   rb   rq   r*   Úframe_positionrm   rR   rl   s              r+   Úget_video_frames_generatorr}   Ä   sn  è è € ðf �!‚|€|ÝÐC°xÐCÐCÑDÔDÐDØ�!‚|€|Ý/Ø#ØØØØ)Øð
ñ 
ô 
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	ˆå1Ø�U˜C ñô Ñ€Eˆ5�#ð €Nðð
	%Ø"ŸZšZ™\œ\‰NˆG�UØð ˜n°Ò3Ð3ØØÐ Ý�3œ;¥r¤xÔ0°%Ñ8Ô8Ð8Ð8Ð8Ý˜6 A™:Ñ&Ô&ð ð �ØŸ*š*™,œ,�Øð Ø�Eðà˜fÑ$ˆNð
	%ð ð 	�Š‰Œˆˆˆøˆ�Š‰Œˆˆøøøs   ÁBC0 Ã0Dc              #  ó\  ‡ ‡‡‡‡‡	‡
K  — t          |¬¦  «        Š	t          j        ¦   «         Š
dˆˆ	ˆˆ ˆˆ
ˆfd„}t          j        |d¬¦  «        }|                     ¦   «          	 	 	 ‰	                     d¬¦  «        }n&# t          $ r |                     ¦   «         sY n6Y Œ;w xY wt          |t          ¦  «        rt          d	|›�¦  «        |‚|€n|V — Œn‰
                     ¦   «          |                     d¬¦  «         d
S # ‰
                     ¦   «          |                     d¬¦  «         w xY w)z‡Read frames into a bounded queue on a daemon thread.

    Sentinel protocol: None = normal EOF, Exception instance = reader error.
    ©Úmaxsizer   rA   c                 óÔ  •— d } 	 t          ‰‰	‰‰‰d¬¦  «        D �]}‰                     ¦   «         rW ‰                     ¦   «         s@	 ‰                     | d¬¦  «         d S # t          $ r Y nw xY w‰                     ¦   «         ¯>d S d S 	 	 ‰                     |d¬¦  «         n}# t          $ ro ‰                     ¦   «         rXY  ‰                     ¦   «         s@	 ‰                     | d¬¦  «         d S # t          $ r Y nw xY w‰                     ¦   «         ¯>d S d S Y nw xY wŒ–�Œn# t          $ r}|} Y d }~nd }~ww xY w‰                     ¦   «         s@	 ‰                     | d¬¦  «         d S # t          $ r Y nw xY w‰                     ¦   «         ¯>d S d S # ‰                     ¦   «         s@	 ‰                     | d¬¦  «          Y d S # t          $ r Y nw xY w‰                     ¦   «         ¯@w xY w)Nr   rt   gš™™™™™¹?©Útimeout)r}   Úis_setÚputr	   r"   )
ÚsentinelrR   Úexcra   Úframe_queuerb   r_   r`   Ú
stop_eventrp   s
      €€€€€€€r+   Úreaderz,_prefetched_frames_generator.<locals>.readerI  sø  ø€ Ø)-ˆð	Ý3Ø'ØØØØ-Øðñ ô ð #ñ #�ð ×$Ò$Ñ&Ô&ð Øð !×'Ò'Ñ)Ô)ð ðØ—O’O H°c�OÑ:Ô:Ð:Ø�FøÝð ð ð Ø�Dðøøøð	 !×'Ò'Ñ)Ô)ð ð ð ð ð ð#ð#Ø#Ÿš¨°s˜Ñ;Ô;Ð;ØøÝð #ð #ð #Ø%×,Ò,Ñ.Ô.ð #Ø"˜Fð !×'Ò'Ñ)Ô)ð ðØ—O’O H°c�OÑ:Ô:Ð:Ø�FøÝð ð ð Ø�Dðøøøð	 !×'Ò'Ñ)Ô)ð ð ð ð ð ð#ð #ð#øøøð	#ñ ð#øõ" ð 	ð 	ð 	ØˆHˆHˆHˆHˆHˆHøøøøð	øøøð !×'Ò'Ñ)Ô)ð ðØ—O’O H°c�OÑ:Ô:Ð:Ø�FøÝð ð ð Ø�Dðøøøð	 !×'Ò'Ñ)Ô)ð ð ð ð ð ø�j×'Ò'Ñ)Ô)ð ðØ—O’O H°c�OÑ:Ô:Ð:Ø�F�F�FøÝð ð ð Ø�Dðøøøð	 !×'Ò'Ñ)Ô)ð ð ð ð ð s¹   …-D" ÁA  Á 
A-Á,A-ÂD" Â
B"Â!D" Â"DÃ D" ÃC0 Ã0
C=Ã<C=ÄD" ÄDÄD" Ä!F Ä"
D8Ä,D3Ä.F Ä3D8Ä8F ÅE) Å)
E6Å5E6ÆG'Æ'GÆ>G'Ç
GÇG'ÇGÇG'T©ÚtargetÚdaemong      à?r‚   úReader thread raised: Ng       @©r   rA   )r
   Ú	threadingÚEventÚThreadr`   r#   r   Úis_aliveÚ
isinstanceÚBaseExceptionrN   rj   Újoin)r_   rp   r`   ra   rb   rq   rŠ   ÚthreadÚitemrˆ   r‰   s   `````    @@r+   rv   rv   8  s’  øøøøøøøè è € õ HMØðHñ Hô H€Kõ ”Ñ"Ô"€Jðð ð ð ð ð ð ð ð ð ð ð õ< Ô V°DÐ9Ñ9Ô9€FØ
‡L‚L�N„N€Nð!ð	ðØ"—’¨s�Ñ3Ô3��øÝð ð ð Ø—’Ñ(Ô(ð Ø�EØ�ðøøøõ ˜$¥Ñ.Ô.ð PÝ"Ð#D¸DÐ#DÐ#DÑEÔEÈ4ÐOØˆ|ØØˆJˆJˆJð	ð 	�ŠÑÔÐØ�Š˜CˆÑ Ô Ð Ð Ð øð 	�ŠÑÔÐØ�Š˜CˆÑ Ô Ð Ð øøøs0   Á$C? Á&A= Á<C? Á=B ÂC? ÂB Â 3C? Ã?,D+é    zProcessing video)Ú
max_framesrq   Úwriter_bufferÚshow_progressÚprogress_messageÚpreserve_audior>   Úcallbackú=Callable[[npt.NDArray[np.uint8], int], npt.NDArray[np.uint8]]rš   r›   rœ   r�   rž   rA   c               ó¾  ‡ ‡‡‡‡— t                                ‰ ¬¦  «        }	|	j        pd}
|
}|�t          |
|¦  «        }|Š|�|
dk    r|Št	          |¬¦  «        Št	          |¬¦  «        ŠdŠdˆˆˆˆ fd„}dˆfd
„}t          j        |d¬¦  «        }t          ||	¬¦  «        5 }t          j        ||fd¬¦  «        }|                     ¦   «          |                     ¦   «          t          || |¬¦  «        }d}d}	 	 ‰ 
                    ¦   «         }|€d}nW|\  }}	  |||¦  «        }‰                     |¦  «         |                     d¦  «         n# t          $ r}|}Y d}~n	d}~ww xY wŒp	 ‰                     dd¬¦  «         n# t          $ r Y nw xY w|sC	 	 ‰ 
                    d¬¦  «        }|€n(n&# t          $ r |                     ¦   «         sY nY Œ>w xY wŒB|                     d¬¦  «         |                     d¬¦  «         |                     ¦   «          |�|‚‰�t'          d‰›�¦  «        ‰‚nÌ# 	 ‰                     dd¬¦  «         n# t          $ r Y nw xY w|sC	 	 ‰ 
                    d¬¦  «        }|€n(n&# t          $ r |                     ¦   «         sY nY Œ>w xY wŒB|                     d¬¦  «         |                     d¬¦  «         |                     ¦   «          |�|‚‰�t'          d‰›�¦  «        ‰‚w xY w	 ddd¦  «         n# 1 swxY w Y   |rC|                     ¦   «         rt(                               d¦  «         dS t-          ‰ |¬¦  «         dS dS )uÀ  
    Process video frames asynchronously using a threaded pipeline.

    This function orchestrates a three-stage pipeline to optimize video processing
    throughput:

    1. Reader thread: Continuously reads frames from the source video file and
       enqueues them into a bounded queue (`frame_read_queue`). The queue size is
       limited by the `prefetch` parameter to control memory usage.
    2. Main thread (Processor): Dequeues frames from `frame_read_queue`, applies the
       user-defined `callback` function to process each frame, then enqueues the
       processed frames into another bounded queue (`frame_write_queue`) for writing.
       The processing happens in the main thread, simplifying use of stateful objects
       without synchronization.
    3. Writer thread: Dequeues processed frames from `frame_write_queue` and writes
       them sequentially to the output video file.

    Args:
        source_path: Path to the input video file.
        target_path: Path where the processed video will be saved.
        callback: Function called for
            each frame, accepting the frame as a numpy array and its zero-based index,
            returning the processed frame.
        max_frames: Optional maximum number of frames to process.
            If None, the entire video is processed (default). A value larger
            than the number of frames in the video processes the whole video.
        prefetch: Maximum number of frames buffered by the reader thread.
            Controls memory use; default is 32.
        writer_buffer: Maximum number of frames buffered before writing.
            Controls output buffer size; default is 32.
        show_progress: Whether to display a tqdm progress bar during processing.
            Default is False.
        progress_message: Description shown in the progress bar.
        preserve_audio: If True, copy the audio stream from `source_path` into
            `target_path` after frame processing. Remuxing is done with PyAV;
            no external `ffmpeg` executable is required. If the mux step
            fails, a warning is logged and the output video is saved without
            audio â€” no exception is raised. Audio is truncated to match the
            processed video duration. Default is False.

    Returns:
        None

    Raises:
        RuntimeError: If the reader thread fails to open or decode the source
            video, raised as `RuntimeError(f"Reader thread raised: {exc!r}")`
            from the original exception. Exceptions raised by `callback` are
            re-raised unchanged.

    Example:
        ```python
        import supervision as sv
        from rfdetr import RFDETRMedium

        model = RFDETRMedium()

        def callback(frame, frame_index):
            return model.predict(frame)

        sv.process_video(
            source_path="source.mp4",
            target_path="target.mp4",
            callback=callback,
            preserve_audio=True,
        )
        ```
    )r   r   Nr   r   rA   c                 ó  •— 	 t          ‰‰¬¦  «        } t          | ¦  «        D ]\  }}‰                     ||f¦  «         Œn# t          $ r}|ŠY d}~nd}~ww xY w‰                     d¦  «         dS # ‰                     d¦  «         w xY w)zAFeed frames into the read queue, always ending with the sentinel.)r_   ra   N)r}   Ú	enumerater…   r"   )Úframe_generatorÚframe_indexrR   r‡   Úframe_read_queueÚ
frames_endÚreader_exceptionr_   s       €€€€r+   Úreader_threadz$process_video.<locals>.reader_threadà  sÎ   ø€ ð	'Ý8Ø'Øðñ ô ˆOõ '0°Ñ&@Ô&@ð ;ð ;Ñ"�˜UØ ×$Ò$ k°5Ð%9Ñ:Ô:Ð:Ð:ð;øåð 	#ð 	#ð 	#ð  #ÐÐÐÐÐÐøøøøð		#øøøð × Ò  Ñ&Ô&Ð&Ð&Ð&øÐ× Ò  Ñ&Ô&Ð&Ð&øøøs/   ƒ=A Á A1 Á
AÁAÁA1 ÁAÁA1 Á1BÚ
video_sinkr<   c                ód   •— 	 ‰                      ¦   «         }|€d S |                      |¬¦  «         Œ/)NT)rR   )r#   rV   )rª   rR   Úframe_write_queues     €r+   Úwriter_threadz$process_video.<locals>.writer_threadò  s?   ø€ ð	0Ø%×)Ò)Ñ+Ô+ˆEØˆ}Ø�Ø×"Ò"¨Ð"Ñ/Ô/Ð/ð		0r-   Tr‹   )r>   r?   )rŒ   Úargsr�   )ÚtotalÚdisableÚdescFro   r‚   é
   rŽ   zdWriter thread did not finish in time; skipping audio mux to avoid reading an incomplete output file.)r_   r   r�   )rª   r<   r   rA   )r   r,   r   rg   r
   r�   r’   r<   r`   r   r#   r…   Úupdater"   r	   r   r“   r–   ÚcloserN   rK   rL   r   )r_   r>   rŸ   rš   rq   r›   rœ   r�   rž   r?   Úvideo_total_framesr   r©   r­   Úreader_workerrª   Úwriter_workerÚprogress_barÚexception_in_workerÚread_finishedÚ	read_itemr¥   rR   Úprocessed_framer‡   r¦   r¬   r§   r¨   s   `                        @@@@r+   Úprocess_videor½   {  s†  øøøøø€ õ^ ×*Ò*°kÐ*ÑBÔB€JØ#Ô0Ð5°AÐØ%€LØÐÝÐ-¨zÑ:Ô:ˆð
 (€JØÐÐ"4°qÒ"8Ð"8Ø!ˆ
åHMØðIñ Iô IÐõ >CØð>ñ >ô >Ðð *.Ðð'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð$0ð 0ð 0ð 0ð 0ð 0õ Ô$¨MÀ$ÐGÑGÔG€MÝ	˜{°zÐ	BÑ	BÔ	Bð @(ÀjÝ!Ô(Ø Ø�Øð
ñ 
ô 
ˆð 	×ÒÑÔÐØ×ÒÑÔÐåØØ%Ð%Ø!ð
ñ 
ô 
ˆð 15ÐØˆð-	(ðØ,×0Ò0Ñ2Ô2�	ØÐ$Ø$(�MØà%.Ñ"�˜UðØ&. h¨u°kÑ&BÔ&B�OØ%×)Ò)¨/Ñ:Ô:Ð:Ø ×'Ò'¨Ñ*Ô*Ð*Ð*øÝ ð ð ð Ø*-Ð'Ø�E�E�E�EøøøøðøøøððØ!×%Ò% d°AÐ%Ñ6Ô6Ð6Ð6øÝð ð ð ð �ðøøøð !ð !ð!ð!Ø$4×$8Ò$8ÀÐ$8Ñ$CÔ$C˜	Ø$Ð,Ø!ð -øõ
 !ð !ð !ð !Ø,×5Ò5Ñ7Ô7ð "Ø!˜Eà ˜ð	!øøøð!ð ×Ò rÐÑ*Ô*Ð*Ø×Ò rÐÑ*Ô*Ð*Ø×ÒÑ Ô Ð Ø"Ð.Ø)Ð)ØÐ+Ý"ØAÐ-=ÐAÐAñô à'ð(ð ,øð5Ø!×%Ò% d°AÐ%Ñ6Ô6Ð6Ð6øÝð ð ð ð �ðøøøð !ð !ð!ð!Ø$4×$8Ò$8ÀÐ$8Ñ$CÔ$C˜	Ø$Ð,Ø!ð -øõ
 !ð !ð !ð !Ø,×5Ò5Ñ7Ô7ð "Ø!˜Eà ˜ð	!øøøð!ð ×Ò rÐÑ*Ô*Ð*Ø×Ò rÐÑ*Ô*Ð*Ø×ÒÑ Ô Ð Ø"Ð.Ø)Ð)ØÐ+Ý"ØAÐ-=ÐAÐAñô à'ð(ð (ð (ð (ð (ð ,ð{@(ð @(ð @(ñ @(ô @(ð @(ð @(ð @(ð @(ð @(ð @(øøøð @(ð @(ð @(ð @(ðD ð HØ×!Ò!Ñ#Ô#ð 	HÝ�NŠNð>ñô ð ð ð õ
  ;¸;ÐGÑGÔGÐGÐGÐGðHð Hsá   Â"ALÃ;H4Ä6EÅH4Å
E(ÅE#ÅH4Å#E(Å(H4Å-FÆLÆ
FÆLÆFÆLÆF3Æ1LÆ3GÇLÇGÇALÈ4K=È6IÉK=É
I	ÉK=ÉI	ÉK=É"I<É:K=É<J	ÊK=ÊJ	ÊAK=Ë=LÌLÌLc                  óD   — e Zd ZdZddd„Zedd	„¦   «         Zdd
„Zdd„ZdS )Ú
FPSMonitorzN
    A class for monitoring frames per second (FPS) to benchmark latency.
    é   Úsample_sizer   r   rA   c                ó0   — t          |¬¦  «        | _        dS )a  
        Args:
            sample_size: The maximum number of observations for latency
                benchmarking.

        Examples:
            ```python
            import supervision as sv

            frames_generator = sv.get_video_frames_generator(
                source_path="<SOURCE_FILE_PATH>")
            fps_monitor = sv.FPSMonitor()

            for frame in frames_generator:
                # your processing code here
                fps_monitor.tick()
                fps = fps_monitor.fps
            ```
        )ÚmaxlenN)r   Úall_timestamps)r1   rÁ   s     r+   rF   zFPSMonitor.__init__K  s   € õ( -2¸Ð,EÑ,EÔ,EˆÔÐÐr-   r   c                ó´   — t          | j        ¦  «        dk     rdS | j        d         | j        d         z
  }t          | j        ¦  «        dz
  }|dk    r||z  ndS )zæ
        Computes and returns the average FPS based on the stored time stamps.

        Returns:
            The average FPS across the recorded intervals. Returns 0.0 if fewer
            than two time stamps are stored.
        é   g        éÿÿÿÿr   ro   )ÚlenrÄ   )r1   Ú
taken_timeÚframe_intervalss      r+   r   zFPSMonitor.fpsa  sf   € õ ˆtÔ"Ñ#Ô# aÒ'Ð'Ø�3ØÔ(¨Ô,¨tÔ/BÀ1Ô/EÑEˆ
Ý˜dÔ1Ñ2Ô2°QÑ6ˆØ/9¸Qª¨ˆ Ñ+Ð+ÀCÐGr-   c                ó\   — | j                              t          j        ¦   «         ¦  «         dS )zI
        Adds a new time stamp to the deque for FPS calculation.
        N)rÄ   ÚappendÚtimeÚ	monotonicr0   s    r+   ÚtickzFPSMonitor.tickp  s)   € ð 	Ô×"Ò"¥4¤>Ñ#3Ô#3Ñ4Ô4Ð4Ð4Ð4r-   c                ó8   — | j                              ¦   «          dS )z<
        Clears all the time stamps from the deque.
        N)rÄ   Úclearr0   s    r+   ÚresetzFPSMonitor.resetv  s   € ð 	Ô×!Ò!Ñ#Ô#Ð#Ð#Ð#r-   N)rÀ   )rÁ   r   r   rA   )r   r   r�   )	r3   r4   r5   r6   rF   r9   r   rÏ   rÒ   r:   r-   r+   r¿   r¿   F  sˆ   € € € € € ðð ðFð Fð Fð Fð Fð, ðHð Hð Hñ „XðHð5ð 5ð 5ð 5ð$ð $ð $ð $ð $ð $r-   r¿   )F)
r_   r   r`   r   ra   r   rb   rc   r   rd   )ro   r   NFr   )r_   r   rp   r   r`   r   ra   r   rb   rc   rq   r   r   rr   )r_   r   r>   r   rŸ   r    rš   r   rq   r   r›   r   rœ   rc   r�   r   rž   rc   r   rA   ))Ú
__future__r   r�   rÍ   Úcollectionsr   Úcollections.abcr   r   Údataclassesr   Úqueuer   r	   r
   Útypesr   Útypingr   Únumpyrz   Únumpy.typingrx   Ú	tqdm.autor   Úsupervisionr   r   Úsupervision._cv2._videor   Úsupervision.utils.loggerr   r3   rK   r   r<   rn   r}   rv   r½   r¿   r:   r-   r+   ú<module>rà      s_  ðØ "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø !Ð !Ð !Ð !Ð !Ð !Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à #Ð #Ð #Ð #Ð #Ð #Ø .Ð .Ð .Ð .Ð .Ð .Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0à	ˆ�XÑ	Ô	€ð ð?'ð ?'ð ?'ð ?'ð ?'ñ ?'ô ?'ñ „ð?'ðDM!ð M!ð M!ð M!ð M!ñ M!ô M!ð M!ðb KPðð ð ð ð ð8 ØØØ Øðqð qð qð qð qðh@!ð @!ð @!ð @!ðP "ØØØØ.Ø ðHHð HHð HHð HHð HHð HHðV4$ð 4$ð 4$ð 4$ð 4$ñ 4$ô 4$ð 4$ð 4$ð 4$r-   