Menu

not getting desired results using hyp() function

Help
2018-09-11
2018-09-12
  • akshat dashore

    akshat dashore - 2018-09-11

    hi,
    i used below code for speech recognition

    import pyaudio
    import wave
    import sphinxbase
    import os
    from os import environ, path
    from pocketsphinx.pocketsphinx import
    from sphinxbase.sphinxbase import

    from scipy.io import wavfile
    import logging

    try:
    import pocketsphinx
    except ValueError:
    import pocketsphinx

    BASE_PATH = os.path.dirname(os.path.realpath(file))
    print(BASE_PATH)
    HMDIR = os.path.join(BASE_PATH, "hmm")
    print(HMDIR)
    LMDIR = os.path.join(BASE_PATH, "lm/en-us.lm.bin")
    print(LMDIR)
    DICTD = os.path.join(BASE_PATH, "dict/cmu07a.dic")
    print(DICTD)

    CHUNK = 128
    FORMAT = pyaudio.paInt16
    RATE = 44100
    RECORD_SECONDS = 2
    WAVE_OUTPUT_FILENAME = "output.wav"

    def find_device(p, tags):

    device_index = 4
    for i in range(p.get_device_count()):
        devinfo = p.get_device_info_by_index(i)
        print("Device %d: %s" % (i, devinfo["name"]))
    
        for keyword in tags:
            if keyword in devinfo["name"].lower():
                print("Found an input: device %d - %s"%(i, devinfo["name"]))
                device_index = i
                return device_index
    
    if device_index is None:
        print("No preferred input found; using default input device.")
    
    return device_index
    

    def save_audio(wav_file):

    p = pyaudio.PyAudio()
    
    device = find_device(p, ["input", "mic", "audio"])
    device_info = p.get_device_info_by_index(device)
    channels = int(device_info['maxInputChannels'])
    
    stream = p.open(
        format=FORMAT,
        channels=1,
        rate=RATE,
        input=True,
        frames_per_buffer=CHUNK,
        input_device_index=device
    )
    
    print("* recording")
    
    frames = []
    
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)
    
    print("* done recording")
    
    stream.stop_stream()
    stream.close()
    p.terminate()
    
    wf = wave.open(wav_file, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()
    

    def transcribe(fp):

    config = pocketsphinx.Decoder.default_config()
    config.set_string('-hmm', HMDIR)
    config.set_string('-lm', LMDIR)
    config.set_string('-dict', DICTD) 
    decoder = Decoder(config)
    
    speech_rec = pocketsphinx.Decoder(config)
    opened_file =  open(fp)
    print("\n""types results",type(opened_file),"\n\n")
    
    opened_file.seek(44)
    data = opened_file.read()
    print("value of data",data)
    decoder.start_utt()
    decoder.process_raw(data, False, True)
    print("value of process_raw",decoder.process_raw(data, False, True))
    #decoder.end_utt()
    result = decoder.hyp()
    print("our results",result)
    transcribed = [result]
    logging.info('PocketSphinx ?????%r', transcribed)
    return transcribed
    

    if name == 'main':
    save_audio(WAVE_OUTPUT_FILENAME)
    print(type(WAVE_OUTPUT_FILENAME))
    result = transcribe(WAVE_OUTPUT_FILENAME)
    print "You just said: {0}".format(result[0])

    i didn,t get any error but also not getting OUTPUT below is results when i say "hello world"
    
    i want : to be written text "hello world" after You just said:
    
    \xff\xf3\xff\xda\xff\xc7\xff\xcc\xff\xc8\xff\xb0\xff\xb2\xff\x9e\xff\x9e\xff\xb2\xff\xc0\xff\xcb\xff\xca\xff\xb3\xff\xa1\xffs\xff\\\xffI\xff5\xff\x1f\xff\x03\xff\x11\xff\xf7\xfe\xe3\xfe\xd0\xfe\xe2\xfe\xe7\xfe\xd3\xfe\xb3\xfe\xa2\xfe\xad\xfe\xba\xfe\xcb\xfe\xd1\xfe\xb7\xfe\xa6\xfe\x99\xfeg\xfe]\xfe5\xfe\x19\xfe\x10\xfe\xfe\xfd\xf7\xfd\xf9\xfd\xfb\xfd\xf6\xfd\xf9\xfd\xf3\xfd\xd9\xfd\xcb\xfd\xcb\xfd\xd4\xfd\xd0\xfd\xb4\xfd\xa9\xfd\xac\xfd\xa6\xfd\xa5\xfd\x9c\xfd\x8e\xfdz\xfdr\xfde\xfdn\xfdk\xfdg\xfda\xfdO\xfdM\xfd:\xfd \xfd\x1f\xfd\x1a\xfd1\xfd#\xfd#\xfd\x18\xfd \xfd-\xfdE\xfdc\xfdu\xfd')
    

    INFO: cmn.c(133): CMN: 56.32 5.42 -6.32 -0.67 2.31 8.66 -7.86 12.76 0.48 7.76 -1.23 -2.60 -0.02
    INFO: cmn.c(133): CMN: 59.70 4.58 -7.47 -0.79 2.90 10.03 -8.61 13.88 0.58 7.44 -1.23 -2.84 -0.44
    ('value of process_raw', 339)
    ('our results', None)
    INFO: ngram_search_fwdtree.c(429): TOTAL fwdtree 0.00 CPU -nan xRT
    INFO: ngram_search_fwdtree.c(432): TOTAL fwdtree 0.00 wall -nan xRT
    INFO: ngram_search_fwdflat.c(176): TOTAL fwdflat 0.00 CPU -nan xRT
    INFO: ngram_search_fwdflat.c(179): TOTAL fwdflat 0.00 wall -nan xRT
    INFO: ngram_search.c(303): TOTAL bestpath 0.00 CPU -nan xRT
    INFO: ngram_search.c(306): TOTAL bestpath 0.00 wall -nan xRT
    INFO: ngram_search_fwdtree.c(429): TOTAL fwdtree 0.00 CPU -nan xRT
    INFO: ngram_search_fwdtree.c(432): TOTAL fwdtree 0.00 wall -nan xRT
    INFO: ngram_search_fwdflat.c(176): TOTAL fwdflat 0.00 CPU -nan xRT
    INFO: ngram_search_fwdflat.c(179): TOTAL fwdflat 0.00 wall -nan xRT
    INFO: ngram_search.c(303): TOTAL bestpath 0.00 CPU -nan xRT
    INFO: ngram_search.c(306): TOTAL bestpath 0.00 wall -nan xRT
    You just said: None

    waiting for your valuable reply
    thanks in advance

     
  • akshat dashore

    akshat dashore - 2018-09-11

    i think the reason is that decoder.hyp() does not contains anything will someone please suggest that what to do

     
  • akshat dashore

    akshat dashore - 2018-09-11

    i think the reason is that decoder.hyp() does not contains anything will someone please suggest that what to do

     
  • akshat dashore

    akshat dashore - 2018-09-12

    i resolved the issue by the way thanks lets close the discussion

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.