Spaces:
Runtime error
Runtime error
AhmedIbrahim007
commited on
Update extract_frames.py
Browse files- 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 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
if
|
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 |
-
|
|
|
|
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.')
|