Alternate approach of creating custom datasets from videos

There may be occasions when the images we find via the Internet do not satisfy our requirements or, we may find no images at all. This could be because of the uniqueness of the data, the use case at hand, copyright restrictions, the required resolution, etc. In this case, an alternative approach would be to record a video of the object you need, extract the frames of that video that meet your requirements, and save each frame as an individual image. How would we go about doing that?

Let's say that we have a skin condition that we are unable to find information about online. We need to somehow classify what this skin condition might be. However, in order to do this, we need to have an image of this condition. Accordingly, we could take a video of that skin condition and save the video file to a file. For the purposes of discussion, let’s say that we save the video with the filename myvideo.mp4.

Once this is complete, we could use the following Python script to break the video into images and save it into a folder. This function will take the path of the video file, break it into frames based on frequency, and save the corresponding images to a specified output location. Here is that function in its entirety:

import sys
import argparse
import os
import cv2
import numpy as np
print(cv2.__version__)

This function takes the path of the video file, breaks it into frames based on frequency, and saves the corresponding images to a specified output location:

def extractImages(pathIn, pathOut):
count = 0
vidcap = cv2.VideoCapture(pathIn)
success,image = vidcap.read()
success = True
while success:
vidcap.set(cv2.CAP_PROP_POS_MSEC,(count*10)) # Adjust frequency of frames here
success,image = vidcap.read()
print ('Read a new frame: ', success)
#Once we identify the last frame, stop there
image_last = cv2.imread("frame{}.png".format(count-1))
if np.array_equal(image,image_last):
break
cv2.imwrite( os.path.join("frames","frame{:d}.jpg".format(count)), image) # save frame as JPEG file
count = count + 1
pathIn = "myvideo.mp4"
pathOut = ""
extractImages(pathIn, pathOut)

As mentioned above, this will save every frame of the video in the current folder based on the frequency set. After running this script, you now will have created your image dataset and be able to use the images you need.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset