簡體   English   中英

你如何在python中定義一個特定的標簽

[英]How do you define a specific label in python

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:])

我試圖讓閾值檢查一個特定的標簽,因為它有多個和

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

這不起作用,這是我第一次搞亂python所以請原諒我乏善可陳的代碼嘗試

終端讀數:

結果(11 毫秒)嘿 Bmo:0.02 噪音:0.94 未知:0.04

結果(17 毫秒)嘿 Bmo:0.90 噪音:0.10 未知:0.15

結果(07 毫秒)嘿 Bmo:0.05 噪音:0.80 未知:0.20

結果(19 毫秒)嘿 Bmo:0.10 噪音:0.40 未知:0.01

結果(14 毫秒)嘿 Bmo:0.01 噪音:0.50 未知:0.5

總之,當 Hey Bmo 達到 0.60 時立即觸發我的閾值,它會檢查未知閾值

嗨,如果沒有重現您的確切問題,我會說您應該在 for 循環中包含 if 語句。

        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)

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

我在 for 循環中使用了它,它似乎有效

感謝 fabian 幫我弄清楚

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM