Created
October 20, 2022 21:30
-
-
Save victormurcia/4b594db73a46f209b9081207eb56f1e2 to your computer and use it in GitHub Desktop.
get video frame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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