-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideo_analyser.py
325 lines (257 loc) · 12.9 KB
/
video_analyser.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
# Given a filename, this class will return a series of timestamps representing clips
# which it considers to be relevant. These clips would be good to be made into a compelation
import tensorflow as tf
from keras.models import load_model
import cv2
import numpy as np
import logging
import time
# when training the model, the input data was padded to always be 16 pixels wide.
# this was larger than the largest digits and made the frame square
STANDARDIZED_DIGIT_WIDTH = 16
# the model predicts with a minimum confidence of ~.95 when given a valid digit
# anything below this threshold is random fuzz or fluff and should be ignored
THRESHOLD_DIGIT_PREDICTION_CONFIDENCE = .75
MODEL_FILE_NAME = 'model1.h5'
# when checking a scorecard for digits, we break the scorecard into 30 columns. This
# speeds up the process of scanning frames
NUMBER_OF_SLICES = 30
FRAMES_PER_SECOND = 60
# we want to parse fewer frames at the start, since we know theres no score for the first
# ~11 min of game
PRE_DRAFT_LARGE_INTERVAL = 343
PRE_DRAFT_MEDIUM_INTERVAL = 49
PRE_DRAFT_SMALL_INTERVAL = 7
PRE_DRAFT_TINY_INTERVAL = 1
# Once we get into the game, it helps having a smaller step size
POST_DRAFT_LARGE_INTERVAL = 27
POST_DRAFT_MEDIUM_INTERVAL = 9
POST_DRAFT_SMALL_INTERVAL = 3
POST_DRAFT_TINY_INTERVAL = 1
# This stores the current frame interval of the pre and post intervals
LARGEST_FRAME_INTERVAL = PRE_DRAFT_LARGE_INTERVAL * FRAMES_PER_SECOND
MIDDLE_FRAME_INTERVAL = PRE_DRAFT_MEDIUM_INTERVAL * FRAMES_PER_SECOND
SMALL_FRAME_INTERVAL = PRE_DRAFT_SMALL_INTERVAL * FRAMES_PER_SECOND
TINY_FRAME_INTERVAL = PRE_DRAFT_TINY_INTERVAL * FRAMES_PER_SECOND
LARGE_STEP = 3
MEDIUM_STEP = 2
SMALL_STEP = 1
TINY_STEP = 0
class VideoAnalyser:
def __init__(self):
self.digitModel = load_model(MODEL_FILE_NAME)
logging.basicConfig(filename='logs/analysis.log', filemode='w', format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
logging.getLogger().setLevel(logging.INFO)
def findClips(self, filename):
start = time.time()
rawClips = []
killsPerClip = []
# start from the videos beginning
frameCount = 0
lastKillCount = -1
stepSize = LARGE_STEP
numberOfFramesCheckedBeforeDraft = 0
numberOfFramesCheckedAfterDraft = 0
LARGEST_FRAME_INTERVAL = PRE_DRAFT_LARGE_INTERVAL * FRAMES_PER_SECOND
MIDDLE_FRAME_INTERVAL = PRE_DRAFT_MEDIUM_INTERVAL * FRAMES_PER_SECOND
SMALL_FRAME_INTERVAL = PRE_DRAFT_SMALL_INTERVAL * FRAMES_PER_SECOND
TINY_FRAME_INTERVAL = PRE_DRAFT_TINY_INTERVAL * FRAMES_PER_SECOND
drafting = True
# itterate through the video first at the largest interval,
# if a kill occured, backtrack and continue again at the medium interval
# if a kill occured, backtrack and continue again at the samalled interval until the kill is found
cap = cv2.VideoCapture(filename)
while cap.isOpened():
if(drafting):
numberOfFramesCheckedBeforeDraft += 1
else:
numberOfFramesCheckedAfterDraft += 1
cap.set(cv2.CAP_PROP_POS_FRAMES, frameCount)
ret, frame = cap.read()
scores = self.getScoreFromFrame(frame)
currentScore = lastKillCount
if scores != None:
currentScore = scores[0] + scores[1]
# no score and not drafting means we hit end of game
elif (not drafting):
# go backwards till we hit end of game
if(stepSize is LARGE_STEP):
frameCount -= LARGEST_FRAME_INTERVAL
stepSize = MEDIUM_STEP
elif(stepSize is MEDIUM_STEP):
frameCount -= MIDDLE_FRAME_INTERVAL
stepSize = SMALL_STEP
elif(stepSize is SMALL_STEP):
frameCount -= SMALL_FRAME_INTERVAL
stepSize = TINY_STEP
# means we just hit the end of the game. we can break loop
else:
currentTime = int(frameCount / FRAMES_PER_SECOND)
rawClips.append([currentTime - 20, currentTime])
killsPerClip.append(currentScore - lastKillCount)
print('GAME JUST ENDED: Time - ' + str(int(frameCount / 60 / 60)) + ':' + str(int((frameCount/60)%60)))
break
# if no score was found or it remained the same, keep looking at the current step sie
if(currentScore <= lastKillCount):
if(stepSize is LARGE_STEP):
frameCount += LARGEST_FRAME_INTERVAL
elif(stepSize is MEDIUM_STEP):
frameCount += MIDDLE_FRAME_INTERVAL
elif(stepSize is SMALL_STEP):
frameCount += SMALL_FRAME_INTERVAL
else:
frameCount += TINY_FRAME_INTERVAL
continue
# if a score was found, back up and downgrade step sizes
else:
if(stepSize is LARGE_STEP):
frameCount -= LARGEST_FRAME_INTERVAL
stepSize = MEDIUM_STEP
elif(stepSize is MEDIUM_STEP):
frameCount -= MIDDLE_FRAME_INTERVAL
stepSize = SMALL_STEP
elif(stepSize is SMALL_STEP):
frameCount -= SMALL_FRAME_INTERVAL
stepSize = TINY_STEP
# means a kill happened at this time
else:
# The exact second of the start of the game
currentTime = int(frameCount / FRAMES_PER_SECOND)
if(drafting):
rawClips.append([max(0, currentTime - 25), max(0, currentTime - 1)])
killsPerClip.append(0)
# once the game starts, we want to take smaller steps through video
LARGEST_FRAME_INTERVAL = POST_DRAFT_LARGE_INTERVAL * FRAMES_PER_SECOND
MIDDLE_FRAME_INTERVAL = POST_DRAFT_MEDIUM_INTERVAL * FRAMES_PER_SECOND
SMALL_FRAME_INTERVAL = POST_DRAFT_SMALL_INTERVAL * FRAMES_PER_SECOND
TINY_FRAME_INTERVAL = POST_DRAFT_TINY_INTERVAL * FRAMES_PER_SECOND
drafting = False
print('DRAFT JUST ENDED: Time - ' + str(int(frameCount / 60 / 60)) + ':' + str(int((frameCount/60)%60)) + " Score - " + str(scores[0]) + " to " + str(scores[1]))
else:
rawClips.append([currentTime - 10, currentTime + 10])
killsPerClip.append(currentScore - lastKillCount)
print('Time - ' + str(int(frameCount / 60 / 60)) + ':' + str(int((frameCount/60)%60)) + " Score - " + str(scores[0]) + " to " + str(scores[1]))
lastKillCount = currentScore
stepSize = LARGE_STEP
end = time.time()
logging.info('Video analyzed for optimal timestamps in {} seconds. Number of frames BEFORE DRAFT: {} Number of frames AFTER DRAFT: {} TOTAL: {}'.format(int(end - start), numberOfFramesCheckedBeforeDraft, numberOfFramesCheckedAfterDraft, numberOfFramesCheckedAfterDraft + numberOfFramesCheckedBeforeDraft))
return self.fixOverlappingClips(rawClips, killsPerClip)
def getScoreFromFrame(self, frame):
# videos might return frames as none when we get to the end
if frame is None:
return None
shape = frame.shape
height = shape[0]
width = shape[1]
# crop the full screen frame into being the two score cards
# then convert them to black and white
leftScore = frame[int(height/90):int(height/38), int(width*(4.5/10)):int(width*(4.75/10))]
leftGray = cv2.cvtColor(leftScore, cv2.COLOR_BGR2GRAY)
leftBw = cv2.threshold(leftGray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
rightScore = frame[int(height/90):int(height/38), int(width*(5.25/10)):int(width*(5.5/10))]
rightGray = cv2.cvtColor(rightScore, cv2.COLOR_BGR2GRAY)
rightBw = cv2.threshold(rightGray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
leftScore = self.extractDigitFromScorecard(leftBw)
if( leftScore is None):
return None
rightScore = self.extractDigitFromScorecard(rightBw)
if( rightScore is None):
return None
return [leftScore, rightScore]
def extractDigitFromScorecard(self, scorecard):
shape = scorecard.shape
height = shape[0]
width = shape[1]
toPredict = []
# moving from left to right, we check each column every NUMBER_OF_SLICES
# to see if it contains any colored dots, indicating a digit.
# we then split the larger scorecard into each individual digit
inMiddleOfDigit = False
xStartOfDigit = 100
for x in range(0, NUMBER_OF_SLICES):
hitBlackMark = False
# check if a black mark exists in this column
for y in range(0, NUMBER_OF_SLICES):
if(scorecard[int(float((height/NUMBER_OF_SLICES)) * y), int((width/NUMBER_OF_SLICES) * x)] == 0):
hitBlackMark = True
if(not inMiddleOfDigit):
xStartOfDigit = x
break
# this means we found a new digit. Lets analyse it
if(not hitBlackMark and inMiddleOfDigit):
currentDigit = scorecard[0:height, int((width/NUMBER_OF_SLICES)*(xStartOfDigit)):int((width/NUMBER_OF_SLICES)* (x))]
# if the "digit" is larger than 15 pixels wide, or narrower than 4,
# its prob fuzz and not a digit
padded = None
currentDigitsWidth = currentDigit.shape[1]
if(currentDigitsWidth <= 4 or currentDigitsWidth >= STANDARDIZED_DIGIT_WIDTH):
xStartOfDigit = 100
inMiddleOfDigit = False
continue
else:
padded = np.pad(currentDigit, pad_width=((0, 0), (0, STANDARDIZED_DIGIT_WIDTH - currentDigit.shape[1])), constant_values = 255)
toPredict.append(padded)
xStartOfDigit = 100
inMiddleOfDigit = False
if(hitBlackMark):
inMiddleOfDigit = True
inDigitColumn = False
if(len(toPredict) == 0):
return None
# shape the values into what the model was trained on
toPredict = np.array(toPredict)
toPredict = toPredict.reshape((toPredict.shape[0], 16, 16, 1))
toPredict = toPredict.astype('float32')
toPredict = toPredict / 255.0
# predict each digit
prediction = self.digitModel.predict(toPredict, verbose=0)
# concat these ints together
finalPrediction = ""
for prediction in prediction:
predictedDigit = np.argmax(prediction)
# durring drafting/when games are done, scropped area might have random fuzz
# the model is normally predicting with ~95% confidence at a minimum, so
# anything that it is less than ~.75% confidence with can be assumed to be
# garbage fuzz
if(prediction[predictedDigit] < THRESHOLD_DIGIT_PREDICTION_CONFIDENCE):
return None
finalPrediction += str(np.argmax(prediction))
return int(finalPrediction)
def fixOverlappingClips(self, rawClips, rawKills):
finalClips = []
finalKills = []
lastClip = [-100, -100]
lastKill = 0
largerClip = []
largerClipKillCount = 0
inBigClip = False
for i in range(0, len(rawClips)):
timestamp = rawClips[i]
killcount = rawKills[i]
# if clips are close to eachother or overlapping, combine
if(timestamp[0] - 10 < lastClip[1]):
if inBigClip:
largerClip[1] = timestamp[1]
largerClipKillCount += killcount
else:
finalClips.pop()
finalKills.pop()
largerClip = [lastClip[0], timestamp[1]]
largerClipKillCount = lastKill + killcount
inBigClip = True
#
else:
if inBigClip:
finalClips.append(largerClip)
finalKills.append(largerClipKillCount)
largerClip = []
largerClipKillCount = 0
inBigClip = False
finalClips.append(timestamp)
finalKills.append(killcount)
lastClip = timestamp
lastKill = killcount
if inBigClip:
finalClips.append(largerClip)
largerClip = []
return finalClips, finalKills