summaryrefslogtreecommitdiff
path: root/AutoVideosGPU.py
blob: 2719a9be43282ead1957ba1a8884a349f6bd35a1 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import GPU
import numpy as np
import random
import cv2
try:
    import Tkinter as tk
    import tkFileDialog as filedialog
except:
    import tkinter as tk
    from tkinter import filedialog
import namemaker3
import os

directory = os.path.realpath(__file__)

try:
    directory = directory[:directory.rindex('/')]
except:
    directory = directory[:directory.rindex('\\')]

single = ['cos', 'sin'] #Operations on a single number
binary = ['*', '+', '-'] #Operations for 2 numbers
numlist = ['x', 'y', 't', 'Constant']


def randFunction(length, singleweight, numberweight):
    hasx = False
    hasy = False
    hast = False
    while not(hasx and hasy and hast):
        #Types: b for binary, s for single, f for first, n for number
        function = ''
        lasttype = 'f'
        thistype = 0
        hasx = False
        hasy = False
        hast = False
        chanceend = 0
        length = 1 #Number of operations done so far
        while True:
            chanceend = (1.0 - (1.0 / length)) ** 12.0
            if lasttype == 'n':
                number = random.random()
                if number < chanceend:
                    break
                function = '(' + function + ')' + random.choice(binary)
                lasttype = 'b'
            elif lasttype == 's' or lasttype == 'b' or lasttype == 'f':
                function += '('
                thistype = random.random()
                if thistype < singleweight / (singleweight + numberweight):
                    function += random.choice(single)
                    lasttype = 's'
                else:
                    what = random.choice(numlist)
                    if what == 'Constant':
                        function += str(random.gauss(150, 50))
                    else:
                        function += what
                        if what == 'x':
                            hasx = True
                        elif what == 'y':
                            hasy = True
                        elif what == 't':
                            hast = True
                    lasttype = 'n'
                    function += ')'
            length += 1
    if function.count('(') > function.count(')'):
        function += ')' * (function.count('(') - function.count(')'))
    return function

def replace(s, substr, newsubstr):
    #Replaces the first instance of substr in s with newsubstr
    return s[:s.index(substr)] + newsubstr + s[s.index(substr)+len(substr):]

gpu = GPU.GPU()

clProgramTemplate = open('AutoVideosGPU.cl').read()

def createVideo(width, height, videoLength, frameRate, functionLength,
                singleWeight, numberWeight):

    rfunction = randFunction(functionLength, singleWeight, numberWeight)
    gfunction = randFunction(functionLength, singleWeight, numberWeight)
    bfunction = randFunction(functionLength, singleWeight, numberWeight)
    
    clProgramTemplate2 = replace(clProgramTemplate, '<WIDTH>', str(width))
    clProgramTemplate2 = replace(clProgramTemplate2, '<RFUNCTION>', rfunction)
    clProgramTemplate2 = replace(clProgramTemplate2, '<GFUNCTION>', gfunction)
    clProgramTemplate2 = replace(clProgramTemplate2, '<BFUNCTION>', bfunction)

    fourcc = cv2.cv.CV_FOURCC(*'XVID')
    name = ' '.join([namemaker3.generate() for i in range(3)]) + '.avi'
    out = cv2.VideoWriter(directory+'/'+name,fourcc, frameRate, (width,height))

    
    for t in range(int(videoLength*frameRate)):
        clProgram = replace(clProgramTemplate2, '<FRAMENUMBER>', str(t))
        gpu.readFromString(clProgram)

        globalSize = (width*height*3,)

        output = np.zeros(globalSize, dtype=np.float32)
        
        gpu.setup([], output)

        output = gpu.run('AutoFrame', globalSize) % 255

        output.resize(height, width, 3, refcheck=False)

        output = output.astype(np.uint8)

        out.write(output[:, :, ::-1])
        
    out.release()

def createVideos():
    
    global goToMenuButton, finishedLabel
    try:
        width = int(widthVar.get())
        height = int(heightVar.get())
        length = float(lengthVar.get())
        frameRate = float(frameRateVar.get())
        functionLength = float(functionLengthVar.get())
        singleWeight = float(singleWeightVar.get())
        numberWeight = float(numberWeightVar.get())
        numVideos = int(numVideosVar.get())
    except:
        errorLabel.config(text='Error: All settings must be numbers')
        return

    f = open('AutoVideosSettings.txt', 'w')
    f.write(' '.join([str(width), str(height), str(length), str(frameRate),
                      str(functionLength), str(singleWeight), str(numberWeight),
                      str(numVideos), directory]))
    f.close()
    try:
        menu.destroy()
    except:
        pass
    loadingLabel = tk.Label(root, text='Creating videos...')
    loadingLabel.grid(row=2, column=0)

    
    for i in range(numVideos):
        f = open('videoNumber.txt', 'w')
        f.write(str(i))
        f.close()
        loadingLabel.config(text='Creating videos... ' + str(i)\
                            + '/' + str(numVideos), font=('Helvetica', 16))

        root.update()

        createVideo(width, height, length, frameRate,
                    functionLength, singleWeight, numberWeight)


    loadingLabel.destroy()
    
    finishedLabel = tk.Label(root, text='AutoVideos has finished creating your\
 videos.')
    finishedLabel.grid(row=2, column=0)

    goToMenuButton = tk.Button(root, text='Go back to menu', command=goToMenu)
    goToMenuButton.grid(row=3, column=0)

   

def chooseDir():
    global directory
    directory = filedialog.askdirectory(title=\
                                        'Choose a directory for your videos')
    directoryLabel.config(text=directory)
    

def goToMenu():
    global menu, errorLabel, directoryLabel

    try:
        goToMenuButton.destroy()
        finishedLabel.destroy()
    except:
        pass
    
    menu = tk.Frame(root)
    menu.grid(row=1, column=0)


    tk.Label(menu, text='Width: ').grid(row=0, column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=widthVar).grid(row=0, column=1)

    tk.Label(menu, text='Height: ').grid(row=1, column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=heightVar).grid(row=1, column=1)

    tk.Label(menu, text='Length: ').grid(row=2, column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=lengthVar).grid(row=2, column=1)

    tk.Label(menu, text='Frame rate: ').grid(row=3, column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=frameRateVar).grid(row=3, column=1)

    tk.Label(menu, text='Function length: ').grid(row=4, column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=functionLengthVar).grid(row=4, column=1)

    tk.Label(menu, text='Single operation weight: ').grid(row=5,
                                                          column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=singleWeightVar).grid(row=5, column=1)

    tk.Label(menu, text='Number weight: ').grid(row=6, column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=numberWeightVar).grid(row=6, column=1)

    tk.Label(menu, text='Number of videos: ').grid(row=7, column=0, sticky=tk.E)
    tk.Entry(menu, textvariable=numVideosVar).grid(row=7, column=1)


    tk.Button(menu, text='Choose Directory',
              command=chooseDir).grid(row=8, column=0, sticky=tk.E)
    directoryLabel = tk.Label(menu, text=directory)
    directoryLabel.grid(row=8, column=1, sticky=tk.W)
    
    tk.Button(menu, text='Create', command=createVideos).grid(row=9, column=0)

    errorLabel = tk.Label(menu, text='', fg='red')
    errorLabel.grid(row=10, column=0)

root = tk.Tk()
root.title('AutoVideos')
root.geometry('800x500+0+0')



if __name__ == '__main__':
    widthVar = tk.StringVar(root, '1280')
    heightVar = tk.StringVar(root, '720')
    lengthVar = tk.StringVar(root, '3')
    frameRateVar = tk.StringVar(root, '24')
    functionLengthVar = tk.StringVar(root, '80')
    singleWeightVar = tk.StringVar(root, '1')
    numberWeightVar = tk.StringVar(root, '1')
    numVideosVar = tk.StringVar(root, '1')

    tk.Label(root, text='AutoVideos',
             font=('Helvetica', 24)).grid(row=0, column=0) #Title label

    goToMenu()

    root.mainloop()

else:
    w, h, l, fr, fl, s, number, nVideos, directory = \
       open('AutoVideosSettings.txt').read().strip('\n').split(' ')

    

    numVideos = int(nVideos) - int(open('videoNumber.txt').read().strip('\n'))

    widthVar = tk.StringVar(root, w)
    heightVar = tk.StringVar(root, h)
    lengthVar = tk.StringVar(root, l)
    frameRateVar = tk.StringVar(root, fr)
    functionLengthVar = tk.StringVar(root, fl)
    singleWeightVar = tk.StringVar(root, s)
    numberWeightVar = tk.StringVar(root, number)
    numVideosVar = tk.StringVar(root, str(numVideos))

    tk.Label(root, text='AutoVideos',
             font=('Helvetica', 24)).grid(row=0, column=0) #Title label

    root.after(1000, createVideos)
    root.mainloop()