#python #json #ibm-cloud #speech-recognition
#python #json #ibm-cloud #распознавание речи
Вопрос:
Я пытаюсь создать простую программу SR на python, которая распознает речь. Я использую для этого API IBM cloud и сталкиваюсь с некоторыми проблемами.
import json;
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator('KEY')
speech_to_text = SpeechToTextV1(
authenticator=authenticator
)
speech_to_text.set_service_url('URL')
with open(join(dirname(__file__), './.', 'recording.mp3'),
'rb') as audio_file:
speech_recognition_results = speech_to_text.recognize(
audio=audio_file,
content_type='audio/mp3',
word_alternatives_threshold=0.9,
keywords=['um', 'uh', 'like'],
keywords_threshold=0.5
).get_result()
print(json.dumps(speech_recognition_results, indent=2))
Этот код работает отлично, но у меня возникают проблемы с выводом.
{
"result_index": 0,
"results": [
{
"final": true,
"alternatives": [
{
"transcript": "hello Ryan how are you today ",
"confidence": 0.73
}
],
"word_alternatives": [
{
"start_time": 0.19,
"end_time": 0.45,
"alternatives": [
{
"word": "hello",
"confidence": 0.93
}
]
},
{
"start_time": 0.45,
"end_time": 0.99,
"alternatives": [
{
"word": "Ryan",
"confidence": 0.92
}
]
},
{
"start_time": 1.12,
"end_time": 1.31,
"alternatives": [
{
"word": "how",
"confidence": 0.98
}
]
},
{
"start_time": 1.31,
"end_time": 1.41,
"alternatives": [
{
"word": "are",
"confidence": 0.98
}
]
},
{
"start_time": 1.41,
"end_time": 1.61,
"alternatives": [
{
"word": "you",
"confidence": 0.98
}
]
},
{
"start_time": 1.61,
"end_time": 2.04,
"alternatives": [
{
"word": "today",
"confidence": 0.99
}
]
}
],
"keywords_result": {}
}
]
}
Все, что я хочу сделать, это изолировать ту часть, где она отображает всю расшифровку во что-то вроде переменной. Как бы я это сделал?
Комментарии:
1.
speech_recognition_results
это словарь python, а не JSON. Вы знаете, как анализировать dict?2. Понятия не имею. Я предполагаю, что фильтрация результатов имеет какое-то отношение к json, потому что он использует json.dumps.
3. Вы можете забыть
json.dumps
, если хотите разобрать одну строку. Возможно, документ «Структуры данных » прояснит ситуацию4. как насчет
x = speech_recognition_results["results"][0]["alternatives"][0]["transcript"]