Splasher commited on
Commit
b90f308
1 Parent(s): a22b6f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -100
app.py CHANGED
@@ -43,13 +43,13 @@ import gradio as gr
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("Frame16_EffiecientNet_Model5.h5", compile=False)
53
 
54
 
55
  # In[48]:
@@ -58,19 +58,7 @@ model = keras.models.load_model("Frame16_EffiecientNet_Model5.h5", compile=False
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
@@ -80,17 +68,7 @@ def videoToFrames(video):
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
@@ -125,16 +103,7 @@ def hconcat_resize(img_list, interpolation=cv2.INTER_CUBIC):
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
 
@@ -263,6 +232,8 @@ def make_average_predictions(video_file_path, predictions_frames_count):
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
 
@@ -288,51 +259,8 @@ def make_average_predictions(video_file_path, predictions_frames_count):
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]:
@@ -351,38 +279,31 @@ def most_frequent(List):
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[ ]:
@@ -395,4 +316,3 @@ if __name__ == "__main__":
395
 
396
 
397
 
398
-
 
43
  # In[46]:
44
 
45
 
46
+ labels = {0: 'Voilent', 1: 'Normal', 2: 'RoadAccidents'}
47
 
48
 
49
  # In[47]:
50
 
51
 
52
+ model = keras.models.load_model("Frame16_Densenet_Model.h5", compile=False)
53
 
54
 
55
  # In[48]:
 
58
  def videoToFrames(video):
59
 
60
  # Read the video from specified path
61
+ cam = cv2.VideoCapture(video)
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # frame
64
  currentframe = 1
 
68
  ret,frame = cam.read()
69
 
70
 
71
+ if ret:
 
 
 
 
 
 
 
 
 
 
72
  currentframe += 1
73
  else:
74
  break
 
103
 
104
  def make_average_predictions(video_file_path, predictions_frames_count):
105
 
 
 
106
  number_of_classes = 3
 
 
 
 
 
 
 
107
 
108
  video_reader = cv2.VideoCapture(video_file_path)
109
 
 
232
  print(predicted_labels_probabilities)
233
 
234
  #predicted_labels_probabilities_np[frame_counter] = predicted_labels_probabilities
235
+
236
+ #Argmax is most commonly used in machine learning for finding the class with the largest predicted probability.
237
 
238
  prediction = np.argmax(predicted_labels_probabilities)
239
 
 
259
  break
260
  except:
261
  break
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
+ return features
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
 
266
  # In[56]:
 
279
  return num
280
 
281
 
 
 
 
 
 
 
 
 
 
282
 
283
+ description = """
284
+ Detecting abnormal events automatically from surveillance if any anomalous event happens in front of the surveillance cameras, it can be detected immediately by designing a model.
285
+ """
286
 
287
+ def classify_video(video):
288
 
289
+ labels = {0: 'Voilent', 1: 'Normal', 2: 'RoadAccidents'}
 
 
 
 
 
290
  framecount = videoToFrames(video)
291
+ features = make_average_predictions(video, framecount)
292
  List = most_frequent(features)
293
  #print("The Video You Have Entered is of",labels.get(List))
294
  return labels.get(List)
295
+ example=[["Example_1.mp4"],["Example_2.mp4"],["Example_3.mp4"]]
296
  demo = gr.Interface(classify_video,
297
  inputs=gr.Video(),
298
+ outputs=gr.outputs.Label(),
299
+ title="Anomaly Detection in Surveillance Videos",
300
+ description=description,
301
+ theme="peach",
302
+ examples=example,
303
  cache_examples=True)
304
 
305
  if __name__ == "__main__":
306
+ demo.launch(share=False)
307
 
308
 
309
  # In[ ]:
 
316
 
317
 
318