-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameProcessing.py
More file actions
57 lines (45 loc) · 1.7 KB
/
Copy pathFrameProcessing.py
File metadata and controls
57 lines (45 loc) · 1.7 KB
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
55
56
57
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 11 01:35:29 2024
@author: andyc
"""
import cv2
import os
def save_video_frames(video_path, output_folder):
"""
Reads a video file and saves each frame as an image in the specified output folder.
:param video_path: Path to the input video file.
:param output_folder: Path to the folder where the frames will be saved.
"""
# Make sure the output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Open the video file
cap = cv2.VideoCapture(video_path)
frame_count = 0
while True:
# Read a frame from the video
ret, frame = cap.read()
# If frame is read correctly, ret is True
if not ret:
break
# Save the frame to disk
frame_filename = os.path.join(output_folder, f"frame_{frame_count:04d}.jpg")
cv2.imwrite(frame_filename, frame)
frame_count += 1
# Release the video capture object
cap.release()
print(f"Finished. Extracted {frame_count} frames.")
def process_frame(frame):
"""
Process a single frame (for example, perform image segmentation).
This method is currently empty but can be filled with processing logic.
:param frame: The image frame to process.
"""
# Example: Implement your image segmentation or processing logic here
pass
# Example usage
if __name__ == "__main__":
video_path = 'path_to_your_video.mp4' # Update this to your video's path
output_folder = 'frames_output' # Update or keep this as your output directory
save_video_frames(video_path, output_folder)