.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_2_frame_selection.py: Frame selection, resizing, and grayscale conversion =================================================== * We want to resize each frame to be 720 pixels in width and 480 pixels in height. * Set ``target_size`` to (720, 480) * All the frames are not required. Let’s just capture exactly 12 random frames from the video. * Set ``mode`` to ``"random"`` * And finally, visualize the captured frames using :func:`mydia.make_grid` .. code-block:: python # Imports import matplotlib.pyplot as plt from mydia import Videos, make_grid # Initialize video path video_path = r"./sample_video/bigbuckbunny.mp4" # Configuring the parameters # For other paramaters available, view the code documentation. reader = Videos(target_size=(720, 480), num_frames=12, mode="random") # Call the 'read()' function to get the required video tensor # which will be of shape (1, 12, 480, 720, 3) video = reader.read(video_path) print("The shape of the tensor:", video.shape) # Plot the video frames in a grid grid = make_grid(video[0]) plt.imshow(grid) .. image:: /auto_examples/images/sphx_glr_plot_2_frame_selection_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none The shape of the tensor: (1, 12, 480, 720, 3) .. note:: The number of channels for a RGB video is 3 (indicated by the last value in the tuple). * Now let's read the video with the same configuration, but in **grayscale** * For this, set ``to_gray`` to `True` * Also, the function `make_grid()` takes certain arguments to construct the grid of frames of the video. For more info, view :func:`mydia.make_grid`. .. code-block:: python # Imports import matplotlib.pyplot as plt from mydia import Videos, make_grid # Initialize video path video_path = r"./sample_video/bigbuckbunny.mp4" # Configuring the parameters reader = Videos(target_size=(720, 480), to_gray=True, num_frames=12, mode="random") # Call the 'read()' function to get the required video tensor # which will be of shape (1, 12, 480, 720, 1) video = reader.read(video_path) print("The shape of the tensor:", video.shape) # Plot the video frames in a grid grid = make_grid(video[0], num_col=2) plt.imshow(grid, cmap="gray") .. image:: /auto_examples/images/sphx_glr_plot_2_frame_selection_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none The shape of the tensor: (1, 12, 480, 720, 1) .. note:: The number of channels for a video in gray scale is 1 (indicated by the last value in the tuple). **Total running time of the script:** ( 0 minutes 1.137 seconds) .. _sphx_glr_download_auto_examples_plot_2_frame_selection.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_2_frame_selection.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_2_frame_selection.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_