Video to Image (JPG)

Image from https://www.pexels.com/@caleboquendo/
Image from https://www.pexels.com/@caleboquendo/

There are many situations when we need to capture frames from video clips into images so that we can use these images for processing (such as Machine Learning and Prediction). In the blog, we shall share how we do this in Python 3.x (I am using 3.9) and some open-source libraries.


1. Open Source Libraries

opencv-python==4.6.0.66
moviepy==1.0.3
argparse==1.4.0

2. Requirements

  1. Capture all frames in a video
  2. Capture some frames in a video by skipping some frames.
  3. Capture frames in greyscale.

3. Command Line Options

Based on the above requirements, we have

usage: Video2JPEG [-h] -i INPUT_FILE [-g] [-s FRAMES_TO_SKIP]

capture frames in video to JPEG files

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT_FILE, --input-file INPUT_FILE
                        path to video file
  -g, --grey_scale      having images in grey scale
  -s FRAMES_TO_SKIP, --frames-to-skip FRAMES_TO_SKIP
                        number frame to skip

4. Tests

I used videos from Taryn Elliott, She has very nice videos :-).

4.1 Video on Penguin

Video: https://www.pexels.com/video/a-cute-penguin-walking-on-the-beach-9116112/

metadata from the command line tool

{
    "file_name": "pexels-taryn-elliott-9116112.mp4",
    "frame_rate": 25,
    "frame_count": 632,
    "frame_width": 640,
    "frame_height": 360,
    "bit_rate": 535
}


Frame 10:


Frame 10 (greyscale):



4.2 Video on Beach

Video: https://www.pexels.com/video/aerial-footage-of-beautiful-beach-9624127/

metadata from the command line tool
{
    "file_name": "pexels-taryn-elliott-9624127.mp4",
    "frame_rate": 25,
    "frame_count": 540,
    "frame_width": 640,
    "frame_height": 360,
    "bit_rate": 650
}
this one has lesser frames as compared to the previous one

Frame 10:


Frame 10 (greyscale)


Appendix Source code: (only 50 lines of code :-))


https://gist.github.com/dennisseah/618c069d0edf203345fff365e9455456







Comments

Popular posts from this blog

Happy New Year, 2024 from DALL-E

Bing Search as tool for OpenAI

OpenAI: Functions Feature in 2023-07-01-preview API version