chuangg / CLEVRER

PyTorch implementation of ICLR 2020 paper "CLEVRER: CoLlision Events for Video REpresentation and Reasoning"
108 stars 25 forks source link

Please download videos from project page, and extract video frames. how to do that? #1

Closed jun0wanan closed 3 years ago

jun0wanan commented 4 years ago

Thank you!

jun0wanan commented 4 years ago

hi, Please download videos from project page, and extract video frames. I checked the mask. Json and wondered if 127 frames were extracted from each video? hope your reply ,or I will not know how to conduct the experiment ....

adiah80 commented 4 years ago

I used the below code to obtain image frames from videos:

import cv2

def get_image_arr(video_path):
    '''Return the Image frame array corresponding to a video path'''

    image_arr = []
    vidcap = cv2.VideoCapture(video_path)
    while True:
        success, image = vidcap.read()
        if(not success): 
            break
        image_arr.append(image[:,:,::-1])
    return image_arr

You should get 128 frames from each video.

iGeralt commented 3 years ago

Use following code to get frames from videos and it will automatically place video frames in the folder as described in readme.

import os
import cv2

if not os.path.exists('video_frames'):
    os.makedirs('video_frames')

#create sim_00000 to sim_19999
for i in range(0,20000):
    num = str(i).zfill(5)
    if not os.path.exists('video_frames/sim_%s'%num):
        os.makedirs('video_frames/sim_%s'%num)

basepath = 'Videos/'
list_of_videos = {}
for entry in os.listdir(basepath):
    new_path = os.path.join(basepath, entry) + str("/")
    #print(new_path)
    for new_entry in os.listdir(new_path):
        video_num = new_entry[6:11]
        video_path = os.path.join(new_path, new_entry)
        if os.path.isfile(video_path):
            list_of_videos[video_num] = video_path 
            #print(video_path)

for vnum, vpath in list_of_videos.items():
    vidcap = cv2.VideoCapture(vpath)
    success,image = vidcap.read()
    count = 0
    while success:
        c = str(count).zfill(5)
        write_path = "video_frames/sim_" + str(vnum) + "/frame_" + c + ".png"
        cv2.imwrite(write_path, image)          
        success,image = vidcap.read()
        print('Read a new frame: ', success)
        count += 1

Correct me if I am wrong. :)

jun0wanan commented 3 years ago

Thank you!

At 2020-10-08 22:56:19, "Sujeet Maurya" notifications@github.com wrote:

Use following code to get frames from videos and it will automatically place video frames in the folder as described in readme. ` import os import cv2

create parent path

if not os.path.exists('video_frames'): os.makedirs('video_frames')

create sim_00000 to sim_19999

for i in range(0,20000): num = str(i).zfill(5) if not os.path.exists('videoframes/sim%s'%num): os.makedirs('videoframes/sim%s'%num)

basepath = 'Videos/' list_of_videos = {} for entry in os.listdir(basepath): new_path = os.path.join(basepath, entry) + str("/")

print(new_path)

for new_entry in os.listdir(new_path): video_num = new_entry[6:11] video_path = os.path.join(new_path, new_entry) if os.path.isfile(video_path): list_of_videos[video_num] = video_path

print(video_path)

for vnum, vpath in list_of_videos.items(): vidcap = cv2.VideoCapture(vpath) success,image = vidcap.read() count = 0 while success: c = str(count).zfill(5) write_path = "videoframes/sim" + str(vnum) + "/frame_" + c + ".png" cv2.imwrite(write_path, image) # save frame as JPEG file success,image = vidcap.read() print('Read a new frame: ', success) count += 1 `

Correct me if I am wrong. :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

chuangg commented 3 years ago

LGTM!