Video editing with Python
Add tests for 'find_objects' video tool (#1607)
Álvaro Mondéjarf0960278226b
diff --git a/moviepy/video/tools/segmenting.py b/moviepy/video/tools/segmenting.pyindex 0f37fc63..42d060a7 100644--- a/moviepy/video/tools/segmenting.py+++ b/moviepy/video/tools/segmenting.py@@ -8,11 +8,33 @@ def find_objects(clip, size_threshold=500, preview=False):"""Returns a list of ImageClips representing each a separate object onthe screen.- size_threshold : all objects found with size < size_threshold will be- considered false positives and will be removed+ Parameters+ ----------++ clip : video.VideoClip.ImageClip+ MoviePy video clip where the objects will be searched.++ size_threshold : float, optional+ Minimum size of what is considered an object. All objects found with+ ``size < size_threshold`` will be considered false positives and will+ be removed.++ preview : bool, optional+ Previews with matplotlib the different objects found in the image before+ applying the size threshold. Requires matplotlib installed.+++ Examples+ --------++ >>> clip = ImageClip("media/afterimage.png")+ >>> objects = find_objects(clip)+ >>>+ >>> print(len(objects))+ >>> print([obj_.screenpos for obj_ in objects])"""image = clip.get_frame(0)- if not clip.mask:+ if not clip.mask: # pragma: no coverclip = clip.add_mask()mask = clip.mask.get_frame(0)@@ -43,7 +65,7 @@ def find_objects(clip, size_threshold=500, preview=False):letter.screenpos = np.array((sx.start, sy.start))letters.append(letter)- if preview:+ if preview: # pragma: no coverimport matplotlib.pyplot as pltprint(f"Found {num_features} objects")diff --git a/tests/test_videotools.py b/tests/test_videotools.pyindex c84e4481..45b2d8e1 100644--- a/tests/test_videotools.py+++ b/tests/test_videotools.py@@ -25,6 +25,15 @@ from moviepy.video.tools.cuts import ()from moviepy.video.tools.drawing import circle, color_gradient, color_splitfrom moviepy.video.tools.interpolators import Interpolator, Trajectory+++try:+ import scipy+except ImportError:+ scipy = None+else:+ from moviepy.video.tools.segmenting import find_objects+from moviepy.video.VideoClip import BitmapClip, ColorClip, ImageClip, VideoClipfrom tests.test_helper import FONT, TMP_DIR, get_mono_wave, get_stereo_wave@@ -922,6 +931,50 @@ def test_Trajectory_from_to_file():assert f.read() == "\n".join(trajectory_file_content.split("\n")[1:])[email protected](not scipy, reason="Requires scipy installed")+ ("filename", "expected_screenpos"),+ (+ pytest.param(+ "media/python_logo.png",+ [+ [2, 2],+ [78, 16],+ [108, 16],+ [137, 8],+ [156, 0],+ [184, 16],+ [214, 16],+ ],+ id="filename=media/python_logo.png-7",+ ),+ pytest.param(+ "media/afterimage.png",+ [+ [56, 402],+ [177, 396],+ [324, 435],+ [453, 435],+ [535, 396],+ [535, 438],+ [589, 435],+ [776, 435],+ [896, 435],+ [1022, 435],+ ],+ id="filename=media/afterimage.png-10",+ ),+ ),+)+def test_find_objects(filename, expected_screenpos):+ clip = ImageClip(filename)+ objects = find_objects(clip)++ assert len(objects) == len(expected_screenpos)+ for i, object_ in enumerate(objects):+ assert np.array_equal(object_.screenpos, np.array(expected_screenpos[i]))++@pytest.mark.parametrize(("clip", "filetype", "fps", "maxduration", "t", "expected_error"),(