Saving captured frames to separate folders

Question:

Currently working on extracting frames from videos and have noticed that the images get overwritten. Would be nice to create a folder for each of the captured frames but I’m unsure how to do that.

data_folder = r"C:UsersjagacDownloadsData"
sub_folder_vid = "hmdb51_org"
path2videos = os.path.join(data_folder, sub_folder_vid)

for path, subdirs, files in os.walk(path2videos):
    for name in files:
        vids = os.path.join(path, name)

        cap = cv2.VideoCapture(vids)
        i = 0
        
        frame_skip = 10
        frame_count = 0

        while(cap.isOpened()):
            ret, frame = cap.read()
            if ret == False:
                break
            
            if i > frame_skip - 1:
                frame_count += 1
                
                path2store= r"C:UsersjagacOneDriveDocumentsCSC578finalHumanActionClassifierimages"
                os.makedirs(path2store, exist_ok= True)
                path2img = os.path.join(path2store, 'test_' + str(frame_count*frame_skip) + ".jpg")
                cv2.imwrite(path2img, frame)
                
                i = 0
                continue
            i += 1 
        cap.release()
        cv2.destroyAllWindows() 
Asked By: jagac

||

Answers:

The problem is that you’re not incorporating name into your path, so each video is overwriting the previous. You can either add the file name to path2img, or add name to the path2store variable before you make the directory.

Answered By: Dash
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.