Google speech to text API in C#

前端 未结 4 2069
遇见更好的自我
遇见更好的自我 2020-12-10 08:34

My program get a correct respon from google when the flac file recorded manual by using windows\'s sound recorder and convert it using a software converter.
But when I

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 08:57

    It's clearly mentioned in the Google cloud api url i.e

    https://cloud.google.com/speech-to-text/docs/async-recognize#speech-async-recognize-gcs-protocol

    If the operation has not completed, you can poll the endpoint by repeatedly making the GET request until the done property of the response is true.

            {
          "name": "operationname here",
          "metadata": {
            "@type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata",
            "progressPercent": 0,
            "startTime": "2018-12-18T10:56:09.425584Z",
            "lastUpdateTime": "2018-12-18T11:10:27.147310Z"
          },
          "done": true,
        }
    

    poll the endpoint by repeatedly making the GET request until the done property of the response is true or you can check for the "progressPercent": 0 until it's value become 100. Once its 100 percent then done property becomes true.

    I did the same in my code using operation name, for reference here is the code

    public async Task TranscribeLongMediaFile(string operationName)
        {
            string bearerToken = GetOAuthToken();
            var baseUrl = new Uri(googleSpeechBaseUrl + operationName);
            string resultContent = string.Empty;
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add(HttpRequestHeader.Authorization.ToString(), "Bearer " + bearerToken);
                client.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/json; charset=utf-8");
    
                client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
    
                int currentPercentage = 0;
                bool responseStatus = false;
                while (!responseStatus)
                {
                    responseStatus = false;
                    // Send request
                    using (var result = await client.GetAsync(baseUrl))
                    {
                        resultContent = await result.Content.ReadAsStringAsync();
    
                        ResponseObject responseObject = JsonConvert.DeserializeObject(resultContent);
                        currentPercentage = responseObject.metadata.progressPercent;
                        responseStatus = (responseObject.done && currentPercentage == 100);
    
                        // Delay the request based on percentage value to repeatedly making the GET request until the done property of the response is true.
                        await Task.Delay(CalculateDealy(currentPercentage));
                    }
                }
            };
            return resultContent;
        }
    

    In order to delay the get request:

    /// 
        /// Delay the request to number of milliseconds
        /// 
        /// 
        /// 
        private int CalculateDealy(int currentPercentage)
        {
            int x = currentPercentage / 10;
            return (10 - x) * 1500;
        }
    

    Get auth token:

    /// 
        /// Get OAuth token
        /// 
        /// 
        public string GetOAuthToken()
        {
            return googleCredential.UnderlyingCredential.GetAccessTokenForRequestAsync("https://accounts.google.com/o/oauth2/v2/auth", CancellationToken.None).Result;
        }
    

    At last, you will get the result like:

        {
      "name": "operationname here",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata",
        "progressPercent": 100,
        "startTime": "2018-12-18T10:56:09.425584Z",
        "lastUpdateTime": "2018-12-18T11:10:27.147310Z"
      },
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse",
        "results": [
          {
            "alternatives": [
              {
                "transcript": "okay let's get started",
                "confidence": 0.97442055
              }
            ]
          }, and so on .....
    

    Things required:

    1. api-key.json file
    2. Install package Google.Apis.Auth.OAuth2 in order to authorize the HTTP web request

    Thanks

提交回复
热议问题