U
    a+d                     @   s   d Z ddlZddlmZmZ ddlmZ ddlm	Z	 zddl
Z
dZW n   d	ZY nX ed
dgedddZdddZdddZdS )a  
This module contains different functions for tracking objects in videos,
manually or automatically. The tracking functions return results under
the form:  ``( txy, (fx,fy) )`` where txy is of the form [(ti, xi, yi)...]
and (fx(t),fy(t)) give the position of the track for all times t (if the
time t is out of the time bounds of the tracking time interval
fx and fy return the position of the object at the start or at the end
of the tracking time interval).
    N)convert_to_secondsuse_clip_fps_by_default   )	imdisplay   )
TrajectoryTFt1t2c                    s  ddl j jd| }|dkr<|dkr<d j }}n|dkrP||d  }|}g } fdd}	||k r|	|}
|
dkr|  ||8 }qj|||
f ||7 }qjt| \}}g }tD ]4fdd	|D }t| \}}|t	||| q|dk	rt	
|| |S )
a  
    Allows manual tracking of an object(s) in the video clip between
    times `t1` and `t2`. This displays the clip frame by frame
    and you must click on the object(s) in each frame. If ``t2=None``
    only the frame at ``t1`` is taken into account.
    
    Returns a list [(t1,x1,y1),(t2,x2,y2) etc... ] if there is one
    object per frame, else returns a list whose elements are of the 
    form (ti, [(xi1,yi1), (xi2,yi2), ...] )
    
    Parameters
    -------------

    t1,t2:
      times during which to track (defaults are start and
      end of the clip). t1 and t2 can be expressed in seconds
      like 15.35, in (min, sec), in (hour, min, sec), or as a
      string: '01:03:05.35'.
    fps:
      Number of frames per second to freeze on. If None, the clip's
      fps attribute is used instead.
    nobjects:
      Number of objects to click on each frame.
    savefile:
      If provided, the result is saved to a file, which makes
      it easier to edit and re-use later.

    Examples
    ---------
    
    >>> from moviepy.editor import VideoFileClip
    >>> from moviepy.video.tools.tracking import manual_tracking
    >>> clip = VideoFileClip("myvideo.mp4")
    >>> # manually indicate 3 trajectories, save them to a file
    >>> trajectories = manual_tracking(clip, t1=5, t2=7, fps=5,
                                       nobjects=3, savefile="track.txt")
    >>> # ...
    >>> # LATER, IN ANOTHER SCRIPT, RECOVER THESE TRAJECTORIES
    >>> from moviepy.video.tools.tracking import Trajectory
    >>> traj1, traj2, traj3 = Trajectory.load_list('track.txt')
    >>> # If ever you only have one object being tracked, recover it with
    >>> traj, =  Trajectory.load_list('track.txt')
    
    r   N      ?r   c                    s   t  |  }g }|rj D ]f}|jjkr\|jjkrH dS |jjkrt	 q&|jj
kr&j \}}|||f |d8 }q&q|S )Nreturnr   )r   	get_frameeventgettypeZKEYDOWNkeyZK_BACKSLASHZK_ESCAPEKeyboardInterruptZMOUSEBUTTONDOWNZmouseZget_posappend)tZobjects_to_clickclicksr   xy)clipnobjectspgscreen @/tmp/pip-unpacked-wheel-0yp4gafk/moviepy/video/tools/tracking.pygatherClicks[   s    z%manual_tracking.<locals>.gatherClicksr   c                    s   g | ]}|  qS r   r   ).0e)ir   r   
<listcomp>   s     z#manual_tracking.<locals>.<listcomp>)ZpygameZdisplayset_modesizedurationpopr   zipranger   	save_list)r   r   r	   fpsr   Zsavefilestepr   Ztxy_listr   r   ttZxylistresultxysxxyyr   )r   r    r   r   r   r   manual_tracking   s4    1


r0   c                 C   s   |rN|rN|j dd \}}|\}}| || || | || || | f } t|| tj}t| |j \}	}
|r|r|| |
 || |	 fS |
|	fS )zq
    find image pattern ``pat`` in ``pic[x +/- r, y +/- r]``.
    if xy is none, consider the whole picture.
    Nr   )shapecv2ZmatchTemplateZTM_CCOEFF_NORMEDnpZunravel_indexZargmax)Zpicpatxyrhwr   r   matchesZyfZxfr   r   r   
findAround   s    ,r:      c           
   	   C   s   t std|s$t| |d |}|dkr@td| jd| }|g}|dd D ]$}|t| |||d |d qRt| \}}	t	|||	S )a  
    Tracks a given pattern (small image array) in a video clip.
    Returns [(x1,y1),(x2,y2)...] where xi,yi are
    the coordinates of the pattern in the clip on frame i.
    To select the frames you can either specify a list of times with ``tt``
    or select a frame rate with ``fps``.
    This algorithm assumes that the pattern's aspect does not vary much
    and that the distance between two occurences of the pattern in
    two consecutive frames is smaller than ``radius`` (if you set ``radius``
    to -1 the pattern will be searched in the whole screen at each frame).
    You can also provide the original position of the pattern with xy0.
    zTSorry, autotrack requires OpenCV for the moment. Install OpenCV (aka cv2) to use it.r   Nr
   r   )r5   r6   )
autotracking_possibleIOErrorr:   r   r3   Zaranger$   r   r&   r   )
r   patternr+   r)   ZradiusZxy0r-   r   r.   r/   r   r   r   	autoTrack   s     
r@   )NNNr   N)NN)NNr;   N)__doc__Znumpyr3   Zmoviepy.decoratorsr   r   Z
io.previewr   Zinterpolatorsr   r2   r=   r0   r:   r@   r   r   r   r   <module>   s    


  i
