Skip to content

Instantly share code, notes, and snippets.

@amogh112
Created September 9, 2020 02:47
Show Gist options
  • Save amogh112/6dade7a5b1a0bce465ae0f3860d9ec7a to your computer and use it in GitHub Desktop.
Save amogh112/6dade7a5b1a0bce465ae0f3860d9ec7a to your computer and use it in GitHub Desktop.
extract frames from videos
import tqdm
from multiprocessing import Pool
def process_movie(movie):
print("Processing movie : ", movie)
path_movie_videos = os.path.join(path_all_movies, movie)
list_avi_files = os.listdir(path_movie_videos)
count=1
for v in tqdm.tqdm(list_avi_files):
v_name = os.path.splitext(v)[0]
path_frames = os.path.join(path_output_frames,movie,v_name)
if not os.path.exists(path_frames):
os.makedirs(path_frames)
vid = os.path.join(path_movie_videos,v)
vidcap = cv2.VideoCapture(vid)
def getFrame(sec):
vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
hasFrames,image = vidcap.read()
path_out = path_frames+"/"+str(count)+".jpg"
if hasFrames:
#print("saving at ",path_out)
# pass
cv2.imwrite(path_out, image) # Save frame as JPG file
return hasFrames
sec = 0
frameRate = 0.5 # Change this number to 1 for each 1 second
success = getFrame(sec)
while success:
count = count + 1
sec = sec + frameRate
sec = round(sec, 2)
success = getFrame(sec)
#print("Done ", v)
print("___DONE WITH MOVIE___", movie)
if __name__ == "__main__":
path_all_movies = r'/proj/vondrick/datasets/mpii_large'
path_output_frames = r'/proj/vondrick/datasets/mpii_large_frames/frames'
list_movies = os.listdir(path_all_movies)
movie_num_to_start = 0
cpu_count = 30
# Single process
# for movie in tqdm.tqdm(list_movies[movie_num_to_start:]):
# process_movie(movie)
# Multiple processes
pool = Pool(cpu_count)
pool.map_async(process_movie, list_movies)
pool.close()
pool.join()
#with Pool(4) as p:
# p.map(process_movie, list_movies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment