Skip to content

Video editing with Python

Document tracking functions parameters (#1608)

Álvaro Mondéjar40db874d6434
diff --git a/moviepy/video/tools/tracking.py b/moviepy/video/tools/tracking.py
index 93a8f4e1..307cd7b1 100644
--- a/moviepy/video/tools/tracking.py
+++ b/moviepy/video/tools/tracking.py
@@ -24,54 +24,67 @@ except Exception:
autotracking_possible = False
-# WE START WITH A TOOL FUNCTION
-
-# MANUAL TRACKING
-
-
@convert_parameter_to_seconds(["t1", "t2"])
@use_clip_fps_by_default
def manual_tracking(clip, t1=None, t2=None, fps=None, n_objects=1, savefile=None):
- """
+ """Manual tracking of objects in videoclips using the mouse.
+
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
+ Returns a list ``[(t1, x1, y1), (t2, x2, y2)...]`` if there is one
object per frame, else returns a list whose elements are of the
- form (ti, [(xi1,yi1), (xi2,yi2), ...] )
+ form ``(ti, [(xi1, yi1), (xi2, yi2)...])``.
+
Parameters
----------
- t1,t2:
- times during which to track (defaults are start and
- end of the clip). start_time 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:
+ clip : video.VideoClip.VideoClip
+ MoviePy video clip to track.
+
+ t1 : float or str or tuple, optional
+ Start time to to track (defaults is start of the clip). Can be expressed
+ in seconds like ``15.35``, in ``(min, sec)``, in ``(hour, min, sec)``,
+ or as a string: ``"01:03:05.35"``.
+
+ t2 : float or str or tuple, optional
+ End time to to track (defaults is end of the clip). Can be expressed
+ in seconds like ``15.35``, in ``(min, sec)``, in ``(hour, min, sec)``,
+ or as a string: ``"01:03:05.35"``.
+
+ fps : int, optional
Number of frames per second to freeze on. If None, the clip's
fps attribute is used instead.
- n_objects:
+
+ n_objects : int, optional
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.
+
+ savefile : str, optional
+ If provided, the result is saved to a file, which makes it easier to edit
+ and re-use later.
+
Examples
--------
>>> from moviepy import VideoFileClip
>>> from moviepy.video.tools.tracking import manual_tracking
- >>> clip = VideoFileClip("myvideo.mp4")
+ >>>
+ >>> clip = VideoFileClip("media/chaplin.mp4")
+ >>>
>>> # manually indicate 3 trajectories, save them to a file
>>> trajectories = manual_tracking(clip, start_time=5, t2=7, fps=5,
- nobjects=3, savefile="track.text")
+ ... nobjects=3, savefile="track.text")
+ >>>
>>> # ...
- >>> # LATER, IN ANOTHER SCRIPT, RECOVER THESE TRAJECTORIES
+ >>> # later, in another script, recover these trajectories
>>> from moviepy.video.tools.tracking import Trajectory
+ >>>
>>> traj1, traj2, traj3 = Trajectory.load_list('track.text')
+ >>>
>>> # If ever you only have one object being tracked, recover it with
>>> traj, = Trajectory.load_list('track.text')
"""
@@ -130,13 +143,28 @@ def manual_tracking(clip, t1=None, t2=None, fps=None, n_objects=1, savefile=None
return result
-# AUTOMATED TRACKING OF A PATTERN
+def findAround(pic, pat, xy=None, r=None):
+ """Find an image pattern in a picture optionally defining bounds to search.
+ The image is found is ``pat`` is inside ``pic[x +/- r, y +/- r]``.
-def findAround(pic, pat, xy=None, r=None):
- """Find a image pattern ``pat`` in ``pic[x +/- r, y +/- r]``.
+ Parameters
+ ----------
+
+ pic : numpy.ndarray
+ Image where the pattern will be searched.
+
+ pat : numpy.ndarray
+ Pattern to search inside the image.
- If xy is none, consider the whole picture.
+ xy : tuple or list, optional
+ Position to search for the pattern. Use it in combination with ``radius``
+ parameter to define the bounds of the search. If is ``None``, consider
+ the whole picture.
+
+ r : float, optional
+ Radius used to define the bounds of the search when ``xy`` argument is
+ defined.
"""
if xy and r:
h, w = pat.shape[:2]
@@ -151,15 +179,40 @@ def findAround(pic, pat, xy=None, r=None):
def autoTrack(clip, pattern, tt=None, fps=None, radius=20, xy0=None):
"""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``.
+ 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.
+
+ Parameters
+ ----------
+
+ clip : video.VideoClip.VideoClip
+ MoviePy video clip to track.
+
+ pattern : numpy.ndarray
+ Image to search inside the clip frames.
+
+ tt : numpy.ndarray, optional
+ Time frames used for auto tracking. As default is used the clip time
+ frames according to its fps.
+
+ fps : int, optional
+ Overwrites fps value used computing time frames. As default, clip's fps.
+
+ radius : int, optional
+ Maximum radius to search looking for the pattern. Setted to ``-1``,
+ the pattern will be searched in the whole screen at each frame.
- 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.
+ xy0 : tuple or list, optional
+ Original position of the pattern. If not provided, will be taken from the
+ first tracked frame of the clip.
"""
if not autotracking_possible:
raise IOError(