-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideoData.py
344 lines (253 loc) · 9.84 KB
/
videoData.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'''
Description: Read and index video data.
#----------------------------------------------------------------------------------------------------------------#
Class functions:
#----------------------------------------------------------------------------------------------------------------#
Notes:
'''
#----------------------------------------------------------------------------------------------------------------#
# Importing dependancies:
import numpy as np
import math
import sys
class videoData:
def __init__(self, FILE_NAME, HEIGHT, WIDTH, CHANNELS):
print('\033[1;33m[Status]==> Loading video file\033[0m')
self.fileName = FILE_NAME
self.iteratorIndex = 0
self.width = WIDTH
self.height = HEIGHT
self.channels = CHANNELS
self.patch = 0
self.decompressor = None
# Load from file if FILE_NAME is mentioned
if(self.fileName):
self.videoFrames = np.fromfile(FILE_NAME, dtype ='uint8')
self.totalFrames = int(len(self.videoFrames)/(WIDTH*HEIGHT*CHANNELS))
self.blockLabels = np.zeros((int(self.totalFrames),int(math.ceil(self.height/8.0)),int(math.ceil(self.width/8.0))))
self.videoFrames = self.videoFrames.reshape((self.totalFrames, self.channels, self.height, self.width))
#self.videoFrames = np.transpose(self.videoFrames,(0,2,3,1))
else:
self.videoFrames = None
self.totalFrames = None
self.blockLabels = None
self.videoFrames_orig = None
#----------------------------------------------------------------------------------------------------------------#
# create instance from array:
@classmethod
def fromArray(cls,videoArray,HEIGHT,WIDTH,CHANNELS):
instance = cls(None,HEIGHT,WIDTH,CHANNELS)
instance.videoFrames = videoArray
instance.videoFrames_orig = videoArray.copy()
instance.totalFrames = np.size(videoArray)/(WIDTH*HEIGHT*CHANNELS)
instance.blockLabels = np.zeros((int(instance.totalFrames),int(math.ceil(HEIGHT/8.0)),int(math.ceil(WIDTH/8.0))))
return instance
#----------------------------------------------------------------------------------------------------------------#
# Metadata:
def writeMetaData(self):
metaData = str(int(self.width))+'\n'
metaData += str(int(self.height))+'\n'
metaData += str(int(self.channels))+'\n'
metaData += str(int(self.totalFrames))+'\n'
metaData += str(30)
metaFile = open('MetaData.txt','w')
metaFile.write(metaData)
metaFile.close()
#----------------------------------------------------------------------------------------------------------------#
# Getters:
def getNumChannels(self):
return self.channels
def getHeight(self):
return self.height
def getWidth(self):
return self.width
def getNumBlocks(self,blockSize):
noOfBlocks = math.ceil(1.0*self.width/blockSize) * math.ceil(1.0*self.height/blockSize)
return int(noOfBlocks)
def getLabel(self, frameNumber, i, j):
return self.blockLabels[int(frameNumber), int(i), int(j)]
'''
def getFrame(self,frameNumber):
# Check frameNumbers:
if(self.iteratorIndex <0):
self.iteratorIndex = self.totalFrames -1
frameNumber = self.iteratorIndex
if(self.iteratorIndex >= self.totalFrames):
self.iteratorIndex = 0
frameNumber = 0
if(frameNumber >= self.totalFrames):
frameNumber = self.totalFrames - 1
if(frameNumber < 0):
frameNumber = 0
# Pre allocate frame memory
frame = np.empty((self.height,self.width, self.channels),'uint8')
frameOffset = frameNumber*self.height*self.width*self.channels
# Loop through all channels
for c in range(self.channels):
channelOffset = self.height*self.width*c
startIndex = frameOffset + channelOffset
endIndex = startIndex + self.height*self.width
frame[:,:,c] = np.copy((self.videoFrames[startIndex:endIndex]).reshape((self.height,self.width)))
return frame
'''
def getFrame(self,frameNumber):
# Check frameNumbers:
if(self.iteratorIndex <0):
self.iteratorIndex = self.totalFrames -1
frameNumber = self.iteratorIndex
if(self.iteratorIndex >= self.totalFrames):
self.iteratorIndex = 0
frameNumber = 0
if(frameNumber >= self.totalFrames):
frameNumber = self.totalFrames - 1
if(frameNumber < 0):
frameNumber = 0
return self.videoFrames[frameNumber, :,:,:]
#----------------------------------------------------------------------------------------------------------------#
# Get values in given Frame:
def getBlock(self, frameNumber, i, j, block_size):
# Perform checks:
if(frameNumber>=self.totalFrames):
frameNumber =0
i_low = i
i_high = i+block_size
j_low = j
j_high = j+block_size
if(i<0):
i_low=0
i_high = block_size+i
if(j<0):
j_low=0
j_high = block_size+j
return self.videoFrames[frameNumber, :, i_low:i_high, j_low:j_high]
#----------------------------------------------------------------------------------------------------------------#
# Set values in given Frame:
def setBlock(self,frameNumber,i,j,block_size,blockValues = 0):
# Perform checks:
if(frameNumber>=self.totalFrames):
frameNumber =0
i_low = i
i_high = i+block_size
j_low = j
j_high = j+block_size
if(i<0):
i_low=0
i_high = block_size+i
if(j<0):
j_low=0
j_high = block_size+j
#print(np.shape(blockValues),np.shape(self.videoFrames[frameNumber, :, i_low:i_high, j_low:j_high]))
self.videoFrames[frameNumber, :, i_low:i_high, j_low:j_high] = blockValues
#----------------------------------------------------------------------------------------------------------------#
# Requantize Frame:
def reQuantize(self,frameNumber,i,j,block_size):
# Perform checks:
if(frameNumber>=self.totalFrames):
frameNumber =0
'''
# Compute i and j for top right corner;
topLeft = [i-block_size//2,j-block_size//2]
if(topLeft[0] <0):
topLeft[0] = 0
if(topLeft[0] >=self.height):
topLeft[0] = 528
if(topLeft[1] <0):
topLeft[1] = 0
if(topLeft[1] >=self.width):
topLeft[1] = 952
# Compute i and j for top right corner;
bottomRight = [i+block_size//2,j+block_size//2]
if(bottomRight[0] <0):
bottomRight[0] = 0
if(bottomRight[0] >=self.height):
bottomRight[0] = 528
if(bottomRight[1] <0):
bottomRight[1] = 0
if(bottomRight[1] >=self.width):
bottomRight[1] = 952
# Get image Patch:
patch = np.empty((1,3,8,8),np.uint8)
i=topLeft[0]
j=topLeft[1]
totalRows = (bottomRight[0]-topLeft[0])//8+1
totalCols = (bottomRight[1]-topLeft[1])//8+1
while(i<bottomRight[0]):
while(j<bottomRight[1]):
blocks = ((i//8)*120+(j//8)+frameNumber*8160)
#patch = self.decompressor.computeIDCT(blocks)
patch1 = np.zeros((1,3,8,8),dtype=np.uint8)
patch = np.append(patch,patch1,0)
j+=8
i+=8
# Get block numbers:
tempPatch = np.empty((3,8,8))
first= True
for h_offset in range(i-block_size//2,i+block_size//2,8):
for w_offset in range(i-block_size//2,i+block_size//2,8):
if(h_offset<0 or w_offset<0):
continue
tempPatch = np.append(tempPatch,np.zeros((3,8,8)),2)
patch = np.append(patch,tempPatch,1)
print(np.shape(patch))
#print(totalRows,totalCols)
#patch = patch.reshape(3,-(topLeft[0]-bottomRight[0])+8,-(topLeft[1]-bottomRight[1]+8))
#print(3,(topLeft[0]-bottomRight[0])//8,(topLeft[1]-bottomRight[1])//8)
'''
# Get block numbers:
blockList = []
temp = []
for h_offset in range(i-block_size//2,i+block_size//2,8):
for w_offset in range(j-block_size//2,j+block_size//2,8):
if(h_offset<0 or w_offset<0 or h_offset>=self.height or w_offset>=self.width):
continue
blocks = ((h_offset//8)*120+(w_offset//8)+frameNumber*8160)
temp.append(blocks)
if(not len(temp)==0):
blockList.append(temp)
temp = []
# Set patch:
self.patch = self.getBlock(frameNumber,i-block_size//2,j-block_size//2,block_size).copy()
patch = np.zeros((3,np.shape(self.patch)[1],np.shape(self.patch)[2]))
for index1,rows in enumerate(blockList):
for index2,columns in enumerate(rows):
if(not np.shape(patch[:,index1*8:8*(index1+1),index2*8:8*(index2+1)])==(3,8,8)):
#print(index1,index2,np.shape(patch[:,index1*8:8*(index1+1),index2*8:8*(index2+1)]),np.shape(patch))
patch[:,index1*8-4:8*(index1+1),index2*8:8*(index2+1)] = self.decompressor.computeIDCT(columns)
continue
patch[:,index1*8:8*(index1+1),index2*8:8*(index2+1)] = self.decompressor.computeIDCT(columns)
#patch[:,index1*8:8*(index1+1),index2*8:8*(index2+1)] = np.zeros((3,8,8))
self.setBlock(frameNumber,i-block_size//2,j-block_size//2,block_size,patch)
#----------------------------------------------------------------------------------------------------------------#
# Repatch:
def repatch(self,frameNumber,i,j,block_size):
# Perform checks:
if(frameNumber>=self.totalFrames):
frameNumber =0
self.setBlock(frameNumber,i-block_size//2,j-block_size//2,block_size,self.patch)
#----------------------------------------------------------------------------------------------------------------#
# Get current Frame:
def currentFrame(self):
return(self.getFrame(self.iteratorIndex))
#----------------------------------------------------------------------------------------------------------------#
# Get next Frame:
def nextFrame(self):
self.iteratorIndex +=1
return(self.getFrame(self.iteratorIndex))
#----------------------------------------------------------------------------------------------------------------#
# Get previous Frame:
def prevFrame(self):
self.iteratorIndex -=1
return(self.getFrame(self.iteratorIndex))
#----------------------------------------------------------------------------------------------------------------#
# Iterate over frames:
def iterator(self):
while(True):
yield(self.getFrame(self.iteratorIndex))
self.iteratorIndex+=1
#----------------------------------------------------------------------------------------------------------------#
# Boiler-plate syntax for testing only:
if __name__ =='__main__':
print('Started')
a = videoData('oneperson_960_540.rgb',540,960,3)
b = videoData.fromArray(a.videoFrames,540,960,3)
print(np.shape(np.transpose(a.currentFrame(),(1,2,0))))