Beispiel #1
0
def _get_repr_html_ffmpeg(images: List[PilImage]) -> str:
  """Runs ffmpeg to get the mp4 encoded <video> str."""
  # Find number of digits in len to give names.
  num_digits = len(str(len(images))) + 1
  with tempfile.TemporaryDirectory() as video_dir:
    for i, img in enumerate(images):
      f = os.path.join(video_dir, f'img{i:0{num_digits}d}.png')
      img.save(f, format='png')

    ffmpeg_args = [
        '-framerate', str(_VISU_FRAMERATE),
        '-i', os.path.join(video_dir, f'img%0{num_digits}d.png'),
        # Using native h264 to encode video stream to H.264 codec
        # Default encoding does not seems to be supported by chrome.
        '-vcodec', 'h264',
        # When outputting H.264, `-pix_fmt yuv420p` maximize compatibility
        # with bad video players.
        # Ref: https://trac.ffmpeg.org/wiki/Slideshow
        '-pix_fmt', 'yuv420p',
        # Native encoder cannot encode images of small scale
        # or the the hardware encoder may be busy which raises
        # Error: cannot create compression session
        # so allow software encoding
        # '-allow_sw', '1',
    ]
    video_path = utils.as_path(video_dir) / 'output.mp4'
    ffmpeg_args.append(os.fspath(video_path))
    utils.ffmpeg_run(ffmpeg_args)
    video_str = utils.get_base64(video_path.read_bytes())
  return (
      f'<video height="{THUMBNAIL_SIZE}" width="175" '
      'controls loop autoplay muted playsinline>'
      f'<source src="data:video/mp4;base64,{video_str}"  type="video/mp4" >'
      '</video>'
  )
Beispiel #2
0
    def repr_html(self, ex: np.ndarray) -> str:
        """Images are displayed as thumbnail."""
        # Normalize image and resize
        img = create_thumbnail(ex)

        # Convert to base64
        img_str = utils.get_base64(lambda buff: img.save(buff, format='PNG'))

        # Display HTML
        return f'<img src="data:image/png;base64,{img_str}" alt="Img" />'
Beispiel #3
0
def _get_repr_html_gif(images: List[PilImage]) -> str:
    """Get the <img/> str."""
    def write_buff(buff):
        images[0].save(
            buff,
            format='GIF',
            save_all=True,
            append_images=images[1:],
            duration=1000 / _VISU_FRAMERATE,
            loop=0,
        )

    # Convert to base64
    gif_str = utils.get_base64(write_buff)
    return f'<img src="data:image/png;base64,{gif_str}" alt="Gif" />'
Beispiel #4
0
    def repr_html(self, ex: np.ndarray) -> str:
        """Audio are displayed in the player."""
        if self.sample_rate:
            rate = self.sample_rate
        else:
            # We should display an error message once to warn the user the sample
            # rate was auto-infered. Requirements:
            # * Should appear only once (even though repr_html is called once per
            #   examples)
            # * Ideally should appear on Colab (while `logging.warning` is hidden
            #   by default)
            rate = 16000

        audio_str = utils.get_base64(lambda buff: _save_wav(buff, ex, rate))
        return (f'<audio controls src="data:audio/ogg;base64,{audio_str}" '
                ' controlsList="nodownload" />')
Beispiel #5
0
    def repr_html(self, ex: np.ndarray) -> str:
        """Video are displayed as GIFs."""
        # Use GIF as there is no easy way to generate a HTML5 compatible video
        # without FFMPEG or other lib that the user is likelly to have on
        # colab by default.
        gif = [image_feature.create_thumbnail(frame) for frame in ex]

        def write_buff(buff):
            gif[0].save(
                buff,
                format='GIF',
                save_all=True,
                append_images=gif[1:],
                # Could add a frame_rate kwargs in __init__ to customize this.
                duration=41,  # 41ms / img ~= 24 img / sec
                loop=0,
            )

        # Convert to base64
        gif_str = utils.get_base64(write_buff)

        # Display HTML
        return f'<img src="data:image/gif;base64,{gif_str}"  alt="Gif" />'
Beispiel #6
0
    def repr_html(self, ex: np.ndarray) -> str:
        """Images are displayed as thumbnail."""
        PIL_Image = lazy_imports_lib.lazy_imports.PIL_Image  # pylint: disable=invalid-name

        # Normalize image and resize
        _, _, c = ex.shape
        postprocess = _postprocess_noop
        if c == 1:
            ex = ex.squeeze(axis=-1)
            mode = 'L'
        elif ex.dtype == np.uint16:
            mode = 'I;16'
            postprocess = _postprocess_convert_rgb
        else:
            mode = None
        img = PIL_Image.fromarray(ex, mode=mode)
        img = postprocess(img)
        img.thumbnail((128, 128))  # Resize the image

        # Convert to base64
        img_str = utils.get_base64(lambda buff: img.save(buff, format='PNG'))

        # Display HTML
        return f'<img src="data:image/png;base64,{img_str}" alt="Img" />'
def _repr_html(ex: np.ndarray) -> str:
    """Returns the HTML str representation of an Image with BBoxes."""
    img = _build_thumbnail_with_bbox(ex)
    img_str = utils.get_base64(lambda buff: img.save(buff, format='PNG'))
    return f'<img src="data:image/png;base64,{img_str}" alt="Img" />'