-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.video_to_image.py
51 lines (33 loc) · 1.5 KB
/
1.video_to_image.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
import cv2
import os
# Function to extract frames from a video until reaching the desired frame count
def extract_frames(video_file):
cap = cv2.VideoCapture(video_file)
# Ensure the video is opened correctly
if not cap.isOpened():
print(f"Error: Could not open video file {video_file}")
return
frame_rate = 3 # Desired frame rate (1 frame every 0.5 seconds)
frame_count = 0
# Get the video file's name without extension
video_name = os.path.splitext(os.path.basename(video_file))[0]
# Create an output folder with a name corresponding to the video
#output_directory = f"{video_name}_frames"
output_directory = "D:/20240814-xd/28/images/front"
os.makedirs(output_directory, exist_ok=True)
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# Only extract frames at the desired frame rate
if frame_count % int(cap.get(5) / frame_rate) == 0:
output_file = f"{output_directory}/{frame_count}_front28.jpg"
cv2.imwrite(output_file, frame)
print(f"Frame {frame_count} has been extracted and saved as {output_file}")
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
video_file = r"D:/20240814-xd/28/video/front.mp4" # Replace with your video's name
extract_frames(video_file)
# 四班学生01同学的实验视频