2017-11-18 4 views
0

ich einen Code von einer Website kopiert bestimmte Wörter in Python mit pocketsphinx.It obwohl läuft zu hören, gibt aber nie das Schlüsselwort als expected.This mein Code:pocket in Python kehrt zufällige Wörter in Keyword-Suche

import sys, os 
from pocketsphinx.pocketsphinx import * 
from sphinxbase.sphinxbase import * 
import pyaudio 

# modeldir = "../../../model" 
# datadir = "../../../test/data" 

modeldir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//en-us" 
dictdir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//cmudict-en-us.dict" 
lmdir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//en-us.lm.bin" 
# Create a decoder with certain model 
config = Decoder.default_config() 
config.set_string('-hmm', modeldir) 
config.set_string('-lm', lmdir) 
config.set_string('-dict', dictdir) 
config.set_string('-keyphrase', 'forward') 
config.set_float('-kws_threshold', 1e+20) 

p = pyaudio.PyAudio() 
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024) 
stream.start_stream() 

# Process audio chunk by chunk. On keyword detected perform action and restart search 
decoder = Decoder(config) 
decoder.start_utt() 
while True: 
    buf = stream.read(1024) 
    if buf: 
     decoder.process_raw(buf, False, False) 
    else: 
     break 
    if decoder.hyp() != None: 
     #print(decoder.hyp().hypstr) 
     if decoder.hyp().hypstr == 'forward': 
     print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()]) 
     print ("Detected keyword, restarting search") 
     decoder.end_utt() 
     decoder.start_utt() 

auch wenn ich print(decoder.hyp().hypstr)

es gibt nur zufällige Wörter, wenn ich anything.For ex sprechen, wenn ich ein Wort oder eine Zeile sprechen es gibt:

the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the 
the da 
the head 
the bed 
the bedding 
the heading of 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and 
the bedding and well 
the bedding and well 
the bedding and well 
the bedding and butler 
the bedding and what lingus 
the bedding and what lingus 
the bedding and what lingus 
the bedding and what lingus ha 
the bedding and blessed are 
the bedding and blessed are 
the bedding and what lingus on 
the bedding and what lingus want 
the bedding and what lingus want 
the bedding and what lingus want 
the bedding and what lingus want 
the bedding and what lingus want or 
the bedding and what lingus want to talk 
the bedding and what lingus current top 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk 
the bedding and what lingus want to talk to her 
the bedding and what lingus want to talk to her 
the bedding and what lingus want to talk to her 
the bedding and what lingus want to talk to her 

Bitte helfen Sie mir dabei. Ich bin nur ein Neuling in Python.

Antwort

1

Erstens möchte ich nur klären; Ihr Pocksphinx ist funktioniert.

So, basierend auf meiner Erfahrung pocketsphinx verwendet wird, ist es kaum die most accurate Spracherkennung Tool, das Sie verwenden können, aber wahrscheinlich die beste Wahl für eine Offline-Lösung. Pockersphinx kann Ihre Wörter (Audio) nur so gut übersetzen wie es 'model vorschreibt. Diese Modelle scheinen immer noch in Arbeit zu sein und vieles davon muss verbessert werden. Es gibt einige Dinge, die Sie tun können, um die Genauigkeit der Erkennung zu erhöhen. wie reducing noise und tuning the recognition, aber das ist außerhalb des unmittelbaren Umfangs dieser Frage.

Von dem, was ich in Ihrem Code verstehe, Sie suchen nach einem bestimmten Schlüsselwort (mündlich, vom Benutzer) und haben es über das Backend von Pockshinx erkannt. Dieses Keyword scheint "vorwärts" zu sein. Sie können weiterlesen, wie Sie richtig erreichen können "hot word listening".

Sie haben die richtige Idee, aber der Ansatz kann verbessert werden. Hier ist meine „Quick Fix“ Version des Codes:

import os 
import pyaudio 
import pocketsphinx as ps 

modeldir = "C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//" 

# Create a decoder with certain model 
config = ps.Decoder.default_config() 
config.set_string('-hmm', os.path.join(modeldir, 'en-us')) 
config.set_string('-lm', os.path.join(modeldir, 'en-us.lm.bin')) 
config.set_string('-dict', os.path.join(modeldir, 'cmudict-en-us.dict')) 
config.set_string('-keyphrase', 'forward') 
config.set_float('-kws_threshold', 1e+20) 

