-
Notifications
You must be signed in to change notification settings - Fork 0
/
video2frame.py
63 lines (47 loc) · 1.92 KB
/
video2frame.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
import os
import cv2
def main():
videos_src_path = '../DeepStab/stable'
videos_save_path = './data_video/stable'
videos = os.listdir(videos_src_path)
videos = filter(lambda x: x.endswith('avi'), videos)
for each_video in videos:
print(each_video)
# get the name of each video, and make the directory to save frames
each_video_name, _ = each_video.split('.')
os.mkdir(videos_save_path + '/' + each_video_name)
each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/'
# get the full path of each video, which will open the video tp extract frames
each_video_full_path = os.path.join(videos_src_path, each_video)
cap = cv2.VideoCapture(each_video_full_path)
frame_count = 1
success = True
while success:
success, frame = cap.read()
if not success:
print('Read a new frame: ', success, frame_count)
params = []
params.append(cv2.IMWRITE_PXM_BINARY)
params.append(1)
cv2.imwrite(each_video_save_full_path + "%d.jpg" % frame_count, frame, params)
frame_count = frame_count + 1
cap.release()
def rename_():
basedir = "./data/train/unstable"
for root, subs, files in os.walk(basedir):
# print("root:", root, files, subs, len(subs))
for sub in subs:
path = os.path.join(basedir, sub)
prefix_len = len(sub)
# print("path:", path, len(sub))
for file in os.listdir(path):
if "txt" in file:
# print("file:", file)
src = os.path.join(path, file)
dst = os.path.join(path, file[prefix_len:])
print("src:", src, "---------dst:", dst)
os.rename(src, dst)
else:
continue
if __name__ == '__main__':
main()