All Products
Search
Document Center

Function Compute-based recording file recognition

Last Updated: Jun 02, 2020

If you store recording files in Alibaba Cloud Object Storage Service (OSS), you can not only integrate the SDK, but also use Function Compute to recognize recording files. With Function Compute, you can use trigger functions to automatically recognize recording files and store recognition results in OSS or other storage devices. Without integrating and developing the SDK, you only need to focus on the final recognition results. If you are not a developer, you can use Function Compute to quickly obtain recognition results for further analysis.For more information about Function Compute, see official documentation.

2. Effects

Note: This topic makes the following assumptions: You create an OSS bucket named nls-file-trans, and store recording files in the filetrans/raw directory and recognition results in the filetrans/result directory. Recognition results are stored in JSON files. The File name_Task ID.json file stores a successful recognition result. The File name_Task ID_failed.json file stores a failed recognition result. You can use your own bucket and file directories based on the actual scenarios.

  • Upload recording files in the OSS console

In the OSS console, you can click Upload on the details page of the nls-file-trans bucket to upload recording files to the filetrans/raw directory, as shown in the following figure.Upload files

The following figure shows the created Function Compute triggers on the Function Compute tab. This topic describes Function Compute triggers later.Function Compute

The following figure shows recognition results that are stored in the filetrans/result directory.Recognition results

The following example shows the recognition result of the uploaded nls-sample-16k.wav file:

  1. {
  2. "Result": {
  3. "Sentences": [{
  4. "EndTime": 2365,
  5. "SilenceDuration": 0,
  6. "BeginTime": 340,
  7. "Text": "Weather in Beijing",
  8. "ChannelId": 0,
  9. "SpeechRate": 177,
  10. "EmotionValue": 5.0
  11. }]
  12. },
  13. "TaskId": "fb0474184c6d11e9a213e11db149f2ff",
  14. "StatusCode": 21050000,
  15. "StatusText": "SUCCESS",
  16. "RequestTime": 1553237085804,
  17. "SolveTime": 1553237086146,
  18. "BizDuration": 2956
  19. }

The following example shows how to use ossutil to upload a recording file:

  1. ossutil cp nls-sample-16k.wav oss://nls-file-trans/filetrans/raw/nls-sample-16k.wav

The recognition process for a recording file uploaded in this way is the same as that for a recording file uploaded in the OSS console.

3. Preparations

Before you begin, you need to prepare the following items:

  • Activate OSS: Obtain the AccessKey ID and AccessKey secret of your Alibaba Cloud account, and the Internet access endpoint of the specified OSS bucket.
  • Activate Intelligent Speech Interaction: Obtain the AccessKey ID and AccessKey secret of your Alibaba Cloud account, and the appkey of a project.
  • Activate Function Compute.
  • Grant the permission to read and write data in OSS to the Resource Access Management (RAM) role that you specify for Function Compute.

4. Implementation

In Function Compute, you need to perform the following operations:

  1. Create a Function Compute service.
  2. Create a function to generate tasks. It uses an OSS trigger and the callback URL generated in step 3 to generate a recording file recognition task.
  3. Create a function to receive callback event notifications. It uses an HTTP trigger to generate a callback URL. The callback URL is used to write the recording file recognition result back to OSS.

4.1 Create a Function Compute service

In the Function Compute console, create a service. You can configure the advanced settings of the service when creating the service or after the service is created.Create a service

Advanced Settings:Turn on the Advanced Settings switch. In the Role Config section, select an existing RAM role or create a RAM role, select AliyunOSSFullAccess, and then click Authorize.Advanced Settings

Go to the RAM Roles page of the RAM console. Click the existing or created RAM role to which you granted the AliyunOSSFullAccess permission in the last step. On the details page, click Add Permissions to grant the RAM role the AliyunSTSAssumeRoleAccess and AliyunNLSFullAccess permissions. If the RAM user already has the permissions, do not grant them again.Grant permissions to the RAM role

4.2 Create a function to generate tasks

Create a function to generate tasks and send recording file recognition requests. When a recording file is uploaded to OSS, an OSS trigger is triggered to call the function to send a recording file recognition request.

4.2.1 Compile function code

In the Function Compute console, select the created service and create a function to generate tasks as follows:

  1. Function Name: Enter call_filetrans.
  2. Function Handler: Enter call_filetrans.handler.
  3. Runtime: Select python3.
  4. Memory: Select 128MB. Function Compute charges you for the memory. You can select the minimum memory 128 MB, which is sufficient.
  5. Timeout: Enter 600. The unit is seconds.

