def voice_handler(self, bot, update): """ Voice recognition """ file = self.bot.getFile(update.message.voice.file_id) print("file_id: " + str(update.message.voice.file_id)) text = speech_to_text(file) print(text) update.message.text = text return self.plain_text_manager(bot, update)
def detect_voice(): f = open('./file.ogg', 'wb') f.write(request.data) f.close() converted = speech.speech_to_text('file.ogg') resp = requests.post(url='http://127.0.0.1:5000/send_message', data={"message": converted}) resp = resp.json() resp["human_mes"] = converted return jsonify(resp)
def run(): try: system.play("listen") #воспроизведение предупреждения о начале записи system.record("record") #запись голосовой команды text = speech.speech_to_text("record") #преобразование голосовой записи в текстовую строку answer = chat.get_answer(text) #получение ответа из преобразованной фразы if answer != "": speech.text_to_speech(answer, "temp") #синтез ответа в голосовую запись system.convert("temp") #конвертация ответа в необходимый формат system.play("temp") #воспроизведение ответа через динамики except Exception: system.play("error") #обработка ошибки и воспроизведение сообщения об ошибке
def record(self, time_limit=2): # Play start recording speech.play_file('siri_start.mp3') self.query = speech.speech_to_text(time_limit=time_limit) # If error if self.query == 'speech error': self.nao.talk({'text': "I didn't quite get that, please repeat"}) self.record() if self.query == 'connection error': self.nao.talk({ 'text': "I'm sorry, the connection is not available right now" }) sys.exit() # Play stop recording speech.play_file('siri_end.mp3')
def index(): language = 'en-US' speak_prompt(langToprompt[language], language) record_speech(filename='help.wav') convert_to_flac(filename='help.wav') texts = speech_to_text('help.flac', lang=language) is_emergency = True score = None if texts: is_emergency, score = detect_help_intent(texts[0]) if is_emergency: # print('Emergency text sent') msg = generate_message( grandma['name'], grandma['time_since_event'], grandma['address']) send_message(msg, grandma['emergency_number']) make_call(msg, grandma['emergency_number']) # TODO: Send Conversation through the socket to front end send_conversation('Are you Okay?', texts[0], True) return jsonify({'resp': texts, 'is_emergency': is_emergency, 'Sentiment score': score})
def run(audio_path: str = 'test.wav', min_length: int = 1000, db_threshold: int = -32, speech_api: str = 'sphinx') -> list: text = speech_to_text(audio_path, min_length=min_length, db_threshold=db_threshold, speech_api=speech_api) print(f'Speech to text: {" ".join(t for t, a in text)}\n') for text_audio in text: results = dict() sentence, chunk = text_audio index = chunk.split('_')[1].split('.')[0] with open(chunk, 'rb') as chunk_bin: results['audio_bin'] = Binary(chunk_bin.read()) results.update(parse(sentence, index=index)) results['created'] = datetime.now() db.texts.insert_one(results)
def process_message(event, user_id, channel_id, text): global USER_LANGUAGE_MAPPINGS user_name = get_name_from_user_id(user_id) logger.info(f"Sender of the message is {user_name}") # When a new user joins if event.get("subtype") == 'channel_join': if not channel_id in USER_LANGUAGE_MAPPINGS: USER_LANGUAGE_MAPPINGS[channel_id] = {} USER_LANGUAGE_MAPPINGS[channel_id].update({user_id: 'en'}) with open(f'{Path(__file__).parent}/user_language_mapping.json', 'w') as fp: json.dump(USER_LANGUAGE_MAPPINGS, fp, sort_keys=True, indent=4) try: slack_web_client.chat_postEphemeral( user=user_id, attachments=language_selecter["attachments"], channel=channel_id, text=f"Hello {user_name}") return except SlackApiError: logger.info("Failed to send message", exc_info=True) # ENDS HERE if 'files' in event: files = event["files"] audio_url = files[0].get("url_private_download") user_preferred_language = USER_LANGUAGE_MAPPINGS.get(channel_id).get( user_id) file_name = get_audio_file(audio_url, user_id) logger.info(f"File name of audio received is :{file_name}") text = speech.speech_to_text(file_name, user_preferred_language) logger.info(f"Text from audio {file_name} is : {text}") if text: text = text.replace("'", "\\'") source_language = translate_api.detect_language(GOOGLE_TRANSLATE_API_KEY, text) logger.info( f"Detected language of the message is {get_language_from_iso_code(source_language)}" ) # When a message is posted, translate it for all the users in the channel for target_user_id, target_language in USER_LANGUAGE_MAPPINGS.get( channel_id).items(): if target_user_id != user_id: logger.info( f'Target user is {user_name} and target language is {get_language_from_iso_code(target_language)} ' ) start = time.time() translated_text = get_translated_message(text, source_language, target_language) logger.info( f'Translated text in {get_language_from_iso_code(target_language)} is {translated_text}. Completed in {time.time()-start} seconds' ) try: slack_web_client.chat_postEphemeral( user=target_user_id, channel=channel_id, text=f"*{user_name}* :\n{translated_text} ") except SlackApiError: logger.error("Failed to send message", exc_info=True)
import functions import yolopy import speech import cv2 import os import detect import datetime os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "dfkey.json" labelsPath = "yolo/coco.names" weightsPath = "yolo/yolov3.weights" configPath = "yolo/yolov3.cfg" args = {"threshold": 0.3, "confidence": 0.5} project_id = "blindbot-4f356" #project_id = "blindbot-286ed" engine = speech.speech_to_text() model = yolopy.yolo(labelsPath, weightsPath, configPath) listening = False intent = None while True: cam = cv2.VideoCapture(1) if not listening: resp = engine.recognize_speech_from_mic() print(resp) if (resp != None): intent, text = detect.detect_intent_texts(project_id, 0, [resp], 'en') if (intent == 'Jyoti' and resp != None): listening = True