File size: 1,220 Bytes
ebeed67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""YAMNet audio classification postprocessor.

Input: [numFrames, 521] score matrix
Output: {"kind": "classifications", "classifications": [{"class", "score"}]}
"""
import numpy as np


def postprocess_yamnet(
    predictions: dict,
    config: dict,
    orig_w: int,
    orig_h: int,
    scale: float,
    pad: tuple[int, int],
) -> dict:
    """Average scores across frames, return top classes above threshold."""
    labels = config.get("labels", [])
    min_score = config.get("confidence", 0.1)

    # Get output_0 (scores) — shape [numFrames, 521]
    raw = np.array(list(predictions.values())[0]).astype(np.float32)
    if raw.ndim == 1:
        raw = raw.reshape(1, -1)

    num_frames, num_classes = raw.shape

    # Average across frames
    avg_scores = np.mean(raw, axis=0)

    # Collect classes above threshold, sorted by score
    results = []
    for c in range(num_classes):
        score = float(avg_scores[c])
        if score >= min_score:
            label = labels[c] if c < len(labels) else str(c)
            results.append({"class": label, "score": round(score, 4)})

    results.sort(key=lambda x: x["score"], reverse=True)

    return {"kind": "classifications", "classifications": results}