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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[30]:
|
5 |
+
|
6 |
+
|
7 |
+
import cv2
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
import tensorflow as tf
|
11 |
+
#from sklearn.metrics import confusion_matrix
|
12 |
+
import itertools
|
13 |
+
import os, glob
|
14 |
+
from tqdm import tqdm
|
15 |
+
#from efficientnet.tfkeras import EfficientNetB4
|
16 |
+
|
17 |
+
import tensorflow as tf
|
18 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
|
19 |
+
from tensorflow.keras.preprocessing import image
|
20 |
+
from tensorflow.keras.utils import img_to_array, array_to_img
|
21 |
+
# Helper libraries
|
22 |
+
import numpy as np
|
23 |
+
import matplotlib.pyplot as plt
|
24 |
+
print(tf.__version__)
|
25 |
+
|
26 |
+
import pandas as pd
|
27 |
+
import numpy as np
|
28 |
+
import os
|
29 |
+
|
30 |
+
import tensorflow as tf
|
31 |
+
from tensorflow import keras
|
32 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
33 |
+
from sklearn.preprocessing import LabelBinarizer
|
34 |
+
|
35 |
+
from IPython.display import clear_output
|
36 |
+
import warnings
|
37 |
+
warnings.filterwarnings('ignore')
|
38 |
+
|
39 |
+
import cv2
|
40 |
+
import gradio as gr
|
41 |
+
|
42 |
+
|
43 |
+
# In[46]:
|
44 |
+
|
45 |
+
|
46 |
+
labels = {0: 'Normal', 1: 'RoadAccidents', 2: 'Violent'}
|
47 |
+
|
48 |
+
|
49 |
+
# In[47]:
|
50 |
+
|
51 |
+
|
52 |
+
model = keras.models.load_model("AniketModel.h5", compile=False)
|
53 |
+
|
54 |
+
|
55 |
+
# In[48]:
|
56 |
+
|
57 |
+
|
58 |
+
def videoToFrames(video):
|
59 |
+
|
60 |
+
# Read the video from specified path
|
61 |
+
cam = cv2.VideoCapture(video)
|
62 |
+
|
63 |
+
'''
|
64 |
+
try:
|
65 |
+
|
66 |
+
# creating a folder named data
|
67 |
+
if not os.path.exists('/home/shubham/__New-D__/VITA/Project/redundant/data/Abuse'):
|
68 |
+
os.makedirs('/home/shubham/__New-D__/VITA/Project/redundant/data/Abuse')
|
69 |
+
|
70 |
+
# if not created then raise error
|
71 |
+
except OSError:
|
72 |
+
print ('Error: Creating directory of data')
|
73 |
+
'''
|
74 |
+
|
75 |
+
# frame
|
76 |
+
currentframe = 1
|
77 |
+
while(True):
|
78 |
+
|
79 |
+
# reading from frame
|
80 |
+
ret,frame = cam.read()
|
81 |
+
|
82 |
+
|
83 |
+
if ret:
|
84 |
+
# if video is still left continue creating images
|
85 |
+
# name = '/home/shubham/__New-D__/VITA/Project/redundant/data/Abuse/frame' + str(currentframe) + '.jpg'
|
86 |
+
# print ('Creating...' + name)
|
87 |
+
|
88 |
+
# writing the extracted images
|
89 |
+
|
90 |
+
# cv2.imwrite(name, frame)
|
91 |
+
|
92 |
+
# increasing counter so that it will
|
93 |
+
# show how many frames are created
|
94 |
+
currentframe += 1
|
95 |
+
else:
|
96 |
+
break
|
97 |
+
|
98 |
+
# Release all space and windows once done
|
99 |
+
cam.release()
|
100 |
+
cv2.destroyAllWindows()
|
101 |
+
|
102 |
+
return currentframe
|
103 |
+
|
104 |
+
|
105 |
+
# In[49]:
|
106 |
+
|
107 |
+
|
108 |
+
def hconcat_resize(img_list, interpolation=cv2.INTER_CUBIC):
|
109 |
+
|
110 |
+
# take minimum hights
|
111 |
+
h_min = min(img.shape[0] for img in img_list)
|
112 |
+
|
113 |
+
# image resizing
|
114 |
+
im_list_resize = [cv2.resize(img,
|
115 |
+
(int(img.shape[1] * h_min / img.shape[0]),
|
116 |
+
h_min), interpolation
|
117 |
+
= interpolation)
|
118 |
+
for img in img_list]
|
119 |
+
|
120 |
+
return cv2.hconcat(im_list_resize)
|
121 |
+
|
122 |
+
|
123 |
+
# In[55]:
|
124 |
+
|
125 |
+
|
126 |
+
def make_average_predictions(video_file_path, predictions_frames_count):
|
127 |
+
|
128 |
+
confidences = {}
|
129 |
+
|
130 |
+
number_of_classes = 3
|
131 |
+
|
132 |
+
# Initializing the Numpy array which will store Prediction Probabilities
|
133 |
+
|
134 |
+
#predicted_labels_probabilities_np = np.zeros((predictions_frames_count, number_of_classes), dtype = np.float)
|
135 |
+
|
136 |
+
|
137 |
+
# Reading the Video File using the VideoCapture Object
|
138 |
+
|
139 |
+
video_reader = cv2.VideoCapture(video_file_path)
|
140 |
+
|
141 |
+
|
142 |
+
#print(video_reader)
|
143 |
+
|
144 |
+
|
145 |
+
# Getting The Total Frames present in the video
|
146 |
+
|
147 |
+
video_frames_count = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
|
148 |
+
|
149 |
+
|
150 |
+
# print(video_frames_count)
|
151 |
+
|
152 |
+
|
153 |
+
# Calculating The Number of Frames to skip Before reading a frame
|
154 |
+
|
155 |
+
skip_frames_window = video_frames_count // predictions_frames_count
|
156 |
+
|
157 |
+
|
158 |
+
# print(skip_frames_window)
|
159 |
+
|
160 |
+
frame_counter = 1
|
161 |
+
count = 0
|
162 |
+
features = []
|
163 |
+
|
164 |
+
for frame_counter in range(predictions_frames_count):
|
165 |
+
|
166 |
+
try:
|
167 |
+
|
168 |
+
frames = []
|
169 |
+
|
170 |
+
|
171 |
+
# Setting Frame Position
|
172 |
+
|
173 |
+
#video_reader.set(cv2.CAP_PROP_POS_FRAMES, frame_counter * skip_frames_window)
|
174 |
+
|
175 |
+
|
176 |
+
# Reading The Frame
|
177 |
+
|
178 |
+
_ , frame = video_reader.read()
|
179 |
+
|
180 |
+
#print(frame)
|
181 |
+
|
182 |
+
|
183 |
+
image_height, image_width = 128, 128
|
184 |
+
|
185 |
+
|
186 |
+
# Resize the Frame to fixed Dimensions
|
187 |
+
|
188 |
+
resized_frame = cv2.resize(frame, (image_height, image_width))
|
189 |
+
|
190 |
+
|
191 |
+
# Normalize the resized frame by dividing it with 255 so that each pixel value then lies between 0 and 1
|
192 |
+
|
193 |
+
normalized_frame = resized_frame / 255
|
194 |
+
|
195 |
+
|
196 |
+
#print(normalized_frame)
|
197 |
+
|
198 |
+
|
199 |
+
#normalized_frame = np.vstack([normalized_frame])
|
200 |
+
|
201 |
+
|
202 |
+
#normalized_frame = image.img_to_array(normalized_frame)
|
203 |
+
|
204 |
+
|
205 |
+
#print(frs.shape)
|
206 |
+
|
207 |
+
#print(normalized_frame.shape)
|
208 |
+
|
209 |
+
|
210 |
+
#normalized_frame = image.array_to_img(normalized_frame)
|
211 |
+
|
212 |
+
|
213 |
+
frames.append(normalized_frame)
|
214 |
+
|
215 |
+
|
216 |
+
if frame_counter % 16 == 0:
|
217 |
+
|
218 |
+
|
219 |
+
#frs = np.append(frs, normalized_frame)
|
220 |
+
|
221 |
+
|
222 |
+
#print(frames)
|
223 |
+
|
224 |
+
|
225 |
+
images = cv2.hconcat(frames)
|
226 |
+
|
227 |
+
|
228 |
+
#cv2.imshow('', images)
|
229 |
+
|
230 |
+
|
231 |
+
images = cv2.resize(images, (128, 128))
|
232 |
+
|
233 |
+
|
234 |
+
#images = images / 255
|
235 |
+
|
236 |
+
|
237 |
+
X = image.img_to_array(images)
|
238 |
+
|
239 |
+
|
240 |
+
X = np.expand_dims(X, axis=0)
|
241 |
+
|
242 |
+
|
243 |
+
images = np.vstack([X])
|
244 |
+
|
245 |
+
|
246 |
+
#print(images.shape)
|
247 |
+
#print(images)
|
248 |
+
|
249 |
+
# Passing the Image Normalized Frame to the model and receiving Predicted Probabilities.
|
250 |
+
|
251 |
+
|
252 |
+
predicted_labels_probabilities = model.predict(images)
|
253 |
+
|
254 |
+
#print(predicted_labels_probabilities)
|
255 |
+
|
256 |
+
#predicted_labels_probabilities = model.predict(images)[0]
|
257 |
+
|
258 |
+
|
259 |
+
# Appending predicted label probabilities to the deque object
|
260 |
+
|
261 |
+
predicted_labels_probabilities = np.squeeze(predicted_labels_probabilities)
|
262 |
+
|
263 |
+
print(predicted_labels_probabilities)
|
264 |
+
|
265 |
+
#predicted_labels_probabilities_np[frame_counter] = predicted_labels_probabilities
|
266 |
+
|
267 |
+
prediction = np.argmax(predicted_labels_probabilities)
|
268 |
+
|
269 |
+
print(prediction)
|
270 |
+
|
271 |
+
|
272 |
+
output = labels[prediction]
|
273 |
+
print(output)
|
274 |
+
|
275 |
+
if normalized_frame is not None:
|
276 |
+
features.append(prediction)
|
277 |
+
|
278 |
+
#print(frame_counter)
|
279 |
+
#print(features)
|
280 |
+
|
281 |
+
|
282 |
+
frames = []
|
283 |
+
|
284 |
+
if count < 10:
|
285 |
+
count += 1
|
286 |
+
#print(count)
|
287 |
+
else:
|
288 |
+
break
|
289 |
+
except:
|
290 |
+
break
|
291 |
+
|
292 |
+
"""# Calculating Average of Predicted Labels Probabilities Column Wise
|
293 |
+
|
294 |
+
predicted_labels_probabilities_averaged = predicted_labels_probabilities_np.mean(axis = 0)
|
295 |
+
|
296 |
+
|
297 |
+
|
298 |
+
# Sorting the Averaged Predicted Labels Probabilities
|
299 |
+
|
300 |
+
predicted_labels_probabilities_averaged_sorted_indexes = np.argsort(predicted_labels_probabilities_averaged)[::-1]
|
301 |
+
|
302 |
+
predicted_labels_probabilities_averaged_sorted_indexes = predicted_labels_probabilities_averaged_sorted_indexes[:3]
|
303 |
+
|
304 |
+
|
305 |
+
|
306 |
+
# Iterating Over All Averaged Predicted Label Probabilities
|
307 |
+
|
308 |
+
for predicted_label in predicted_labels_probabilities_averaged_sorted_indexes:
|
309 |
+
|
310 |
+
|
311 |
+
|
312 |
+
# Accessing The Class Name using predicted label.
|
313 |
+
|
314 |
+
predicted_class_name = labels[predicted_label]
|
315 |
+
|
316 |
+
|
317 |
+
|
318 |
+
# Accessing The Averaged Probability using predicted label.
|
319 |
+
|
320 |
+
predicted_probability = predicted_labels_probabilities_averaged[predicted_label]
|
321 |
+
|
322 |
+
|
323 |
+
|
324 |
+
print(f"CLASS NAME: {predicted_class_name} AVERAGED PROBABILITY: {(predicted_probability*100):.2}")
|
325 |
+
|
326 |
+
confidences[predicted_class_name]=predicted_probability
|
327 |
+
|
328 |
+
|
329 |
+
|
330 |
+
|
331 |
+
# Closing the VideoCapture Object and releasing all resources held by it.
|
332 |
+
|
333 |
+
video_reader.release()"""
|
334 |
+
|
335 |
+
return confidences, features
|
336 |
+
|
337 |
+
|
338 |
+
# In[56]:
|
339 |
+
|
340 |
+
|
341 |
+
def most_frequent(List):
|
342 |
+
counter = 0
|
343 |
+
num = List[0]
|
344 |
+
|
345 |
+
for i in List:
|
346 |
+
curr_frequency = List.count(i)
|
347 |
+
if(curr_frequency> counter):
|
348 |
+
counter = curr_frequency
|
349 |
+
num = i
|
350 |
+
|
351 |
+
return num
|
352 |
+
|
353 |
+
|
354 |
+
# In[64]:
|
355 |
+
|
356 |
+
|
357 |
+
video = "/home/shubham/__New-D__/VITA/Project/redundant/production ID_4959443.mp4"
|
358 |
+
#labels = {0: 'RoadAccidents', 1: 'Normal', 2: 'Violent'}
|
359 |
+
framecount = videoToFrames(video)
|
360 |
+
confidences, features = make_average_predictions(video, framecount)
|
361 |
+
List = most_frequent(features)
|
362 |
+
print("The Video You Have Entered is of",labels.get(List))
|
363 |
+
|
364 |
+
#print(confidences)
|
365 |
+
|
366 |
+
|
367 |
+
# In[53]:
|
368 |
+
|
369 |
+
|
370 |
+
"""def classify_video(video):
|
371 |
+
|
372 |
+
labels = {0: 'RoadAccidents', 1: 'Normal', 2: 'Violent'}
|
373 |
+
framecount = videoToFrames(video)
|
374 |
+
confidences, features = make_average_predictions(video, framecount)
|
375 |
+
List = most_frequent(features)
|
376 |
+
#print("The Video You Have Entered is of",labels.get(List))
|
377 |
+
return labels.get(List)
|
378 |
+
|
379 |
+
demo = gr.Interface(classify_video,
|
380 |
+
inputs=gr.Video(),
|
381 |
+
outputs=gr.outputs.Label(),
|
382 |
+
cache_examples=True)
|
383 |
+
|
384 |
+
if __name__ == "__main__":
|
385 |
+
demo.launch(share=False)"""
|
386 |
+
|
387 |
+
|
388 |
+
# In[ ]:
|
389 |
+
|
390 |
+
|
391 |
+
|
392 |
+
|
393 |
+
|
394 |
+
# In[ ]:
|
395 |
+
|
396 |
+
|
397 |
+
|
398 |
+
|