/opencv-scripts

Demo OpenCV scripts to use for projects.

Primary LanguagePython

OpenCV Scripts

Demo scripts to use for OpenCV projects.

Table of Contents

Helper scripts:

  • record_video.py : record videos using terminal and save to desired location in local system
  • downsize_video.py : downsample a video with fps, for eg. recorded at 24 fps but downsample at 4 fps
  • test_output.py : view the tentative output window for testing
  • process_output.py : post-process input video stream and get live predictions
  • train_model.py : train a model on mnist dataset and save it to use with test_output.py
  • augment_dataset.py : script to process all raw videos and create an augmented data-set from the selected data augmentation methods

Dataset Augmentation:

  • flip_video.py : horizontally flip recorded videos
  • convert_to_gray.py : convert colored videos to grayscale
  • invert_color.py : invert image colors instead of converting to grayscale
  • blur_video.py : add gaussian/averaging blur to videos

TODO:

  • create the model to train using PyTorch
  • finalize data-set to use
  • add padding for shorter videos, or clip frames for longer videos, to train on in case number of input frames is fixed
  • create a new script to train and test model outputs
  • create a new output window for live testing
  • complete report by November 19th

Project Outline:

Project Slides Deck:


Requirements and Installation

The following steps are confirmed to work on Windows 10 and macOS.

Step 1

Cloning the repository

The first step is to clone this repository into a desired local directory, and switch to the project:

git clone https://github.com/manikd31/opencv-scripts.git
cd opencv-scripts

Step 2

Installing required dependencies

Once cloned, install the dependencies specified in the file requirements.txt within a virtual environment, preferably using miniconda. To create the virtual environment, use the command:

conda create -y -n opencv-scripts
conda activate opencv-scripts

And then, install the dependencies using:

pip install -r requirements.txt

Getting Started

The first step to using any of the scripts is to populate/update the constants.py file with the relevant default values used across the project. The most relevant ones will be:

TEST_PATH_IN = r"C:/Users/user/Desktop"
TEST_PATH_OUT = r"C:/Users/user/Desktop"
TEST_FILE_NAME = "video_name.mp4"

Usage

To begin using the scripts, follow the commands mentioned below:

Video Recording

Use this script to record several videos in your desired directory just with the help of a few key presses.

  • Run the script using the command:

    python record_video.py
    
  • When prompted about the video resolution, select the one you wish to use (select 480p for low-res 640x480 videos)

    Select the video resolution:
     > 480p
       720p
       1080p
    
  • Next, specify the path to save the recorded videos to (if the directory doesn't exist, the program will create one)

    Enter the path to save the videos to:     C:/Users/user/Desktop
    
  • And finally, enter the name of the video (with or without the extension .MP4)

    Enter the video name:      video_name.mp4
    

When recording, the videos will be saved with a number at the end, for example, the first 2 videos with the name video_name.mp4 will become video_name_0.mp4 and video_name_1.mp4 to avoid overwriting files with same names.

To manipulate the video recording mechanism, there are key shortcuts on the screen. The display also shows if a video is being recorded or not, and how long the video has been recorded so far. These keypresses are as follows:

  • Press R to start recording a video if not currently recording.
  • Press E to end the current video recording and save it to the directory.
  • Press Q to terminate the script and get back to the terminal.

Downsizing Video


Data Augmentation

Use these data augmentation methods on raw videos to create a variety of different styles of videos to enhance the dataset.

Horizontal Flipping


Grayscale Conversion


Color Inversion


Video Blurring