-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4ToFrames.py
98 lines (80 loc) · 3.39 KB
/
mp4ToFrames.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import sys
import cv2
import numpy as np
import os
from os import path
import argparse
parser = argparse.ArgumentParser(description=
"Converts video files to video frames.")
parser.add_argument("videos", help="Path to folder containing the video files.")
parser.add_argument("output_folder", help="Path to output folder.")
parser.add_argument("--frame_skip", type=int, default=1, help="Number of frames to skip.")
args = parser.parse_args()
# Will resize the frames to this resolution
NET_INPUT_WIDTH = 320
NET_INPUT_HEIGHT = 240
trainingVideosPath = path.join(args.videos, "training")
validationVideosPath = path.join(args.videos, "validation")
outputRoot = args.output_folder
outputTraining = path.join(outputRoot, "training")
outputValidation = path.join(outputRoot, "validation")
if not os.path.exists(trainingVideosPath):
print(f"Couldn't find path `{trainingVideosPath}` .")
sys.exit(1)
if not os.path.exists(validationVideosPath):
print(f"Couldn't find path `{validationVideosPath}` .")
sys.exit(1)
if not os.path.exists(outputRoot):
os.mkdir(outputRoot)
if not os.path.exists(outputTraining):
os.mkdir(outputTraining)
if not os.path.exists(outputValidation):
os.mkdir(outputValidation)
for folder_name in os.listdir(trainingVideosPath):
currReadFolder = path.join(trainingVideosPath, folder_name)
currOutputFolder = path.join(outputTraining, folder_name)
if not os.path.exists(currOutputFolder):
os.mkdir(currOutputFolder)
for video_fn in os.listdir(currReadFolder):
if not video_fn.endswith(".avi"):
print(f"Skipping `{video_fn}`, doesn't end with .avi")
continue
print(f"Processing `{video_fn}`.")
name = video_fn[:-4]
# Open video capture
videoCap = cv2.VideoCapture(path.join(currReadFolder, video_fn))
frameCount = 0
# Read first frame pre-loop
ret, frame = videoCap.read()
while frame is not None:
output_fn = path.join(currOutputFolder, f"{name}_{frameCount}.jpg")
if frameCount % args.frame_skip == 0:
frame = cv2.resize(frame, (NET_INPUT_WIDTH, NET_INPUT_HEIGHT))
cv2.imwrite(output_fn, frame)
frameCount += 1
ret, frame = videoCap.read()
videoCap.release()
for folder_name in os.listdir(validationVideosPath):
currReadFolder = path.join(validationVideosPath, folder_name)
currOutputFolder = path.join(outputValidation, folder_name)
if not os.path.exists(currOutputFolder):
os.mkdir(currOutputFolder)
for video_fn in os.listdir(currReadFolder):
if not video_fn.endswith(".avi"):
print(f"Skipping `{video_fn}`, doesn't end with .avi")
continue
print(f"Processing `{video_fn}`.")
name = video_fn[:-4]
# Open video capture
videoCap = cv2.VideoCapture(path.join(currReadFolder, video_fn))
frameCount = 0
# Read first frame pre-loop
ret, frame = videoCap.read()
while frame is not None:
output_fn = path.join(currOutputFolder, f"{name}_{frameCount}.jpg")
if frameCount % args.frame_skip == 0:
frame = cv2.resize(frame, (NET_INPUT_WIDTH, NET_INPUT_HEIGHT))
cv2.imwrite(output_fn, frame)
frameCount += 1
ret, frame = videoCap.read()
videoCap.release()