Ejemplo n.º 1
0
def recognize(device_id, voice_path):
    # voice_db_lock.acquire()
    print db.get_speakers()
    # assume only one speaker in one sample, To Do: multiple speakers in one sample
    # set to True to force to avoid diarization, in case a single speaker in the file
    try:
        voice = Voiceid(db, voice_path, single=True)
        # extract_speakers(interactive=False, quiet=False, thrd_n=1)
        voice.extract_speakers(quiet=True, thrd_n=3)
        # clusters = voice.get_clusters()
        cluster = voice.get_cluster('S0')
        # speaker = cluster.get_best_speaker()
        speaker = "unknown"
        speakers = cluster.get_best_five()
        if len(speakers) > 0:
            value = speakers[0][1]
            if value > -33.0:
                speaker = speakers[0][0]
        # speaker = cluster.get_speaker()

        print speaker
        payload = {
            'audio': 'http://52.24.205.33/voice/' + voice_path,
            'userName': speaker,
            'time': '1'
        }
        requests.post('http://129.236.234.21:8080/message',
                      data=json.dumps(payload),
                      headers=HEADERS)
        client.publish("ais/recognize/result/" + device_id + "/" + voice_path,
                       speaker)
        os.remove(voice.get_file_basename() + '.seg')
        os.remove(voice.get_file_basename() + '.g.seg')
        os.remove(voice.get_file_basename() + '.s.seg')
        w = voice.get_file_basename() + '.wav'
        if voice.get_filename() != w:
            os.remove(w)
        shutil.rmtree(voice.get_file_basename())
    except IOError:
        print "voice file doesn't exist"