This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
get_frames_resize.py
executable file
·176 lines (134 loc) · 4.97 KB
/
get_frames_resize.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# coding=utf-8
"""
given a list of videos, get all the frames and resize note that the
frames are 0-indexed
"""
import argparse
import cv2
import os
import pickle
import sys
from tqdm import tqdm
parser = argparse.ArgumentParser()
parser.add_argument("videolist")
parser.add_argument("despath")
parser.add_argument("--resize", default=False, action="store_true")
parser.add_argument("--size", default=800, type=int)
parser.add_argument("--maxsize", default=1333, type=int)
parser.add_argument("--job", type=int, default=1, help="total job")
parser.add_argument("--curJob", type=int, default=1,
help="this script run job Num")
parser.add_argument("--statspath", default=None,
help="path to write videoname.p to save some stats for "
"that video")
parser.add_argument("--use_2level", action="store_true",
help="make videoname/frames dir")
parser.add_argument("--name_level", type=int, default=None,
help="add the top level folder name to the videoname")
parser.add_argument("--cv2path", default=None)
parser.add_argument("--use_moviepy", action="store_true")
parser.add_argument("--use_lijun", action="store_true")
def get_new_hw(h, w, size, max_size):
"""Get new hw."""
scale = size * 1.0 / min(h, w)
if h < w:
newh, neww = size, scale * w
else:
newh, neww = scale * h, size
if max(newh, neww) > max_size:
scale = max_size * 1.0 / max(newh, neww)
newh = newh * scale
neww = neww * scale
neww = int(neww + 0.5)
newh = int(newh + 0.5)
return neww, newh
if __name__ == "__main__":
args = parser.parse_args()
if args.cv2path is not None:
sys.path = [args.cv2path] + sys.path
if args.use_moviepy:
from moviepy.editor import VideoFileClip
elif args.use_lijun:
from diva_io.video import VideoReader
# still need this to write image
print("using opencv version:%s"%(cv2.__version__))
if not os.path.exists(args.despath):
os.makedirs(args.despath)
if args.statspath is not None and not os.path.exists(args.statspath):
os.makedirs(args.statspath)
count = 0
for line in tqdm(open(args.videolist, "r").readlines()):
count += 1
if (count % args.job) != (args.curJob-1):
continue
video = line.strip()
stats = {"h":None, "w":None, "fps":None, "frame_count":None,
"actual_frame_count":None}
videoname = os.path.splitext(os.path.basename(video))[0]
targetpath = args.despath
if args.use_2level:
targetpath = os.path.join(args.despath, videoname)
if not os.path.exists(targetpath):
os.makedirs(targetpath)
if args.name_level is not None:
foldernames = video.split("/")
prefixes = foldernames[-1-args.name_level:-1]
videoname = "__".join(prefixes + [videoname])
if args.use_moviepy:
vcap = VideoFileClip(video, audio=False)
frame_count = int(vcap.fps * vcap.duration) # uh
vcap_iter = vcap.iter_frames()
elif args.use_lijun:
vcap = VideoReader(video)
frame_count = int(vcap.length)
else:
try:
vcap = cv2.VideoCapture(video)
if not vcap.isOpened():
raise Exception("cannot open %s"%video)
except Exception as e:
raise e
if cv2.__version__.split(".") != "2":
frame_width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = vcap.get(cv2.CAP_PROP_FPS)
frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
else:
frame_width = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
frame_height = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
fps = vcap.get(cv2.cv.CV_CAP_PROP_FPS)
frame_count = vcap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
stats['h'] = frame_height
stats['w'] = frame_width
stats['fps'] = fps
stats['frame_count'] = frame_count
cur_frame = 0
count_actual = 0
while cur_frame < frame_count:
if args.use_moviepy:
suc = True
frame = next(vcap_iter)
else:
suc, frame = vcap.read()
if not suc:
cur_frame += 1
tqdm.write("warning, %s frame of %s failed" % (cur_frame, videoname))
continue
count_actual += 1
if args.use_moviepy:
# moviepy ask ffmpeg to get rgb24
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
frame = frame.astype("float32")
if args.resize:
neww, newh = get_new_hw(frame.shape[0],
frame.shape[1], args.size, args.maxsize)
frame = cv2.resize(frame, (neww, newh), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(os.path.join(targetpath,
"%s_F_%08d.jpg" % (videoname, cur_frame)), frame)
cur_frame += 1
stats['actual_frame_count'] = count_actual
if args.statspath is not None:
with open(os.path.join(args.statspath, "%s.p" % videoname), "wb") as fs:
pickle.dump(stats, fs)
if not args.use_moviepy and not args.use_lijun:
vcap.release()