forked from boysugi20/python-image-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoToImage.py
67 lines (52 loc) · 1.9 KB
/
videoToImage.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
'''
This script convert an input video to frames / a list of images
'''
import os
import cv2
import numpy as np
def video_to_images(video_path, output_folder):
# Check if the video file exists
if not os.path.isfile(video_path):
print(f"Error: Video file '{video_path}' not found.")
return
# Create the output folder if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Open the video file
video = cv2.VideoCapture(video_path)
# Check if the video was opened successfully
if not video.isOpened():
print(f"Error: Unable to open video file '{video_path}'.")
return
frame_count = 0
saved_count = 0
prev_frame = None # To store the previous frame for comparison
while True:
# Read a frame from the video
ret, frame = video.read()
# If no frame is read, we reached the end of the video
if not ret:
break
# Check if the current frame is identical to the previous frame
if prev_frame is not None and np.array_equal(prev_frame, frame):
frame_count += 1
continue # Skip saving this frame
# Generate the file name for the frame image
frame_filename = os.path.join(output_folder, f"frame_{frame_count:04d}.png")
# Save the frame as an image
cv2.imwrite(frame_filename, frame)
saved_count += 1
# Update the previous frame
prev_frame = frame
frame_count += 1
# Release the video capture object
video.release()
print(f"Exported {saved_count} unique frames to '{output_folder}'.")
if __name__ == "__main__":
# Input MP4 video file
#video_path = input("Enter the path to the MP4 video file: ")
video_path = 'canada.mp4'
# Output folder
output_folder = "test-ExportedImages"
# Extract frames
video_to_images(video_path, output_folder)