-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvidtophoto.py
74 lines (55 loc) · 2.31 KB
/
vidtophoto.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import cv2
import os
def video_to_frames(video_path, output_folder):
# Open the video file
video_capture = cv2.VideoCapture(video_path)
# Create the output folder if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Get the frames per second (fps) of the input video
fps = video_capture.get(cv2.CAP_PROP_FPS)
# Get the width and height of the frames
frame_width = int(video_capture.get(3))
frame_height = int(video_capture.get(4))
# Set the format for saving the frames
file_format = '.jpg'
# Counter for naming frames
frame_count = 0
while True:
# Read the next frame
success, frame = video_capture.read()
if not success:
break
# Save the frame as an image file
frame_filename = f"{output_folder}/frame_{frame_count:04d}{file_format}"
cv2.imwrite(frame_filename, frame)
# Increment the frame counter
frame_count += 1
# Release the video capture object
video_capture.release()
print(f"Frames extracted: {frame_count}")
print(f"Frames per second (fps): {fps}")
print(f"Frame dimensions: {frame_width}x{frame_height}")
def create_folder(folder_name):
# Specify the path for the new folder
folder_path = os.path.join(os.getcwd(), folder_name)
# Create the folder if it doesn't exist
if not os.path.exists(folder_path):
os.makedirs(folder_path)
print(f"Folder '{folder_name}' created successfully at {folder_path}")
else:
print(f"Folder '{folder_name}' already exists at {folder_path}")
if __name__ == "__main__":
# Replace 'input_video.avi' with the path to your input video file
folder_path = 'data/mTBI/video_sequence'
# Iterate through all files in the folder
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
# Check if the current item is a file (not a subdirectory)
if os.path.isfile(file_path):
# Your processing code goes here
print(f"Processing file: {filename}")
# Example: Print the contents of each file
new_folder = "data/mTBI/video_frames/" + "output_" + filename
create_folder(new_folder)
video_to_frames(file_path, new_folder)