2

I am currently working on making a dataset for a computer vision problem. I wanted to add some data to the previous ones I had. So I wanted to get around ~3000 frames from 2 different videos.

I used openCV because I knew the capture feature but I'm not sure about this because my memory is really exploding. I was using pickle file for the previous dataset that was already processed and I had no problem having that much information with my memory. Maybe my code is horrible without noticing it...

Here is my code to get around 3000 frames from the videos :

import cv2
video_name1 = "videosDataset/AMAZExNHORMS2019_Lo-res.mp4"

video_name2 = "videosDataset/CAMILLATHULINS2019_Lo-res.mp4"

def getAllFrames(videoName):
    video_name = videoName
    property_id =int(cv2.CAP_PROP_FRAME_COUNT)
    cap = cv2.VideoCapture(video_name) #video_name is the video being called
    frames = []
    length = int(cv2.VideoCapture.get(cap, property_id))
    print(length)
    minL = int(length/2)
    maxL = int(2*length/3)
    print(minL,maxL)
    for i in range(minL,maxL):
        cap.set(1,i); # Where frame_no is the frame you want
        ret, frame = cap.read() # Read the frame
        frames.append(frame)
        print(str(round((i-minL)/(maxL-minL)*100, 2))+'%')
    return frames

frames1 = getAllFrames(video_name1)

I would like to know if there is a better way to do this. Thank you

Otor
  • 410
  • 10
  • 21

2 Answers2

1

Assuming that by making a dataset, you mean that want to save all the frames individually for use in the dataset, the easiest option would probably be to use a tool like ffmpeg to do so. See here for an example to do so. Ffmpeg will support a number of image file formats, probably including the format you want to save the image in.

TheBarrometer
  • 356
  • 1
  • 7
1

The problem here is the compresion - when read, each frame is stored as numpy array which is rather expensive. For example - one RGB frame of 1280 x 720 pixels is about 200 kB in jpg format, 1.2 MB in png format, 2.7 MB when stored in numpy uint8 array and 22 MB when stored in numpy float64 array. Easiest solution is to store each frame to disk as jpg image (e.g. by cv2.imwrite) instead of creating an array with all frames.

  • Thank you for the info ! I didn't though that it would be that heavy but when you think about it, it is pretty obvious. I'll try to reduce the shape of the frames before saving into an array or save it as you mentionned :) – Otor Jul 19 '19 at 13:31