Views
No views yet
.npy files, generated from audio clips.npy files as input.1import numpy as np
2import tensorflow as tf
3from huggingface_hub import hf_hub_download
4
5# Download the model
6model_path = hf_hub_download(repo_id="ranvir-not-found/resnet50-sda_gunshot-detection", filename="resnet50_model.keras")
7model = tf.keras.models.load_model(model_path)
8
9# Function to load and preprocess .npy file
10def load_and_preprocess_npy(file_path):
11 # Load .npy file
12 mel_spectrogram = np.load(file_path)
13 # Ensure 3D shape: (height, width, channels)
14 if mel_spectrogram.ndim == 2:
15 mel_spectrogram = np.expand_dims(mel_spectrogram, axis=-1)
16 # Resize to 128x128 if needed
17 if mel_spectrogram.shape[:2] != (128, 128):
18 mel_spectrogram = tf.image.resize(mel_spectrogram, (128, 128)).numpy()
19 # Convert to 3-channel (RGB)
20 mel_spectrogram = tf.repeat(mel_spectrogram, 3, axis=-1)
21 # Apply ResNet-50 preprocessing
22 mel_spectrogram = tf.keras.applications.resnet50.preprocess_input(mel_spectrogram)
23 return mel_spectrogram
24
25# Example usage
26npy_path = "path/to/your/spectrogram.npy"
27input_data = load_and_preprocess_npy(npy_path)
28input_data = tf.expand_dims(input_data, axis=0) # Add batch dimension
29predictions = model.predict(input_data)
30class_names = ['gunshot', 'background']
31predicted_class = class_names[np.argmax(predictions[0])]
32print(f"Predicted class: {predicted_class}, Probabilities: {predictions[0]}")