-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeMovie.py
203 lines (147 loc) · 5.97 KB
/
MakeMovie.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import cv2
import cv
import colorsys
'''
STILL IN DEVELOPMENT
NOT YET A GENERAL PURPOSE SCRIPT!!!
NEEDS MORE WORK...
'''
import argparse
import sys
width = 800
height = 600
scale_font = 1
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
font_colour = (255,255,255)
def process_args():
"""
Parse command-line arguments.
"""
parser = argparse.ArgumentParser(description="A file for overlaying POVRay files generated from NeuroML by NeuroML1ToPOVRay.py with cell activity (e.g. as generated from a neuroConstruct simulation)")
parser.add_argument('prefix',
type=str,
metavar='<network prefix>',
help='Prefix for files in PovRay, e.g. use PREFIX is files are PREFIX.pov, PREFIX_net.inc, etc.')
parser.add_argument('-maxV',
type=float,
metavar='<maxV>',
default=50.0,
help='Max voltage for colour scale in mV')
parser.add_argument('-minV',
type=float,
metavar='<minV>',
default=-90.0,
help='Min voltage for colour scale in mV')
parser.add_argument('-startTime',
type=float,
metavar='<startTime>',
default=0,
help='Time in ms at which to start overlaying the simulation activity')
parser.add_argument('-endTime',
type=float,
metavar='<endTime>',
default=100,
help='End time of simulation activity in ms')
parser.add_argument('-title',
type=str,
metavar='<title>',
default='Movie generated from neuroConstruct simulation',
help='Title for movie')
parser.add_argument('-left',
type=str,
metavar='<left info>',
default='',
help='Text on left')
parser.add_argument('-frames',
type=int,
metavar='<frames>',
default=100,
help='Number of frames')
parser.add_argument('-name',
type=str,
metavar='<Movie name>',
default='output',
help='Movie name')
return parser.parse_args()
def generate_volt_scale(img, x, y, height, width, num):
for i in range(num):
ww = int(float(width)/num)
xx = int(x + i * ww)
fract = 1 - (float(i)/num + .5/num)
hue = (270 * (1-fract))/360
rgb = colorsys.hls_to_rgb(hue, 0.5, 1)
rgb = tuple([int(256*rr) for rr in rgb])
c1 = (xx,y)
c2 = (xx+ww,y+height)
#print "Fract: %f - hue: %f - RGB: %s - %s - %s"%(fract, hue, rgb, c1, c2)
cv2.rectangle(img,c1,c2,rgb,3)
def main (argv):
args = process_args()
print("Making a movie....")
img_files_pre = []
img_files_post = []
gen_images = True
gen_movie = False
#gen_images = False
gen_movie = True
pref = args.prefix+'_T00'
if gen_images:
for i in range(args.frames+1):
index = str(i)
while len(index)<3: index="0"+index
img_files_pre.append("%s%s.png"%(pref,index))
for i in range(len(img_files_pre)):
img_file = img_files_pre[i]
img = cv2.imread(img_file)
print("Read in file: %s"%img_file)
show = False
if show:
cv2.imshow('Image: '+img_file,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
t = args.startTime + i*float(args.endTime-args.startTime)/args.frames
cv2.putText(img,'Time: %.3fms'%t,(width-220,50), font, 1,font_colour,scale_font)
cv2.putText(img,'%imV : %imV'%(args.minV, args.maxV),(20,50), font, 1,font_colour,scale_font)
cv2.putText(img,args.title,(15,550), font, 1,font_colour,scale_font)
cv2.putText(img,args.left,(15,570), font, 1,font_colour,scale_font)
generate_volt_scale(img, 20, 65, 12, 200, 50)
new_file = args.name+'_'+img_file
cv2.imwrite(new_file,img)
print("Written %s"%new_file)
if gen_movie:
for i in range(args.frames+1):
index = str(i)
while len(index)<3: index="0"+index
img_files_post.append("%s_%s%s.png"%(args.name,pref,index))
imgs = []
for i in range(len(img_files_post)):
img_file = img_files_post[i]
img = cv2.imread(img_file)
print("Read in %s"%img_file)
imgs.append(img)
format = 'avi'
#format = 'mpg'
format = 'divx'
fps = 24
if format is 'avi':
fourcc = cv.CV_FOURCC('X','V','I','D')
mov_file = args.name+'.avi'
out = cv2.VideoWriter(mov_file,fourcc, fps, (width,height))
if format is 'divx':
fourcc = cv.CV_FOURCC('D','I','V','X')
mov_file = args.name+'.avi'
out = cv2.VideoWriter(mov_file,fourcc, fps, (width,height))
if format is 'mpg':
fourcc = cv.CV_FOURCC('M','J','P','G')
mov_file = args.name+'.mpg'
out = cv2.VideoWriter(mov_file,fourcc, fps, (width,height))
f = 0
for img in imgs:
print("Writing frame %i"%f)
f+=1
out.write(img)
out.release()
print("Saved movie file %s"%mov_file)
print "Done!"
if __name__ == '__main__':
main(sys.argv)