Copy the following function code to the Code Configuration section and replace the following parameters:

  1. The AccessKey ID and AccessKey secret of your Alibaba Cloud account that has OSS activated, and the Internet access endpoint of the specified OSS bucket.
  2. The AccessKey ID and AccessKey secret of your Alibaba Cloud account that has Intelligent Speech Interaction activated, the appkey of a project, and the callback URL obtained when you create an HTTP trigger.
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import time
  4. from aliyunsdkcore.acs_exception.exceptions import ClientException
  5. from aliyunsdkcore.acs_exception.exceptions import ServerException
  6. from aliyunsdkcore.client import AcsClient
  7. from aliyunsdkcore.auth.credentials import StsTokenCredential
  8. from aliyunsdkcore.request import CommonRequest
  9. import oss2
  10. import logging
  11. ossEndPoint = "Your OSS endpoint" # The Internet access endpoint of the specified OSS bucket.
  12. fileTransAppkey = "Your project appkey" # The appkey of an Intelligent Speech Interaction project.
  13. fileTransCallbackUrl = "Your callback URL" # The callback URL obtained when you create an HTTP trigger.
  14. def handler(event, context):
  15. logger = logging.getLogger()
  16. logger.info(event)
  17. eventObj = json.loads(event)["events"]
  18. eventName=eventObj[0]["eventName"]
  19. bucketName=eventObj[0]["oss"]["bucket"]["name"]
  20. ossFileName=eventObj[0]["oss"]["object"]["key"]
  21. logger.info("eventName: %s" % eventName)
  22. logger.info("bucketName: %s" % bucketName)
  23. logger.info("ossFileName: %s" % ossFileName)
  24. appKey = fileTransAppkey
  25. # You can use the fixed values of the following parameters:
  26. REGION_ID = "cn-shanghai"
  27. PRODUCT = "nls-filetrans"
  28. DOMAIN = "filetrans.cn-shanghai.aliyuncs.com"
  29. API_VERSION = "2018-08-17"
  30. POST_REQUEST_ACTION = "SubmitTask"
  31. GET_REQUEST_ACTION = "GetTaskResult"
  32. KEY_APP_KEY = "appkey"
  33. KEY_FILE_LINK = "file_link"
  34. KET_VERSION = "version"
  35. KEY_TASK = "Task"
  36. KEY_TASK_ID = "TaskId"
  37. KEY_STATUS_TEXT = "StatusText"
  38. creds = context.credentials
  39. # Create an AcsClient instance.
  40. sts_token_credential = StsTokenCredential(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)
  41. client = AcsClient(region_id=REGION_ID, credential=sts_token_credential)
  42. # Create a recording file recognition request and set request parameters.
  43. postRequest = CommonRequest()
  44. postRequest.set_domain(DOMAIN)
  45. postRequest.set_version(API_VERSION)
  46. postRequest.set_product(PRODUCT)
  47. postRequest.set_action_name(POST_REQUEST_ACTION)
  48. postRequest.set_method('POST')
  49. filename = ossFileName.split('/')[-1]
  50. # create file url
  51. auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)
  52. bucket = oss2.Bucket(auth, ossEndPoint, bucketName)
  53. fileLink = bucket.sign_url('GET', ossFileName, 3600)
  54. logger.info("file link = " + fileLink)
  55. # Specify the callback URL obtained when you create an HTTP trigger.
  56. callback_url = fileTransCallbackUrl + "/" + filename
  57. logger.info("callback url = " + callback_url)
  58. task = {"SecurityToken": creds.securityToken, KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KET_VERSION : "4.0", "enable_words" : False, "enable_callback" : True, "callback_url" : callback_url}
  59. task = json.dumps(task)
  60. #logger.info (task)
  61. postRequest.add_body_params(KEY_TASK, task)
  62. taskId = ""
  63. try :
  64. # Send the recording file recognition request and process the response returned by the server.
  65. postResponse = client.do_action_with_exception(postRequest)
  66. postResponse = json.loads(postResponse)
  67. logger.info(postResponse)
  68. # Obtain the ID of the recording file recognition task. You can use the task ID to query the recognition result.
  69. statusText = postResponse[KEY_STATUS_TEXT]
  70. if statusText == "SUCCESS" :
  71. logger.info("The recording file recognition request is successful.")
  72. taskId = postResponse[KEY_TASK_ID]
  73. logger.info("taskId = " + taskId)
  74. else :
  75. logger.info("The recording file recognition request fails.")
  76. except ServerException as e:
  77. logger.error(e)
  78. except ClientException as e:
  79. logger.error(e)
  80. logger.info('hello world')
  81. logger.info(taskId)
  82. return taskId

4.2.2 Configure OSS triggers

