AhmedIbrahim007 commited on
Commit
5aa5bf2
·
verified ·
1 Parent(s): fbf6ddc

Update extract_frames.py

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