-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocess.py
executable file
·50 lines (38 loc) · 1.38 KB
/
preprocess.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
import cv2
import configure as cfg
import os
def create_dirs():
if not os.path.exists(cfg.DIR_DATA): os.makedirs(cfg.DIR_DATA)
for category in cfg.CATEGORIES:
pth = os.path.join(cfg.DIR_DATA, category)
if not os.path.exists(pth): os.makedirs(pth)
return
def preprocess():
lst = os.listdir(cfg.DIR_DATA_RAW)
newsize = (cfg.IMG_RAW_W, cfg.IMG_RAW_H)
for filename in lst:
print filename
# parse file name
instance = filename.replace('.avi','')
category = instance[instance.index('_')+1:]
category = category[:category.index('_')]
# create new directory if needed
pth = os.path.join(cfg.DIR_DATA, category, instance)
if not os.path.exists(pth): os.makedirs(pth)
# read video and extract frame
cap = cv2.VideoCapture(os.path.join(cfg.DIR_DATA_RAW, filename))
frameid = 0
while cap.isOpened():
# read new frame
ret, frame = cap.read()
if ret == False: break # end of video
# resize and write frame
frame = cv2.resize(frame, newsize, interpolation=cv2.INTER_CUBIC)
frame_file = os.path.join(pth, cfg.IMAGE_FORMAT.format(frameid+1))
cv2.imwrite(frame_file, frame)
frameid += 1
cap.release()
return
if __name__ == '__main__':
create_dirs()
preprocess()