moten.io module

moten.io.apply_mask(mask, generator)[source]
Parameters:
  • mask (2D np.ndarray)

  • generator (generator) – Yields a video frame

Yields:

masked_image (2D np.ndarray) – Masked image of each frame (i.e. original_image[mask])

Examples

>>> import moten
>>> video_file = 'http://anwarnunez.github.io/downloads/avsnr150s24fps_tiny.mp4'
>>> small_size = (36, 64)  # downsample to (vdim, hdim) 16:9 aspect ratio
>>> oim = next(moten.io.generate_frame_difference_from_greyvideo(video_file, size=small_size))
>>> mask = np.zeros(small_size, dtype=np.bool)
>>> mask[16:, :40] = True
>>> nim = next(moten.io.apply_mask(mask, moten.io.generate_frame_difference_from_greyvideo(video_file, size=small_size)))
>>> np.allclose(oim[16:, :40], nim)
moten.io.generate_frame_difference_from_greyvideo(video_file, size=None, nimages=inf, dtype='float32')[source]

Generates the difference between the current frame and the previous frame.

Notes

The video is assumed to be greyscale.

Parameters:
  • video_file (str) – Full path to the video file. This can be a video file on disk or from a website.

  • size (optional, tuple (vdim, hdim)) – The desired output image size. If specified, the image is scaled or shrunk to this size. If not specified, the original size is kept.

  • nimages (optional, int) – If specified, only nimages frames are loaded.

Yields:

greyscale_image_difference (2D np.ndarray, (vdim, hdim)) – The difference image: (current_frame - previous_frame). Pixel values are in the (-1, 1) range.

moten.io.generate_frames_from_greyvideo(video_file, size=None, nimages=inf)[source]

Yields one frame from the greyscale video file.

Notes

The video is assumed to be greyscale.

Parameters:
  • video_file (str) – Full path to the video file. This can be a video file on disk or from a website.

  • size (optional, tuple (vdim, hdim)) – The desired output image size. If specified, the image is scaled or shrunk to this size. If not specified, the original size is kept.

  • nimages (optional, int) – If specified, only nimages frames are loaded.

Yields:

greyscale_image (2D np.ndarray, (vdim, hdim)) – Pixel values are in the 0-1 range.

moten.io.imagearray2luminance(uint8arr, size=None, filter=Resampling.LANCZOS, dtype=<class 'numpy.float64'>)[source]

Convert an array of uint8 RGB images to a luminance image

Parameters:
  • uint8arr (4D np.ndarray, (nimages, vdim, hdim, color)) – The uint8 RGB frames.

  • size (optional, tuple (vdim, hdim)) – The desired output image size.

  • filter (to be passed to PIL)

Returns:

luminance_array – The luminance image representation. Pixel values are in the 0-100 range.

Return type:

3D np.ndarray, (nimages, vdim, hdim)

moten.io.load_image_luminance(image_files, hdim=None, vdim=None)[source]

Load a set of RGB images and return its luminance representation.

Parameters:
  • image_files (list-like, (nimages,)) – A list of file names. The images should be in RGB uint8 format.

  • vdim (int, optional)

  • hdim (int, optional) – Vertical and horizontal dimensions, respectively. If provided the images will be scaled to this size.

Returns:

arr – The luminance representation of the images.

Return type:

3D np.array (nimages, vdim, hdim)

moten.io.resize_image(im, size=(96, 96), filter=Resampling.LANCZOS)[source]

Resize an image and return its array representation.

Parameters:
  • im (str, np.ndarray(uint8), or PIL.Image object) – The path to the image, an image array, or a loaded PIL.Image.

  • size (tuple, (vdim, hdim)) – The desired output image size.

Returns:

arr – The resized image array

Return type:

uint8 np.array, (vdim, hdim, 3)

moten.io.video2grey(video_file, size=None, nimages=inf)[source]

Convert the video frames to greyscale images.

This function computes the mean across RGB color channels.

Parameters:
  • video_file (str) – Full path to the video file. This can be a video file on disk or from a website.

  • size (optional, tuple (vdim, hdim)) – The desired output image size. If specified, the image is scaled or shrunk to this size. If not specified, the original size is kept.

  • nimages (optional, int) – If specified, only nimages frames are converted to greyscale.

Returns:

greyscale_images – Pixel values are in the 0-1 range.

Return type:

3D np.ndarray, (nimages, vdim, hdim)

moten.io.video2luminance(video_file, size=None, nimages=inf)[source]

Convert the video frames to luminance images.

Internally, this function loads one video frame into memory at a time. Tt converts the RGB pixel values from one frame to CIE-LAB pixel values. It then keeps the luminance channel only. This process is performed for all frames requested or until we reach the end of the video file.

Parameters:
  • video_file (str) – Full path to the video file. This can be a video file on disk or from a website.

  • size (optional, tuple (vdim, hdim)) – The desired output image size. If specified, the image is scaled or shrunk to this size. If not specified, the original size is kept.

  • nimages (optional, int) – If specified, only nimages frames are converted to luminance.

Returns:

luminance_images – Pixel values are in the 0-100 range.

Return type:

3D np.ndarray, (nimages, vdim, hdim)

moten.io.video_buffer(video_file, nimages=inf)[source]

Generator for a video file.

Yields individual uint8 images from a video file. The video is loaded into memory one frame at a time.

Parameters:
  • video_file (str) – Full path to the video file. This can be a video file on disk or from a website.

  • nimages (optional, int) – If specified, only nimages frames are loaded.

Yields:

video_frame (uint8 3D np.ndarray, (vdim, hdim, color)) – Each next() call yields an uint8 RGB frame from video.

Example

>>> video_file = 'http://anwarnunez.github.io/downloads/avsnr150s24fps_tiny.mp4'
>>> image_buffer = video_buffer(video_file, nimages=50)
>>> movie = np.asarray([frame for frame in image_buffer])
>>> print(movie.shape) # (nimages, vdim, hdim, color)
(50, 144, 256, 3)