p = pyaudio.PyAudio() 
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024) 
stream.start_stream() 

# Process audio chunk by chunk. On keyword detected perform action and restart search 
decoder = ps.Decoder(config) 
decoder.start_utt() 

while True: 
    buf = stream.read(1024) 
    if buf: 
     decoder.process_raw(buf, False, False) 
    else: 
     break 
    if decoder.hyp() is not None: 
     print(decoder.hyp().hypstr) 
     if 'forward' in decoder.hyp().hypstr: 
      print([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()]) 
      print("Detected keyword, restarting search") 
      decoder.end_utt() 
      decoder.start_utt() 

Für eine pocketsphinx.Decoder() „Session“ (dh die .start_utt() Methode aufrufen, ohne anschließend .ent_utt() Aufruf), die decoder.hyp().hypstr Variable effektiv weiterhin Worte selbst hinzufügen sobald es erkennt, dass der eingegebene Audiostrom eine "gültige" Übersetzung/Erkennung von der Taschensphinx-Decodierung hatte. Sie haben if decoder.hyp().hypstr == 'forward': verwendet. Was dies tut ist, es zwingt die ganze Zeichenfolge, genau "vorwärts" für den Code zu sein, um diesen (ich vermute, gewünscht ... ja?) Bedingten Codeblock einzugeben. Da pockenshinx standardmäßig nicht sehr genau ist, dauert es bei den meisten Wörtern im Allgemeinen ein paar Versuche, damit das korrekte Wort tatsächlich registriert wird. Aus diesem Grund, und seit decoder.hyp().hypstr zu sich selbst hinzugefügt (wie zuvor erläutert), habe ich die Zeile if 'forward' in decoder.hyp().hypstr: verwendet. Dabei wird nach dem gewünschten Schlüsselwort "forward" in der gesamten Zeichenfolge gesucht. Auf diese Weise wird eine falsche Erkennung ermöglicht, bis das Schlüsselwort gefunden wird.

Ich hoffe, es hilft!

+0

Danke für die Antwort.Aber dieser Code ist auch keine Hilfe Bruder.Es erkennt nie Wort "weiter" in der Sprache und druckt nur zufällige Wort, wenn ich mit ihm sprechen.Ist es etwas, was ich in dem Modell fehlt? – TechieBoy101

+0

Wenn ich sage 'Vorwärts' dies ist die Ausgabe oh oh oh aber oh aber oh aber oh aber oh aber oh oh aber oh oh aber oh oh aber oh oh aber oh oh, aber oh oh aber oh oh aber oh oh aber oh Apfel oder Anwalt oh aber oh Apfel rick oh aber oh Apfel befreien oh aber oh Apfel rick aber oh aber oh Apfel rick vor oh aber oh Apfel rick Ball oh aber oh Apfel oder Lord Butler oh aber oh Apfel rick Ball oh aber oh Apfel rick Bar waren oh aber oh Apfel rick vor unserer Arbeit oh aber oh apple rick vor unserem Wort
Kein Zeichen von 'Detected keywo ...' was bedeutet, dass es das Schlüsselwort nie identifiziert. – TechieBoy101

+0

All dies bedeutet, dass die "Übersetzung" von Taschensphinx nicht sehr genau ist mit den Daten, die Sie hineinlegen. Deshalb, wie ich schon sagte, müssen Sie einige (viele) Male versuchen, bevor pocketsphinx Ihr Wort richtig erkennt. Ich verstehe, wie unbefriedigend das ist. Sie müssen dann sehen, ** die Genauigkeit ** Ihrer Anerkennung zu erhöhen, und ** richtig implementieren "heißen Wort hören". Die Links dazu finden Sie in meiner ursprünglichen Antwort. –

0

Sie benötigen diese Linie sind

config.set_string('-lm', lmdir) 

keyphrase Suche und lm Suche zu entfernen, sich gegenseitig ausschließen.

+0

Vielen Dank Man.It wirklich funktioniert.Ich musste Sie fragen, ob es eine Möglichkeit gibt, mehr als 1 Keyword oder einen Satz in Taschensphinx zu hören.Ist das möglich? – TechieBoy101

+0

Ja, Sie können die Keyword-Liste verwenden, siehe http://cmusphinx.github.io/wiki/tutoriallm –