2023-09-13, Wednesday, Cloudy

新工上遇上了挫折。寫好的東西達不到標準需要別人執手尾了。雖然同事說沒關係,但反應了自己的不足。有時反省自己為何做事總是不好,為何別人有留意或想到的事情自己總是忽略。現在覺得是自己天生的思考模式問題,可能看多少書都沒用。或者自己應該更適合做地盤呢類工作,而唔係IT。

Google Speech API with Cantonese

I am trying to convert some Cantonese audio clip into text with the Google Speech API. The example app on https://cloud.google.com/speech/docs/samples was good (I am using Java) and following the instruction I can get the example converting the sample clip into text. But than I get into trouble converting the Cantonese.

Firstly, I can't get it converting Cantonese clip. It just returned blank result. I did two things to make it return some transcript.


  1. Setting the Language Code
        RecognitionConfig config =
            RecognitionConfig.newBuilder()
                .setEncoding(AudioEncoding.LINEAR16)
                .setSampleRate(samplingRate).setLanguageCode("yue-Hant-HK")
                .build();
  2. Using Audacity and record the clip with MONO channel, and export it as RAW type:
    1. File type: Other uncompressed files
    2. Header: RAW (header-less)
    3. Encoding: Signed 16-bit PCM
And finally I got a response like below:
INFO: Received response: results {
  alternatives {
    transcript: "\350\251\246\345\232\207\345\273\243\346\235\261\350\251\261\350\250\273\345\206\212\346\231\202\351\226\223"
    confidence: 0.8150804
  }

}

It looked like a JSON but it wasn't. It was GRPC's response to get the Chinese transcript we need GRPC client.

I modified the AsynClient a bit and make it return the AsyncRecognizeResponse from the recognize method, and use some code like below:

    AsyncRecognizeResponse result = null;
    try {
      result = client.recognize();
    } catch(Exception e){
    e.printStackTrace();
    }finally {
      client.shutdown();
    }
    result.getResultsCount();
    List rresult = result.getResultsList();
   
    for(SpeechRecognitionResult srr:rresult){
    SpeechRecognitionAlternative alternativesStr = srr.getAlternatives(0);
    String transcriptStr= alternativesStr.getTranscript();
    System.out.println(transcriptStr);

    }
And the transcriptStr contains the correct Chinese characters now.

Comments

Popular posts from this blog

Install Apache Superset on Ubuntu 18.04

生的感覺、死的感覺

2023-03-18, Saturday, Sunny