Skip to content

Instantly share code, notes, and snippets.

@victormurcia
Created October 20, 2022 21:30
Show Gist options
  • Select an option

  • Save victormurcia/4b594db73a46f209b9081207eb56f1e2 to your computer and use it in GitHub Desktop.

Select an option

Save victormurcia/4b594db73a46f209b9081207eb56f1e2 to your computer and use it in GitHub Desktop.
get video frame
def get_frame(video_file, frame_index):
"""
Args:
video_file: (str) path to .MP4 video file
frame_index: (int) query frame index
Returns:
frame: (ndarray, size (y, x, 3)) video frame
Uses OpenCV BGR channels
"""
video_capture = cv2.VideoCapture(video_file)
video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
success, frame = video_capture.read()
if not success:
raise ValueError(
"Couldn't retrieve frame {0} from video {1}".format(
frame_index,
video_file
)
)
return frame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment