kalyani94/UI_02
0
1import cv22import numpy as np3 4import tensorflow as tf5#from sklearn.metrics import confusion_matrix6import itertools7import os, glob8from tqdm import tqdm9#from efficientnet.tfkeras import EfficientNetB410 11import tensorflow as tf12from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions13from tensorflow.keras.preprocessing import image14from tensorflow.keras.utils import img_to_array, array_to_img15# Helper libraries16import numpy as np17import matplotlib.pyplot as plt18print(tf.__version__)19 20import pandas as pd21import numpy as np22import os23 24import tensorflow as tf25from tensorflow import keras26from tensorflow.keras.preprocessing.image import ImageDataGenerator27from sklearn.preprocessing import LabelBinarizer28 29from IPython.display import clear_output30import warnings31warnings.filterwarnings('ignore')32 33import cv234import gradio as gr35 36 37 38 39 40 41 42labels =['Abuse','Arrest','Arson','Assault','Burglary','Explosion','Fighting',"Normal",'RoadAccidents','Robbery','Shooting','Shoplifting','Stealing','Vandalism']43 44model = keras.models.load_model("classifier.h5")45 46def videoToFrames(video):47 48 # Read the video from specified path 49 cam = cv2.VideoCapture(video) 50 51 '''try: 52 53 # creating a folder named data 54 if not os.path.exists('/home/shubham/__New-D__/VITA/Project/redundant/data/Abuse'): 55 os.makedirs('/home/shubham/__New-D__/VITA/Project/redundant/data/Abuse') 56 57 # if not created then raise error 58 except OSError: 59 print ('Error: Creating directory of data') 60 '''61 62 # frame 63 currentframe = 164 while(True): 65 66 # reading from frame 67 ret,frame = cam.read() 68 69 70 if ret: 71 # if video is still left continue creating images 72 #name = '/home/shubham/__New-D__/VITA/Project/redundant/data/Abuse/frame' + str(currentframe) + '.jpg'73 #print ('Creating...' + name) 74 75 # writing the extracted images 76 77 #cv2.imwrite(name, frame) 78 79 # increasing counter so that it will 80 # show how many frames are created 81 currentframe += 182 else: 83 break84 85 # Release all space and windows once done 86 cam.release() 87 cv2.destroyAllWindows() 88 89 return currentframe90 91def make_average_predictions(video_file_path, predictions_frames_count):92 93 confidences={}94 95 number_of_classes = 1496 97 # Initializing the Numpy array which will store Prediction Probabilities98 predicted_labels_probabilities_np = np.zeros((predictions_frames_count, number_of_classes), dtype = np.float)99 100 # Reading the Video File using the VideoCapture Object101 video_reader = cv2.VideoCapture(video_file_path)102 103 #print(video_reader)104 105 # Getting The Total Frames present in the video106 107 video_frames_count = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))108 109 #print(video_frames_count)110 111 # Calculating The Number of Frames to skip Before reading a frame112 113 skip_frames_window = video_frames_count // predictions_frames_count114 115 #print(skip_frames_window)116 117 118 119 for frame_counter in range(predictions_frames_count):120 121 122 # Setting Frame Position123 124 video_reader.set(cv2.CAP_PROP_POS_FRAMES, frame_counter * skip_frames_window)125 126 127 128 # Reading The Frame129 130 _ , frame = video_reader.read()131 132 133 134 image_height, image_width = 64, 64135 136 137 # Resize the Frame to fixed Dimensions138 139 resized_frame = cv2.resize(frame, (image_height, image_width))140 141 142 143 # Normalize the resized frame by dividing it with 255 so that each pixel value then lies between 0 and 1144 145 normalized_frame = resized_frame / 255146 147 148 149 # Passing the Image Normalized Frame to the model and receiving Predicted Probabilities.150 151 predicted_labels_probabilities = model.predict(np.expand_dims(normalized_frame, axis = 0))[0]152 153 154 155 # Appending predicted label probabilities to the deque object156 157 predicted_labels_probabilities_np[frame_counter] = predicted_labels_probabilities158 159 160 161 # Calculating Average of Predicted Labels Probabilities Column Wise162 163 predicted_labels_probabilities_averaged = predicted_labels_probabilities_np.mean(axis = 0)164 165 166 167 # Sorting the Averaged Predicted Labels Probabilities168 169 predicted_labels_probabilities_averaged_sorted_indexes = np.argsort(predicted_labels_probabilities_averaged)[::-1]170 171 predicted_labels_probabilities_averaged_sorted_indexes = predicted_labels_probabilities_averaged_sorted_indexes[:3]172 173 # Iterating Over All Averaged Predicted Label Probabilities174 175 for predicted_label in predicted_labels_probabilities_averaged_sorted_indexes:176 177 178 179 # Accessing The Class Name using predicted label.180 181 predicted_class_name = labels[predicted_label]182 183 184 185 # Accessing The Averaged Probability using predicted label.186 187 predicted_probability = predicted_labels_probabilities_averaged[predicted_label]188 189 190 191 #print(f"CLASS NAME: {predicted_class_name} AVERAGED PROBABILITY: {(predicted_probability*100):.2}")192 193 confidences[predicted_class_name]=predicted_probability 194 195 196 197 198 # Closing the VideoCapture Object and releasing all resources held by it.199 200 video_reader.release()201 202 return confidences203 204 205def classify_video(video):206 207 framecount = videoToFrames(video)208 confidences = make_average_predictions(video, framecount)209 210 return confidences211 #return confidences212 213demo = gr.Interface(classify_video, 214 inputs=gr.Video(), 215 outputs=gr.outputs.Label(), 216 cache_examples=True)217 218if __name__ == "__main__":219 demo.launch(share=False)220 221 222 223 