import gradio as gr import numpy as np import cv2 from tensorflow.keras.applications import VGG16, ResNet50 from tensorflow.keras.models import load_model from tensorflow.keras.applications.vgg16 import preprocess_input from tensorflow.keras.applications.resnet50 import preprocess_input as resnet_preprocess_input from huggingface_hub import hf_hub_download # Download the models from Hugging Face Hub model_path1 = hf_hub_download(repo_id="Jayson20/violence-detection-model", filename="violence_detection_model.h5") model_path2 = hf_hub_download(repo_id="Jayson20/ResNet-50_violence_detection", filename="violence_detection_resnet50_model.h5") model_path3 = hf_hub_download(repo_id="Jayson20/Custom_CNN_violence_detection_model", filename="cnn_lstm_violence_detection_complex_model.h5") # Load the models model1 = load_model(model_path1) # VGG + CNN model model2 = load_model(model_path2) # ResNet + CNN model model3 = load_model(model_path3) # Custom CNN + LSTM model # Load feature extractors for VGG16 and ResNet50 vgg_model = VGG16(include_top=False, weights='imagenet', input_shape=(224, 224, 3)) resnet_model = ResNet50(include_top=False, weights='imagenet', input_shape=(224, 224, 3)) # Custom CNN+LSTM settings frames_per_video = 30 img_size = (64, 64) # Helper function to extract frames from video for CNN+LSTM def extract_frames(video_path, max_frames=frames_per_video): cap = cv2.VideoCapture(video_path) frames = [] while len(frames) < max_frames: ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, img_size) frames.append(frame) cap.release() frames = np.array(frames) # Padding if not enough frames if len(frames) < max_frames: pad_width = ((0, max_frames - len(frames)), (0, 0), (0, 0), (0, 0)) frames = np.pad(frames, pad_width, mode='constant') return np.array(frames) # Preprocess video for VGG-based model def preprocess_video_vgg(video_path): frames = [] cap = cv2.VideoCapture(video_path) while True: ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, (224, 224)) frame = preprocess_input(frame) # Preprocess for VGG frames.append(frame) cap.release() frames = np.array(frames) # Padding or trimming frames to 20 if len(frames) < 20: pad_width = ((0, 20 - len(frames)), (0, 0), (0, 0), (0, 0)) frames = np.pad(frames, pad_width, mode='constant') else: frames = frames[:20] features = vgg_model.predict(frames) features = features.reshape(1, 20, 7, 7, 512) # Reshape for model input return features # Preprocess video for ResNet-based model def preprocess_video_resnet(video_path): frames = [] cap = cv2.VideoCapture(video_path) while True: ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, (224, 224)) frame = resnet_preprocess_input(frame) # Preprocess for ResNet frames.append(frame) cap.release() frames = np.array(frames) # Padding or trimming frames to 20 if len(frames) < 20: pad_width = ((0, 20 - len(frames)), (0, 0), (0, 0), (0, 0)) frames = np.pad(frames, pad_width, mode='constant') else: frames = frames[:20] features = resnet_model.predict(frames) features = features.reshape(1, 20, 7, 7, 2048) # Reshape for model input return features # Predict for a single video using all three models def predict_video(video_path): # Process video for all three models vgg_features = preprocess_video_vgg(video_path) resnet_features = preprocess_video_resnet(video_path) cnn_lstm_frames = extract_frames(video_path) # Model 1 (VGG + CNN) predictions1 = model1.predict(vgg_features) prediction_label1 = 'Violence Detected' if predictions1[0][0] < 0.5 else 'No Violent Actions Detected' # Model 2 (ResNet + CNN) predictions2 = model2.predict(resnet_features) prediction_label2 = 'Violence Detected' if predictions2[0][0] < 0.5 else 'No Violent Actions Detected' # Model 3 (Custom CNN + LSTM) cnn_lstm_frames = cnn_lstm_frames.reshape(1, frames_per_video, img_size[0], img_size[1], 3) # Reshape for model input predictions3 = model3.predict(cnn_lstm_frames) prediction_label3 = 'Violence Detected' if predictions3[0][0] < 0.5 else 'No Violent Actions Detected' # Return separate predictions from all three models return prediction_label1, prediction_label2, prediction_label3 # Define the Gradio interface gr_interface = gr.Interface( fn=predict_video, inputs=gr.Video(label="Input Video"), outputs=[ gr.Textbox(label="VGG16 + LSTM Model Prediction"), gr.Textbox(label="ResNet50 + LSTM Model Prediction"), gr.Textbox(label="Custom CNN + LSTM Model Prediction") ], title="Violence Detection with Multiple Models", description="Upload a video to detect if it contains violence using three different models" ) # Launch the interface if __name__ == "__main__": gr_interface.launch()