phpgoogle-speech-api

Can't loop over Google Speech Api Response in PHP


I have been messing around with the Google Speech Api in PHP, and I have encountered an issue.

When I am done sending the file it does respond with an array/collection/object of all the data (see the end).

The thing is, it does not let me loop over the response. And I can't figure out how. I just need the plain transcript.

Here is the code:

try {
            $file = $request->file('data');
            $recognitionConfig = new RecognitionConfig();
            $recognitionConfig->setEncoding(AudioEncoding::MP3);
            $recognitionConfig->setSampleRateHertz(44100);
            $recognitionConfig->setModel('default');
            $recognitionConfig->setLanguageCode('en-US');
            $audio = new RecognitionAudio();
            $content = file_get_contents($file);
            $audio->setContent($content);
            $responses = $speechClient->recognize($recognitionConfig, $audio);
            
            foreach ($responses->getResults() as $response) {
                $alternatives = $response->getAlternatives();
                $mostlikely = $alternatives[0];
                $transcript = $mostlikely->getTranscript();
                dd($transcript);
            }
        } finally {
            $speechClient->close();
        }

this is what I get when I die and dump the "$responses->getResults()". The transcript says "Good morning to you"

Google\Protobuf\Internal\RepeatedField {#2257 // app/Http/Controllers/Api/FileController.php:49
  -container: array:1 [
    0 => Google\Cloud\Speech\V1p1beta1\SpeechRecognitionResult {#2265
      -desc: Google\Protobuf\Internal\Descriptor {#2512
        -full_name: "google.cloud.speech.v1p1beta1.SpeechRecognitionResult"
        -field: array:4 [
          1 => Google\Protobuf\Internal\FieldDescriptor {#2529
            -name: "alternatives"
            -json_name: "alternatives"
            -setter: "setAlternatives"
            -getter: "getAlternatives"
            -number: 1
            -label: 3
            -type: 11
            -message_type: Google\Protobuf\Internal\Descriptor {#2527
              -full_name: "google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative"
              -field: array:3 [
                1 => Google\Protobuf\Internal\FieldDescriptor {#2539
                  -name: "transcript"
                  -json_name: "transcript"
                  -setter: "setTranscript"
                  -getter: "getTranscript"
                  -number: 1
                  -label: 1
                  -type: 9
                  -message_type: null
                  -enum_type: null
                  -packed: false
                  -oneof_index: -1
                  -proto3_optional: false
                  -containing_oneof: null
                  -public_desc: Google\Protobuf\FieldDescriptor {#2540
                    -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2539}
                  }
                }
                2 => Google\Protobuf\Internal\FieldDescriptor {#2541
                  -name: "confidence"
                  -json_name: "confidence"
                  -setter: "setConfidence"
                  -getter: "getConfidence"
                  -number: 2
                  -label: 1
                  -type: 2
                  -message_type: null
                  -enum_type: null
                  -packed: false
                  -oneof_index: -1
                  -proto3_optional: false
                  -containing_oneof: null
                  -public_desc: Google\Protobuf\FieldDescriptor {#2542
                    -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2541}
                  }
                }
                3 => Google\Protobuf\Internal\FieldDescriptor {#2543
                  -name: "words"
                  -json_name: "words"
                  -setter: "setWords"
                  -getter: "getWords"
                  -number: 3
                  -label: 3
                  -type: 11
                  -message_type: Google\Protobuf\Internal\Descriptor {#2537
                    -full_name: "google.cloud.speech.v1p1beta1.WordInfo"
                    -field: array:5 [
                      1 => Google\Protobuf\Internal\FieldDescriptor {#2547
                        -name: "start_time"
                        -json_name: "startTime"
                        -setter: "setStartTime"
                        -getter: "getStartTime"
                        -number: 1
                        -label: 1
                        -type: 11
                        -message_type: Google\Protobuf\Internal\Descriptor {#1821
                          -full_name: "google.protobuf.Duration"
                          -field: array:2 [
                            1 => Google\Protobuf\Internal\FieldDescriptor {#1822
                              -name: "seconds"
                              -json_name: "seconds"
                              -setter: "setSeconds"
                              -getter: "getSeconds"
                              -number: 1
                              -label: 1
                              -type: 3
                              -message_type: null
                              -enum_type: null
                              -packed: false
                              -oneof_index: -1
                              -proto3_optional: false
                              -containing_oneof: null
                              -public_desc: Google\Protobuf\FieldDescriptor {#1834
                                -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#1822}
                              }
                            }
                            2 => Google\Protobuf\Internal\FieldDescriptor {#1831
                              -name: "nanos"
                              -json_name: "nanos"
                              -setter: "setNanos"
                              -getter: "getNanos"
                              -number: 2
                              -label: 1
                              -type: 5
                              -message_type: null
                              -enum_type: null
                              -packed: false
                              -oneof_index: -1
                              -proto3_optional: false
                              -containing_oneof: null
                              -public_desc: Google\Protobuf\FieldDescriptor {#1832
                                -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#1831}
                              }
                            }
                          ]
                          -json_to_field: array:2 [
                            "seconds" => Google\Protobuf\Internal\FieldDescriptor {#1822}
                            "nanos" => Google\Protobuf\Internal\FieldDescriptor {#1831}
                          ]
                          -name_to_field: array:2 [
                            "seconds" => Google\Protobuf\Internal\FieldDescriptor {#1822}
                            "nanos" => Google\Protobuf\Internal\FieldDescriptor {#1831}
                          ]
                          -index_to_field: array:2 [
                            0 => Google\Protobuf\Internal\FieldDescriptor {#1822}
                            1 => Google\Protobuf\Internal\FieldDescriptor {#1831}
                          ]
                          -nested_type: []
                          -enum_type: []
                          -klass: "Google\Protobuf\Duration"
                          -legacy_klass: "Google\Protobuf\Duration"
                          -previous_klass: "Google\Protobuf\Duration"
                          -options: null
                          -oneof_decl: []
                          -public_desc: Google\Protobuf\Descriptor {#1829
                            -internal_desc: Google\Protobuf\Internal\Descriptor {#1821}
                          }
                        }
                        -enum_type: null
                        -packed: false
                        -oneof_index: -1
                        -proto3_optional: false
                        -containing_oneof: null
                        -public_desc: Google\Protobuf\FieldDescriptor {#2548
                          -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2547}
                        }
                      }
                      2 => Google\Protobuf\Internal\FieldDescriptor {#2549
                        -name: "end_time"
                        -json_name: "endTime"
                        -setter: "setEndTime"
                        -getter: "getEndTime"
                        -number: 2
                        -label: 1
                        -type: 11
                        -message_type: Google\Protobuf\Internal\Descriptor {#1821}
                        -enum_type: null
                        -packed: false
                        -oneof_index: -1
                        -proto3_optional: false
                        -containing_oneof: null
                        -public_desc: Google\Protobuf\FieldDescriptor {#2550
                          -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2549}
                        }
                      }
                      3 => Google\Protobuf\Internal\FieldDescriptor {#2551
                        -name: "word"
                        -json_name: "word"
                        -setter: "setWord"
                        -getter: "getWord"
                        -number: 3
                        -label: 1
                        -type: 9
                        -message_type: null
                        -enum_type: null
                        -packed: false
                        -oneof_index: -1
                        -proto3_optional: false
                        -containing_oneof: null
                        -public_desc: Google\Protobuf\FieldDescriptor {#2552
                          -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2551}
                        }
                      }
                      4 => Google\Protobuf\Internal\FieldDescriptor {#2553
                        -name: "confidence"
                        -json_name: "confidence"
                        -setter: "setConfidence"
                        -getter: "getConfidence"
                        -number: 4
                        -label: 1
                        -type: 2
                        -message_type: null
                        -enum_type: null
                        -packed: false
                        -oneof_index: -1
                        -proto3_optional: false
                        -containing_oneof: null
                        -public_desc: Google\Protobuf\FieldDescriptor {#2554
                          -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2553}
                        }
                      }
                      5 => Google\Protobuf\Internal\FieldDescriptor {#2555
                        -name: "speaker_tag"
                        -json_name: "speakerTag"
                        -setter: "setSpeakerTag"
                        -getter: "getSpeakerTag"
                        -number: 5
                        -label: 1
                        -type: 5
                        -message_type: null
                        -enum_type: null
                        -packed: false
                        -oneof_index: -1
                        -proto3_optional: false
                        -containing_oneof: null
                        -public_desc: Google\Protobuf\FieldDescriptor {#2556
                          -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2555}
                        }
                      }
                    ]
                    -json_to_field: array:5 [
                      "startTime" => Google\Protobuf\Internal\FieldDescriptor {#2547}
                      "endTime" => Google\Protobuf\Internal\FieldDescriptor {#2549}
                      "word" => Google\Protobuf\Internal\FieldDescriptor {#2551}
                      "confidence" => Google\Protobuf\Internal\FieldDescriptor {#2553}
                      "speakerTag" => Google\Protobuf\Internal\FieldDescriptor {#2555}
                    ]
                    -name_to_field: array:5 [
                      "start_time" => Google\Protobuf\Internal\FieldDescriptor {#2547}
                      "end_time" => Google\Protobuf\Internal\FieldDescriptor {#2549}
                      "word" => Google\Protobuf\Internal\FieldDescriptor {#2551}
                      "confidence" => Google\Protobuf\Internal\FieldDescriptor {#2553}
                      "speaker_tag" => Google\Protobuf\Internal\FieldDescriptor {#2555}
                    ]
                    -index_to_field: array:5 [
                      0 => Google\Protobuf\Internal\FieldDescriptor {#2547}
                      1 => Google\Protobuf\Internal\FieldDescriptor {#2549}
                      2 => Google\Protobuf\Internal\FieldDescriptor {#2551}
                      3 => Google\Protobuf\Internal\FieldDescriptor {#2553}
                      4 => Google\Protobuf\Internal\FieldDescriptor {#2555}
                    ]
                    -nested_type: []
                    -enum_type: []
                    -klass: "Google\Cloud\Speech\V1p1beta1\WordInfo"
                    -legacy_klass: "Google\Cloud\Speech\V1p1beta1\WordInfo"
                    -previous_klass: "Google\Cloud\Speech\V1p1beta1\WordInfo"
                    -options: null
                    -oneof_decl: []
                    -public_desc: Google\Protobuf\Descriptor {#2538
                      -internal_desc: Google\Protobuf\Internal\Descriptor {#2537}
                    }
                  }
                  -enum_type: null
                  -packed: false
                  -oneof_index: -1
                  -proto3_optional: false
                  -containing_oneof: null
                  -public_desc: Google\Protobuf\FieldDescriptor {#2544
                    -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2543}
                  }
                }
              ]
              -json_to_field: array:3 [
                "transcript" => Google\Protobuf\Internal\FieldDescriptor {#2539}
                "confidence" => Google\Protobuf\Internal\FieldDescriptor {#2541}
                "words" => Google\Protobuf\Internal\FieldDescriptor {#2543}
              ]
              -name_to_field: array:3 [
                "transcript" => Google\Protobuf\Internal\FieldDescriptor {#2539}
                "confidence" => Google\Protobuf\Internal\FieldDescriptor {#2541}
                "words" => Google\Protobuf\Internal\FieldDescriptor {#2543}
              ]
              -index_to_field: array:3 [
                0 => Google\Protobuf\Internal\FieldDescriptor {#2539}
                1 => Google\Protobuf\Internal\FieldDescriptor {#2541}
                2 => Google\Protobuf\Internal\FieldDescriptor {#2543}
              ]
              -nested_type: []
              -enum_type: []
              -klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionAlternative"
              -legacy_klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionAlternative"
              -previous_klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionAlternative"
              -options: null
              -oneof_decl: []
              -public_desc: Google\Protobuf\Descriptor {#2528
                -internal_desc: Google\Protobuf\Internal\Descriptor {#2527}
              }
            }
            -enum_type: null
            -packed: false
            -oneof_index: -1
            -proto3_optional: false
            -containing_oneof: null
            -public_desc: Google\Protobuf\FieldDescriptor {#2530
              -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2529}
            }
          }
          2 => Google\Protobuf\Internal\FieldDescriptor {#2531
            -name: "channel_tag"
            -json_name: "channelTag"
            -setter: "setChannelTag"
            -getter: "getChannelTag"
            -number: 2
            -label: 1
            -type: 5
            -message_type: null
            -enum_type: null
            -packed: false
            -oneof_index: -1
            -proto3_optional: false
            -containing_oneof: null
            -public_desc: Google\Protobuf\FieldDescriptor {#2532
              -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2531}
            }
          }
          4 => Google\Protobuf\Internal\FieldDescriptor {#2533
            -name: "result_end_time"
            -json_name: "resultEndTime"
            -setter: "setResultEndTime"
            -getter: "getResultEndTime"
            -number: 4
            -label: 1
            -type: 11
            -message_type: Google\Protobuf\Internal\Descriptor {#1821}
            -enum_type: null
            -packed: false
            -oneof_index: -1
            -proto3_optional: false
            -containing_oneof: null
            -public_desc: Google\Protobuf\FieldDescriptor {#2534
              -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2533}
            }
          }
          5 => Google\Protobuf\Internal\FieldDescriptor {#2535
            -name: "language_code"
            -json_name: "languageCode"
            -setter: "setLanguageCode"
            -getter: "getLanguageCode"
            -number: 5
            -label: 1
            -type: 9
            -message_type: null
            -enum_type: null
            -packed: false
            -oneof_index: -1
            -proto3_optional: false
            -containing_oneof: null
            -public_desc: Google\Protobuf\FieldDescriptor {#2536
              -internal_desc: Google\Protobuf\Internal\FieldDescriptor {#2535}
            }
          }
        ]
        -json_to_field: array:4 [
          "alternatives" => Google\Protobuf\Internal\FieldDescriptor {#2529}
          "channelTag" => Google\Protobuf\Internal\FieldDescriptor {#2531}
          "resultEndTime" => Google\Protobuf\Internal\FieldDescriptor {#2533}
          "languageCode" => Google\Protobuf\Internal\FieldDescriptor {#2535}
        ]
        -name_to_field: array:4 [
          "alternatives" => Google\Protobuf\Internal\FieldDescriptor {#2529}
          "channel_tag" => Google\Protobuf\Internal\FieldDescriptor {#2531}
          "result_end_time" => Google\Protobuf\Internal\FieldDescriptor {#2533}
          "language_code" => Google\Protobuf\Internal\FieldDescriptor {#2535}
        ]
        -index_to_field: array:4 [
          0 => Google\Protobuf\Internal\FieldDescriptor {#2529}
          1 => Google\Protobuf\Internal\FieldDescriptor {#2531}
          2 => Google\Protobuf\Internal\FieldDescriptor {#2533}
          3 => Google\Protobuf\Internal\FieldDescriptor {#2535}
        ]
        -nested_type: []
        -enum_type: []
        -klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionResult"
        -legacy_klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionResult"
        -previous_klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionResult"
        -options: null
        -oneof_decl: []
        -public_desc: Google\Protobuf\Descriptor {#2511
          -internal_desc: Google\Protobuf\Internal\Descriptor {#2512}
        }
      }
      -unknown: ""
      -alternatives: Google\Protobuf\Internal\RepeatedField {#2290
        -container: array:1 [
          0 => Google\Cloud\Speech\V1p1beta1\SpeechRecognitionAlternative {#2261
            -desc: Google\Protobuf\Internal\Descriptor {#2527}
            -unknown: ""
            -transcript: "good morning to you"
            -confidence: 0.97746843099594
            -words: Google\Protobuf\Internal\RepeatedField {#2259
              -container: []
              -type: 11
              -klass: "Google\Cloud\Speech\V1p1beta1\WordInfo"
              -legacy_klass: "Google\Cloud\Speech\V1p1beta1\WordInfo"
            }
          }
        ]
        -type: 11
        -klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionAlternative"
        -legacy_klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionAlternative"
      }
      -channel_tag: 0
      -result_end_time: Google\Protobuf\Duration {#2279
        -desc: Google\Protobuf\Internal\Descriptor {#1821}
        -unknown: ""
        #seconds: 2
        #nanos: 400000000
      }
      -language_code: "en-us"
    }
  ]
  -type: 11
  -klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionResult"
  -legacy_klass: "Google\Cloud\Speech\V1p1beta1\SpeechRecognitionResult"
}

Solution

  • I figured it out.

    The code itself was fine and it works great! I probably didn't speak clearly. And thus it didn't recognize anything.

    Thanks to all for viewing this.