# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: actionshandstools
#     language: python
#     name: actionshandstools
# ---

# +
import os
import cv2


# %cd /home/egoodman/intermountain/videos




def extract_frames_from_video(video_location, num_frames_to_collect):
    vid_cap = cv2.VideoCapture(video_location)
    num_frames_in_video = int(vid_cap.get(cv2.CAP_PROP_FRAME_COUNT))
    
    sample_every = int(num_frames_in_video / (num_frames_to_collect-1))
    
    print("\n\nExtracting frames from video at location {} having {} frames".format(video_location, num_frames_in_video))
    print("Therefore, we will sample every {} frames".format(sample_every))
    
    count = 1
    frame_no = 0
    ret = True
    while ret:
        vid_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
        ret, frame = vid_cap.read()
        if ret:
            frame_name = video_location[-15:-4] + "-frame-" + str(frame_no) + ".jpg"
            print("Writing frame", count, frame_name)
            cv2.imwrite(frame_name, frame)
            frame_no += sample_every
            count += 1

    
    
    
head_dir = "/home/egoodman/intermountain/videos/"
for file in os.listdir():
    if file.endswith(".avi"):
        complete_video_path = head_dir + file
        extract_frames_from_video(complete_video_path, 20)
        
        

StarcoderdataJupyterScriptsDedupFiltered