Spaces:
Runtime error
Runtime error
File size: 1,448 Bytes
5aa5bf2 f42083a 5aa5bf2 f42083a 5aa5bf2 a02b4d6 5aa5bf2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import cv2
import os, shutil
def ExtractFrames(videoPath):
directory = "data/output"
if os.path.exists(directory):
shutil.rmtree(directory)
# Path to the video file
video_path = videoPath
# Directory to save the frames
output_dir = 'data/output/extracted-frames'
os.makedirs(output_dir, exist_ok=True)
# Open the video file
video_capture = cv2.VideoCapture(video_path)
# Check if the video was opened successfully
if not video_capture.isOpened():
print(f"Error: Could not open video file {video_path}")
else:
print(f"Successfully opened video file {video_path}")
# Frame index
frame_index = 0
while True:
# Read the next frame from the video
success, frame = video_capture.read()
# If there are no more frames, break the loop
if not success:
print("No more frames to read or an error occurred.")
break
# Construct the filename for the frame
frame_filename = os.path.join(output_dir, f'frame_{frame_index:04d}.png')
# Save the current frame as an image file
cv2.imwrite(frame_filename, frame)
# Print the saved frame information
print(f'Saved {frame_filename}')
# Increment the frame index
frame_index += 1
# Release the video capture object
video_capture.release()
return output_dir
print('All frames extracted.')
|