Note: Both the PUT and POST methods can be used to upload recording files to the specified OSS bucket. To ensure that all files uploaded can trigger an OSS trigger, you need to create two OSS triggers as follows:

  • Trigger Type: Select Object Storage Service (OSS).
  • Trigger Name: Enter a trigger name. You can customize a trigger name.
  • Events: Select oss:ObjectCreated:PutObject and oss:ObjectCreated:PostObject respectively to create two OSS triggers.
  • Trigger Rule: Prefix: Enter filetrans/raw/. You can enter a custom file directory based on the actual situation.
  • Suffix: Enter .wav. You can enter a custom suffix based on the actual situation.
  • Service Role Management: Select an existing RAM role that is appropriate or create a RAM role.

The following figures show how to create OSS triggers in the Function Compute console.

oss:ObjectCreated:PutObject event:

putobject

oss:ObjectCreated:PostObject event:

postobject

After OSS triggers are created, you can find them on the Function Compute tab of the specified OSS bucket in the OSS console.

4.3 Create a function to receive callback event notifications

Create a function to receive callback event notifications. After a recording file recognition task is completed, an HTTP trigger is triggered to write the recognition result back to the specified OSS bucket.When you create an HTTP trigger, you can obtain a callback URL. You need to specify this callback URL in the function code used to generate tasks.

4.3.1 Compile function code

In the Function Compute console, select the created service and create a function to write recognition results back to OSS as follows:

  • Function Name: Enter put_http_post_to_oss.
  • Function Handler: Enter index.handler.
  • Runtime: Select python3.
  • Memory: Select 128MB. Function Compute charges you for the memory. You can select the minimum memory 128 MB, which is sufficient.
  • Timeout: Enter 600. The unit is seconds.

Copy the following function code to the Code Configuration section and replace the following parameters:

  1. The Internet access endpoint of the specified OSS bucket.
  2. The name of the specified OSS bucket.
  3. The file directory that stores recognition results in the specified OSS bucket.
  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import oss2
  4. import json
  5. endpoint = "Your OSS endpoint" # The Internet access endpoint of the specified OSS bucket.
  6. bucketName = "Your OSS bucket name" # The name of the specified OSS bucket.
  7. resultOssPath = "Your file directory that stores recording file recognition results" # The file directory that stores recognition results in the specified OSS bucket.
  8. B_OK = b"ok"
  9. def handler(environ, start_response):
  10. logger = logging.getLogger()
  11. context = environ['fc.context']
  12. request_uri = environ['fc.request_uri']
  13. for k, v in environ.items():
  14. if k.startswith("HTTP_"):
  15. # process custom request headers
  16. pass
  17. logger.info("request_uri = " + request_uri)
  18. filename = request_uri.split('/')[-1]
  19. logger.info('filename = ' + filename)
  20. # get request_body
  21. try:
  22. request_body_size = int(environ.get('CONTENT_LENGTH', 0))
  23. except (ValueError):
  24. request_body_size = 0
  25. request_body = environ['wsgi.input'].read(request_body_size)
  26. # parse result
  27. postResponse = json.loads(request_body)
  28. taskId = postResponse['TaskId']
  29. statusText = postResponse['StatusText']
  30. # put to oss
  31. creds = context.credentials
  32. auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)
  33. logger.info("creds.accessKeyId = " + creds.accessKeyId)
  34. logger.info("creds.accessKeySecret = " + creds.accessKeySecret)
  35. logger.info("creds.securityToken = " + creds.securityToken)
  36. bucket = oss2.Bucket(auth, endpoint, bucketName)
  37. if statusText == "SUCCESS" :
  38. filename = resultOssPath + "/" + filename + "_" + taskId + '.json'
  39. else :
  40. filename = resultOssPath + "/" + filename + "_" + taskId + '_failed.json'
  41. logger.info('filename = ' + filename)
  42. bucket.put_object(filename, request_body)
  43. # do something here
  44. status = '200 OK'
  45. response_headers = [('Content-type', 'text/plain')]
  46. start_response(status, response_headers)
  47. return [B_OK]

4.3.2 Configure an HTTP trigger

Create an HTTP trigger as follows:

  • Trigger Type: Select HTTP Trigger.
  • Trigger Name: Enter http_post_caller. You can customize a trigger name.
  • Authorization: Select anonymous.
  • Method: Select POST.

After the HTTP trigger is created, a callback URL is automatically generated. You need to specify this callback URL in the function code used to generate tasks.

Create an HTTP trigger

At this point, you have completed the configuration of Function Compute-based recording file recognition. You can then upload recording files to OSS to test and use the recording file recognition service based on Function Compute.