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.')