Jetson Nano - How to play sound when inference value reaches predefined value

Hello,

I am a student following the “Getting Started with AI on Jetson Nano” course. I am trying to make it so that if the value in the prediction widget reaches ~0.8, then it will play a sound file. I have imported the audio file into the Jupyter Notebook and am planning on playing the audio through an HDMI cable to a monitor.

My question is whether the JetPack comes with an audio player installed and if I can just run:

import os

file = “file.mp3”
os.system("mpg123 " + file)

to play audio or if I need some other module. In addition, what would be the variable inside of the code that is the inference value itself? Here is some of the code below:

Live Evaluation

 import threading
 import time
 from utils import preprocess
 import torch.nn.functional as F
 
 state_widget = ipywidgets.ToggleButtons(options=['stop', 'live'], description='state',      value='stop')
 prediction_widget = ipywidgets.Text(description='prediction')
 score_widgets = []
 for category in dataset.categories:
     score_widget = ipywidgets.FloatSlider(min=0.0, max=1.0, description=category,      orientation='vertical')
     score_widgets.append(score_widget)

 def live(state_widget, model, camera, prediction_widget, score_widget):
     global dataset
     while state_widget.value == 'live':
         image = camera.value
         preprocessed = preprocess(image)
         output = model(preprocessed)
         output = F.softmax(output, dim=1).detach().cpu().numpy().flatten()
         category_index = output.argmax()
         prediction_widget.value = dataset.categories[category_index]
         for i, score in enumerate(list(output)):
             score_widgets[i].value = score
        
 def start_live(change):
     if change['new'] == 'live':
         execute_thread = threading.Thread(target=live, args=(state_widget, model,      camera, prediction_widget, score_widget))
         execute_thread.start()

 state_widget.observe(start_live, names='value')

 live_execution_widget = ipywidgets.VBox([
     ipywidgets.HBox(score_widgets),
     prediction_widget,
     state_widget
 ])

 print("live_execution_widget created")

Training and Evaluation

 BATCH_SIZE = 8

 optimizer = torch.optim.Adam(model.parameters())

 epochs_widget = ipywidgets.IntText(description='epochs', value=1)
 eval_button = ipywidgets.Button(description='evaluate')
 train_button = ipywidgets.Button(description='train')
 loss_widget = ipywidgets.FloatText(description='loss')
 accuracy_widget = ipywidgets.FloatText(description='accuracy')
 progress_widget = ipywidgets.FloatProgress(min=0.0, max=1.0, description='progress')

def train_eval(is_training):
global BATCH_SIZE, LEARNING_RATE, MOMENTUM, model, dataset, optimizer, eval_button, train_button, accuracy_widget, loss_widget, progress_widget, state_widget

try:
    train_loader = torch.utils.data.DataLoader(
        dataset,
        batch_size=BATCH_SIZE,
        shuffle=True
    )

    state_widget.value = 'stop'
    train_button.disabled = True
    eval_button.disabled = True
    time.sleep(1)

    if is_training:
        model = model.train()
    else:
        model = model.eval()
    while epochs_widget.value > 0:
        i = 0
        sum_loss = 0.0
        error_count = 0.0
        for images, labels in iter(train_loader):
            # send data to device
            images = images.to(device)
            labels = labels.to(device)

            if is_training:
                # zero gradients of parameters
                optimizer.zero_grad()

            # execute model to get outputs
            outputs = model(images)

            # compute loss
            loss = F.cross_entropy(outputs, labels)

            if is_training:
                # run backpropogation to accumulate gradients
                loss.backward()

                # step optimizer to adjust parameters
                optimizer.step()

            # increment progress
            error_count += len(torch.nonzero(outputs.argmax(1) - labels).flatten())
            count = len(labels.flatten())
            i += count
            sum_loss += float(loss)
            progress_widget.value = i / len(dataset)
            loss_widget.value = sum_loss / i
            accuracy_widget.value = 1.0 - error_count / i
            
        if is_training:
            epochs_widget.value = epochs_widget.value - 1
        else:
            break
except e:
    pass
model = model.eval()

train_button.disabled = False
eval_button.disabled = False
state_widget.value = 'live'

 train_button.on_click(lambda c: train_eval(is_training=True))
 eval_button.on_click(lambda c: train_eval(is_training=False))

 train_eval_widget = ipywidgets.VBox([
     epochs_widget,
     progress_widget,
     loss_widget,
     accuracy_widget,
     ipywidgets.HBox([train_button, eval_button])
 ])

 print("trainer configured and train_eval_widget created") 

Trying to play noise when the slider on right reaches certain value

Thank you for your help.

You need to enable the audio codec for your Nano, please refer to Enabling I2S audio output on 40 Pin Connector - Jetson & Embedded Systems / Jetson Nano - NVIDIA Developer Forums