Home > Net >  How do you define a specific label in python
How do you define a specific label in python

Time:06-02

import os
import sys, getopt
import signal
import time
from edge_impulse_linux.audio import AudioImpulseRunner
DEFAULT_THRESHOLD = 0.60

my_threshold = DEFAULT_THRESHOLD

runner = None

def signal_handler(sig, frame):
print('Interrupted')
if (runner):
    runner.stop()
sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

def help():
print('python classify.py <path_to_model.eim> <audio_device_ID, optional>' )

def my_function(label, score):
print('' )

def main(argv):



try:
    opts, args = getopt.getopt(argv, "h", ["--help"])
except getopt.GetoptError:
    help()
    sys.exit(2)

for opt, arg in opts:
    if opt in ('-h', '--help'):
        help()
        sys.exit()

if len(args) == 0:
    help()
    sys.exit(2)

model = args[0]

dir_path = os.path.dirname(os.path.realpath(__file__))
modelfile = os.path.join(dir_path, model)

with AudioImpulseRunner(modelfile) as runner:
    try:
        model_info = runner.init()
        labels = model_info['model_parameters']['labels']
        print('Loaded runner for "'   model_info['project']['owner']   ' / '   
model_info['project']['name']   '"')

        #Let the library choose an audio interface suitable for this model, or pass device ID 
parameter to manually select a specific audio interface
        selected_device_id = None
        if len(args) >= 2:
            selected_device_id=int(args[1])
            print("Device ID "  str(selected_device_id)   " has been provided as an 
argument.")

        for res, audio in runner.classifier(device_id=selected_device_id):
            print('Result (%d ms.) ' % (res['timing']['dsp']   res['timing'] 
['classification']), end='')
            for label in labels:
                score = res['result']['classification'][label]
                print('%s: %.2f\t' % (label, score), end='')
            print('', flush=True)

            
            
            print('', flush=True)
            if score > my_threshold:
                my_function(label,score)


                print('Yes', flush=True)
            if label == "Hey Bmo":
                my_function(label,score)

         

    finally:
        if (runner):
            runner.stop()

if __name__ == '__main__':
main(sys.argv[1:])

main(sys.argv[1:])

I'm trying to make the Threshold check a specific label as it has multiple and

                print('Yes', flush=True)
            if label == "Hey Bmo":
                my_function(label,score)

This doesn't work, this is my first time messing around with python so please excuse my lacklustre code attempt

terminal readout:

result (11 ms.) Hey Bmo: 0.02 Noise: 0.94 Unknown: 0.04

result (17 ms.) Hey Bmo: 0.90 Noise: 0.10 Unknown: 0.15

result (07 ms.) Hey Bmo: 0.05 Noise: 0.80 Unknown: 0.20

result (19 ms.) Hey Bmo: 0.10 Noise: 0.40 Unknown: 0.01

result (14 ms.) Hey Bmo: 0.01 Noise: 0.50 Unknown: 0.5

In conclusion when Hey Bmo reaches 0.60 trigger my threshold right now it checks Unknowns threshold

CodePudding user response:

Hi without reproducing your exact problem I would say that you should include the if-statement in your for loop.

        for label in labels:
            score = res['result']['classification'][label]
            if label == "Hey Bmo":
               my_function(label,score)
            print('%s: %.2f\t' % (label, score), end='')
        print('', flush=True)

        
        

CodePudding user response:

if label == ("Labels name") and (score > my_threshold):
print("Working", flush=True)
                    
                    
my_function(label,score)

I used this in the for loop and it seems to work

Thanks to fabian for helping me figure it out

  • Related