Beispiel #1
0
 def test_show_videos_dict(self):
     htmls = []
     with mock.patch('IPython.display.display', htmls.append):
         media.show_videos({
             'title1': media.moving_circle(),
             'title2': media.moving_circle(),
         })
     self.assertLen(htmls, 1)
     self.assertIsInstance(htmls[0], IPython.display.HTML)
     self.assertLen(re.findall('(?s)<table', htmls[0].data), 1)
     self.assertRegex(htmls[0].data, '(?s)title1.*<video.*title2.*<video')
     self.assertLen(re.findall('(?s)<video', htmls[0].data), 2)
Beispiel #2
0
    def test_video_streaming_write_read_roundtrip(self):
        shape = (62, 744)
        num_images = 20
        fps = 120
        bps = 400_000
        with tempfile.TemporaryDirectory() as directory_name:
            filename = os.path.join(directory_name, 'test.mp4')
            images = []
            with media.VideoWriter(filename, shape, fps=fps,
                                   bps=bps) as writer:
                for image in media.moving_circle(shape, num_images):
                    image_uint8 = media.to_uint8(image)
                    writer.add_image(image_uint8)
                    images.append(image_uint8)

            with media.VideoReader(filename) as reader:
                self.assertEqual(reader.num_images, num_images)
                self.assert_all_equal(reader.shape, shape)
                self.assertEqual(reader.fps, fps)
                self.assertBetween(reader.bps, 100_000, 500_000)
                self.assertEqual(reader.metadata.num_images, reader.num_images)
                self.assertEqual(reader.metadata.shape, reader.shape)
                self.assertEqual(reader.metadata.fps, reader.fps)
                self.assertEqual(reader.metadata.bps, reader.bps)
                for index, new_image in enumerate(reader):
                    self._check_similar(images[index], new_image, 7.0,
                                        f'index={index}')
Beispiel #3
0
 def test_compress_decompress_video_roundtrip(self):
     video = media.moving_circle((28, 66), num_images=10, dtype=np.uint8)
     data = media.compress_video(video)
     new_video = media.decompress_video(data)
     self.assertEqual(video.shape, new_video.shape)
     self.assertEqual(video.dtype, new_video.dtype)
     self._check_similar(video, new_video, max_rms=8.0)
Beispiel #4
0
 def test_show_videos_over_multiple_rows(self):
     htmls = []
     with mock.patch('IPython.display.display', htmls.append):
         media.show_videos([media.moving_circle()] * 12, columns=3)
     self.assertLen(htmls, 1)
     self.assertIsInstance(htmls[0], IPython.display.HTML)
     self.assertLen(re.findall('(?s)<table', htmls[0].data), 4)
     self.assertLen(re.findall('(?s)<video', htmls[0].data), 12)
Beispiel #5
0
 def test_show_videos_list(self):
     htmls = []
     with mock.patch('IPython.display.display', htmls.append):
         media.show_videos([media.moving_circle()] * 2)
     self.assertLen(htmls, 1)
     self.assertIsInstance(htmls[0], IPython.display.HTML)
     self.assertLen(re.findall('(?s)<table', htmls[0].data), 1)
     self.assertLen(re.findall('(?s)<video', htmls[0].data), 2)
Beispiel #6
0
 def test_show_video_gif(self):
     htmls = []
     with mock.patch('IPython.display.display', htmls.append):
         media.show_video(media.moving_circle(), codec='gif')
     self.assertLen(htmls, 1)
     self.assertIsInstance(htmls[0], IPython.display.HTML)
     self.assertContainsInOrder(['<img', 'src="data:image/gif'],
                                htmls[0].data)
Beispiel #7
0
 def test_show_video(self):
     htmls = []
     with mock.patch('IPython.display.display', htmls.append):
         media.show_video(media.moving_circle())
     self.assertLen(htmls, 1)
     self.assertIsInstance(htmls[0], IPython.display.HTML)
     self.assertLen(re.findall('(?s)<video', htmls[0].data), 1)
     self.assertRegex(htmls[0].data, '(?s)<video .*>.*</video>')
