-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideo2img.py
executable file
·62 lines (44 loc) · 1.59 KB
/
video2img.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
# https://note.nkmk.me/python-opencv-video-to-still-image/
import os
import cv2
import glob
from tqdm import tqdm
PWD = os.getcwd() + "/"
def save_frame_range(video_path, step_frame, cnt,
dir_path, basename, ext='jpg'):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return
os.makedirs(dir_path, exist_ok=True)
base_path = os.path.join(dir_path, basename)
digit = len(str(int(cap.get(cv2.CAP_PROP_FRAME_COUNT))))
stop_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
pbar = tqdm(total=int((stop_frame/step_frame)))
pbar.set_description("{}:{}".format(cnt, os.path.basename(video_path)))
for n in range(0, stop_frame, step_frame):
cap.set(cv2.CAP_PROP_POS_FRAMES, n)
ret, frame = cap.read()
if ret:
cv2.imwrite('{}_{}.{}'.format(base_path, str(n).zfill(digit), ext), frame)
n += 1
else:
return
pbar.update(1)
pbar.close()
foldername = 'demo'
files=glob.glob(PWD + "videos/" + foldername + "/*")
total = 0
cnt = 1
print('--directories to be processed--')
for fname in files:
print(os.path.basename(fname))
total+=1
print('-----------------------------')
print('-----Total:{} directories-----'.format(total))
for fname in files:
vname = os.path.basename(fname)
prefix = vname.split(".")
save_frame_range('./videos/' + foldername + '/' + vname, #input video
60, cnt, # frame interval, counting videos
'./' + foldername + '_image/' + vname, prefix[0]) #output directory and output images' prefix
cnt+=1