Phoneme Recognition
This guide demonstrates how to perform Phoneme Recognition with Phonexia Speech Platform 4. You can find a high-level description in then Phoneme Recognition article.
Through this guide, we'll be using a mono channel audio in Czech as an example: Paula.wav.
At the end of this guide, you'll find the full Python code example that combines all the steps that will first be discussed separately. This guide should give you a comprehensive understanding on how to integrate Phoneme Recognition in your own projects.
Prerequisites
Follow the prerequisites for setup of Virtual Appliance and Python environment as described in the Task lifecycle code examples.
Run Phoneme Recognition
To run Phoneme Recognition for a single media file, you should start by sending
a POST request to the
/api/technology/phoneme-recognition
endpoint. file and language are mandatory parameters.
The example file is in Czech, so you should pass cs-CZ as the language. In
Python, you can do this as follows:
import json
import requests
VIRTUAL_APPLIANCE_ADDRESS = "http://<virtual-appliance-address>:8000" # Replace with your address
MEDIA_FILE_BASED_ENDPOINT_URL = f"{VIRTUAL_APPLIANCE_ADDRESS}/api/technology/phoneme-recognition"
media_file = "Paula.wav"
params = {"language": "cs-CZ"}
with open(media_file, mode="rb") as file:
files = {"file": file}
start_task_response = requests.post(
url=MEDIA_FILE_BASED_ENDPOINT_URL,
files=files,
params=params
)
print(start_task_response.status_code) # Should print `202`
Note that to get a meaningful results, the language parameter has to match the
language spoken in the media file. See the documentation of the list of
supported languages.
If the task has been successfully accepted, the 202 code will be returned
together with a unique task ID in the response body. The task isn't processed
immediately, but only scheduled for processing. You can check the current task
status by polling for the result.
Polling
To obtain the final result, periodically query the task status until the task
state changes to done, failed, or rejected. The general polling procedure
is described in detail in the
Task lifecycle code examples.
Result for Phoneme Recognition
The result field of the task contains the list of channels. Each channel
contains information about channel_number and segments. Segments is a list of
recognized phonemes, containing information about phoneme, start_time, and
end_time.
For our sample file, the task result should look as follows (result has been truncated for brevity sake):
{
"channels": [
{
"channel_number": 0,
"segments": [
{
"phoneme": "sil",
"start_time": 0.29,
"end_time": 0.65
},
{
"phoneme": "e",
"start_time": 0.65,
"end_time": 0.86
},
{
"phoneme": "l",
"start_time": 0.86,
"end_time": 0.92
},
{
"phoneme": "d",
"start_time": 0.92,
"end_time": 0.98
},
...,
{
"phoneme": "sil",
"start_time": 38.46,
"end_time": 38.85
}
]
}
]
}
Full Python code
Here is the full example on how to run the Phoneme Recognition technology. The code is slightly adjusted and wrapped into functions for better readability. Refer to the Task lifecycle code examples for generic code template, applicable to all technologies.
import json
import requests
import time
VIRTUAL_APPLIANCE_ADDRESS = "http://<virtual-appliance-address>:8000" # Replace with your address
MEDIA_FILE_BASED_ENDPOINT_URL = f"{VIRTUAL_APPLIANCE_ADDRESS}/api/technology/phoneme-recognition"
def poll_result(polling_url, polling_interval=5):
"""Poll the task endpoint until processing completes."""
while True:
polling_task_response = requests.get(polling_url)
polling_task_response.raise_for_status()
polling_task_response_json = polling_task_response.json()
task_state = polling_task_response_json["task"]["state"]
if task_state in {"done", "failed", "rejected"}:
break
time.sleep(polling_interval)
return polling_task_response
def run_media_based_task(media_file, params):
"""Create a media-based task and wait for results."""
with open(media_file, mode="rb") as file:
files = {"file": file}
start_task_response = requests.post(
url=MEDIA_FILE_BASED_ENDPOINT_URL,
files=files,
params=params
)
start_task_response.raise_for_status()
polling_url = start_task_response.headers["Location"]
task_result = poll_result(polling_url)
return task_result.json()
# Run Phoneme Recognition
media_file = "Paula.wav"
params = {"language": "cs-CZ"}
print(f" Running Phoneme Recognition for file {media_file}.")
media_file_based_task = run_media_based_task(media_file=media_file, params=params)
media_file_based_task_result = media_file_based_task["result"]
print(json.dumps(media_file_based_task_result, indent=2))