|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +import cv2 |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from plantcv.plantcv.visualize.time_lapse_video import time_lapse_video |
| 7 | + |
| 8 | +@pytest.mark.parametrize("display",[False,True]) |
| 9 | +def test_plantcv_visualize_time_lapse_video_passes(display, tmpdir): |
| 10 | + """Test for PlantCV.""" |
| 11 | + # Generate 3 test images and saved in tmpdir |
| 12 | + list_im = [] |
| 13 | + for i in range(3): |
| 14 | + temp_img = np.random.rand(3,3) |
| 15 | + min_, max_ = np.nanmin(temp_img), np.nanmax(temp_img) |
| 16 | + temp_img = np.interp(temp_img, (min_, max_), (0, 255)).astype('uint8') |
| 17 | + img_i_path = os.path.join(tmpdir, f"img{i}.png") |
| 18 | + cv2.imwrite(img_i_path, temp_img) |
| 19 | + list_im.append(img_i_path) |
| 20 | + |
| 21 | + # list_im = [os.path.join(tmpdir, img) for img in os.listdir(tmpdir) if img.endswith('.png')] |
| 22 | + |
| 23 | + vid_name = os.path.join(tmpdir, 'test_time_lapse_video.mp4') |
| 24 | + _ = time_lapse_video(img_list=list_im, out_filename=vid_name, fps=29.97, display=display) |
| 25 | + assert os.path.exists(vid_name) |
| 26 | + |
| 27 | +@pytest.mark.parametrize("list_im_f", |
| 28 | + [([]), # empty list |
| 29 | + (['./this_img_does_not_exist.png'])]) # non existent image |
| 30 | +def test_plantcv_visualize_time_lapse_video_errors(list_im_f, tmpdir): |
| 31 | + """Test for PlantCV.""" |
| 32 | + with pytest.raises(RuntimeError): |
| 33 | + _ = time_lapse_video(img_list=list_im_f, fps=29.97) |
| 34 | + |
| 35 | + |
| 36 | +# not all images have the same size (essential to generate a video) |
| 37 | +def test_plantcv_visualize_time_lapse_video_different_img_sizes_warns(tmpdir, capsys): |
| 38 | + """Test for PlantCV.""" |
| 39 | + # Generate 3 test images of different size and save in tmpdir |
| 40 | + list_im = [] |
| 41 | + for i in range(2): |
| 42 | + temp_img = np.random.rand(i+2, 3) |
| 43 | + min_, max_ = np.nanmin(temp_img), np.nanmax(temp_img) |
| 44 | + temp_img = np.interp(temp_img, (min_, max_), (0, 255)).astype('uint8') |
| 45 | + img_i_path = os.path.join(tmpdir, f"img{i}.png") |
| 46 | + cv2.imwrite(img_i_path, temp_img) |
| 47 | + list_im.append(img_i_path) |
| 48 | + |
| 49 | + vid_name = os.path.join(tmpdir, 'test_time_lapse_video.mp4') |
| 50 | + _ = time_lapse_video(img_list=list_im, out_filename=vid_name, fps=29.97, display=True) |
| 51 | + _, err = capsys.readouterr() |
| 52 | + |
| 53 | + assert "Warning" in err and os.path.exists(vid_name) |
0 commit comments