forked from 12dmodel/deep_motion_mag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_grayscale.py
65 lines (42 loc) · 1.33 KB
/
convert_grayscale.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
import cv2
import os
# determine what OpenCV version we are using
try:
import cv2.cv as cv
USE_CV2 = True
except ImportError:
# OpenCV 3.x does not have cv2.cv submodule
USE_CV2 = False
def frame_to_grayscale(frame):
'''
Convert current frame color to grayscale
'''
# convert to gray image
grayIm = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
return grayIm
################# main script
def main():
# path to folder with frames to convert
#video = 'guitar'
video = 'juguete-turbio'
path = 'data/vids/'+video+'/'
# path to folder with grayscale frames
output_path = 'data/vids/'+video+'/gs/'
# Check whether the specified path exists or not
isExist = os.path.exists(output_path)
if not isExist:
# Create a new directory because it does not exist
os.makedirs(output_path)
print("Grayscale frames will be stored in "+output_path)
count = 0
namelist = sorted(os.listdir(path))
for filename in namelist:
if filename.endswith(".png"):
count += 1
frame = cv2.imread(path+filename)
if frame is not None:
gs_frame = frame_to_grayscale(frame)
name = output_path + '%04d.png' % (count)
cv2.imwrite(name,gs_frame)
if __name__ == "__main__":
main()