File size: 1,648 Bytes
05b56c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import tensorflow as tf
import numpy as np

# Load trained model
model = tf.keras.models.load_model("saved_model/Audio_Model_Classification.h5")

# IMPORTANT: Keep same order as training (alphabetical default in TF)
CLASS_NAMES = ["Baby Cry", "Chainsaw", "Clock Tick", "Cow", "Dog", "Fire Crackling", "Frog", "Helicopter", "Person Sneeze", "Pig", "Rain", "Rooster", "Sea Waves"]

def predict(img):
    # Convert PIL image → numpy array
    img = np.array(img).astype("float32") / 255.0  # normalize [0,1]

    # Resize to match training target (231x232)
    img = tf.image.resize(img, (231, 232))  # (231, 232, 4)

    # Add batch dimension
    img = np.expand_dims(img, axis=0)  # (1, 231, 232, 4)

    # Predict
    preds = model.predict(img)
    probs = preds[0]

    class_idx = int(np.argmax(probs))
    confidence = float(np.max(probs))
    prob_dict = {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}

    return CLASS_NAMES[class_idx], confidence, prob_dict

# THIS OUR PAST VERSION OF... IT WAS OUT RESIZING BUT EVEN THOUGH THE MODEL WAS WORKING FINE, SO, MAYBE TF COULD ACCEPT DYNAMIC SIZES OF INPUT IMAGES
# def predict(img):
#     # Convert to numpy array (RGBA)
#     img = np.array(img) / 255.0  # shape (H, W, 4)
#     img = np.expand_dims(img, axis=0)  # (1, H, W, 4)
#
#     # Predict
#     preds = model.predict(img)
#     probs = preds[0]
#
#     class_idx = int(np.argmax(probs))
#     confidence = float(np.max(probs))
#     prob_dict = {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
#
#     return CLASS_NAMES[class_idx], confidence, prob_dict