Beispiel #8
0
 def test_moving_circle(self, dtype):
     video = media.moving_circle(shape=(256, 256),
                                 num_images=10,
                                 dtype=dtype)
     self.assert_all_equal(video.shape, (10, 256, 256, 3))
     mean_image = np.mean(video, axis=0)
     expected_mean = 0.329926 if dtype == 'float32' else 84.295
     self.assertAlmostEqual(np.std(mean_image), expected_mean, delta=0.001)
Beispiel #9
0
 def test_html_from_compressed_video(self):
     shape = (240, 320)
     video = media.moving_circle(shape, 10)
     text = media.html_from_compressed_video(media.compress_video(video),
                                             shape[1], shape[0])
     self.assertGreater(len(text), 2_000)
     self.assertContainsInOrder(
         ['<video', '<source src="data', 'type="video/mp4"/>', '</video>'],
         text)
Beispiel #10
0
 def test_video_read_write_odd_dimensions(self):
     video = media.moving_circle((35, 97), num_images=4, dtype=np.uint8)
     fps = 60
     bps = 40_000_000
     with tempfile.TemporaryDirectory() as directory_name:
         path = pathlib.Path(directory_name) / 'test5.mp4'
         media.write_video(path, video, fps=fps, bps=bps)
         new_video = media.read_video(path)
     self.assertEqual(new_video.dtype, np.uint8)
     self._check_similar(video, new_video, max_rms=5.0)
Beispiel #11
0
 def test_show_save_video(self):
     video = media.moving_circle((32, 32), num_images=10)
     with tempfile.TemporaryDirectory() as directory_name:
         directory_path = pathlib.Path(directory_name)
         with media.show_save.to_dir(directory_path):
             with mock.patch('IPython.display.display'):
                 media.show_videos({'video0': video, 'video1': video})
         for i in range(2):
             path = directory_path / f'video{i}.mp4'
             self.assertTrue(path.is_file())
             self.assertBetween(path.stat().st_size, 1500, 3000)
Beispiel #12
0
 def test_video_non_streaming_write_read_roundtrip(self, use_generator):
     shape = (240, 320)
     num_images = 10
     fps = 40
     qp = 20
     original_video = media.to_uint8(media.moving_circle(shape, num_images))
     video = (
         image
         for image in original_video) if use_generator else original_video
     with tempfile.TemporaryDirectory() as directory_name:
         filename = os.path.join(directory_name, 'test.mp4')
         media.write_video(filename, video, fps=fps, qp=qp)
         new_video = media.read_video(filename)
         self._check_similar(original_video, new_video, 3.0)
Beispiel #13
0
    def test_video_streaming_read_write(self):
        shape = (400, 400)
        num_images = 4
        fps = 60
        bps = 40_000_000
        video = media.to_uint8(media.moving_circle(shape, num_images))
        with tempfile.TemporaryDirectory() as directory_name:
            filename1 = os.path.join(directory_name, 'test1.mp4')
            filename2 = os.path.join(directory_name, 'test2.mp4')
            media.write_video(filename1, video, fps=fps, bps=bps)

            with media.VideoReader(filename1) as reader:
                with media.VideoWriter(filename2,
                                       reader.shape,
                                       fps=reader.fps,
                                       bps=reader.bps,
                                       encoded_format='yuv420p') as writer:
                    for image in reader:
                        writer.add_image(image)

            new_video = media.read_video(filename2)
            self._check_similar(video, new_video, 3.0)
Beispiel #14
0
 def create_video(shape, num_images=5):
     video = media.moving_circle(shape[:2], num_images, dtype=dtype)
     return video.mean(axis=-1).astype(dtype) if len(
         shape) == 2 else video[..., :shape[2]]