質問編集履歴
2
pythonのコメントアウト記号を使うと挙動がおかしくなるため、「-」を使用しています
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,35 +18,35 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
-
|
21
|
+
- Copyright 2017 Google Inc. All Rights Reserved.-
|
22
|
-
|
22
|
+
|
23
|
-
|
23
|
+
- -
|
24
|
-
|
24
|
+
|
25
|
-
|
25
|
+
- Licensed under the Apache License, Version 2.0 (the "License");-
|
26
|
-
|
26
|
+
|
27
|
-
|
27
|
+
- you may not use this file except in compliance with the License.-
|
28
|
-
|
28
|
+
|
29
|
-
|
29
|
+
- You may obtain a copy of the License at-
|
30
|
-
|
30
|
+
|
31
|
-
|
31
|
+
-
|
32
|
-
|
32
|
+
|
33
|
-
|
33
|
+
- http://www.apache.org/licenses/LICENSE-2.0-
|
34
|
-
|
34
|
+
|
35
|
-
|
35
|
+
--
|
36
|
-
|
36
|
+
|
37
|
-
|
37
|
+
- Unless required by applicable law or agreed to in writing, software-
|
38
|
-
|
38
|
+
|
39
|
-
|
39
|
+
- distributed under the License is distributed on an "AS IS" BASIS,-
|
40
|
-
|
40
|
+
|
41
|
-
|
41
|
+
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-
|
42
|
-
|
42
|
+
|
43
|
-
|
43
|
+
- See the License for the specific language governing permissions and-
|
44
|
-
|
44
|
+
|
45
|
-
|
45
|
+
- limitations under the License.-
|
46
|
-
|
47
|
-
|
48
|
-
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
-
|
49
|
+
---Google Cloud Speech API sample application using the REST API for async
|
50
50
|
|
51
51
|
batch processing.
|
52
52
|
|
@@ -58,7 +58,7 @@
|
|
58
58
|
|
59
59
|
python transcribe_async.py gs://cloud-samples-tests/speech/brooklyn.flac
|
60
60
|
|
61
|
-
|
61
|
+
---
|
62
62
|
|
63
63
|
|
64
64
|
|
@@ -74,7 +74,7 @@
|
|
74
74
|
|
75
75
|
def transcribe_file(speech_file):
|
76
76
|
|
77
|
-
|
77
|
+
---Transcribe the given audio file asynchronously.---
|
78
78
|
|
79
79
|
from google.cloud import speech
|
80
80
|
|
@@ -126,7 +126,7 @@
|
|
126
126
|
|
127
127
|
print('Confidence: {}'.format(alternative.confidence))
|
128
128
|
|
129
|
-
|
129
|
+
- [END send_request] -
|
130
130
|
|
131
131
|
|
132
132
|
|
@@ -134,7 +134,7 @@
|
|
134
134
|
|
135
135
|
def transcribe_gcs(gcs_uri):
|
136
136
|
|
137
|
-
|
137
|
+
---Asynchronously transcribes the audio file specified by the gcs_uri.---
|
138
138
|
|
139
139
|
from google.cloud import speech
|
140
140
|
|
@@ -186,7 +186,7 @@
|
|
186
186
|
|
187
187
|
print('Confidence: {}'.format(alternative.confidence))
|
188
188
|
|
189
|
-
|
189
|
+
-[END send_request_gcs]-
|
190
190
|
|
191
191
|
|
192
192
|
|
1
ソースコードを付けたしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,10 +4,212 @@
|
|
4
4
|
|
5
5
|
出力に、各単語が音声ファイル内のどの時間に発話されたものであるかを知りたいのですが、どのように行えばいいかわかりません、お詳しい方宜しくお願いします。
|
6
6
|
|
7
|
+
|
8
|
+
|
9
|
+
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/cloud-client/transcribe_async.py
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
上記のコードにマイナーチェンジを行ったもので出力を行っています。ここにどのようにコードを足せば、時間つきのファイルになるのかを知りたいです
|
14
|
+
|
7
15
|
###該当のソースコード
|
8
16
|
|
17
|
+
#!/usr/bin/env python
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
# Copyright 2017 Google Inc. All Rights Reserved.
|
22
|
+
|
23
|
+
#
|
24
|
+
|
25
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
26
|
+
|
27
|
+
# you may not use this file except in compliance with the License.
|
28
|
+
|
29
|
+
# You may obtain a copy of the License at
|
30
|
+
|
31
|
+
#
|
32
|
+
|
33
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
34
|
+
|
35
|
+
#
|
36
|
+
|
37
|
+
# Unless required by applicable law or agreed to in writing, software
|
38
|
+
|
39
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
40
|
+
|
41
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
42
|
+
|
43
|
+
# See the License for the specific language governing permissions and
|
44
|
+
|
45
|
+
# limitations under the License.
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
"""Google Cloud Speech API sample application using the REST API for async
|
50
|
+
|
51
|
+
batch processing.
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
Example usage:
|
56
|
+
|
57
|
+
python transcribe_async.py resources/audio.raw
|
58
|
+
|
59
|
+
python transcribe_async.py gs://cloud-samples-tests/speech/brooklyn.flac
|
60
|
+
|
61
|
+
"""
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
import argparse
|
66
|
+
|
67
|
+
import io
|
68
|
+
|
69
|
+
import time
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def transcribe_file(speech_file):
|
76
|
+
|
77
|
+
"""Transcribe the given audio file asynchronously."""
|
78
|
+
|
79
|
+
from google.cloud import speech
|
80
|
+
|
81
|
+
speech_client = speech.Client()
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
with io.open(speech_file, 'rb') as audio_file:
|
86
|
+
|
87
|
+
content = audio_file.read()
|
88
|
+
|
89
|
+
audio_sample = speech_client.sample(
|
90
|
+
|
91
|
+
content,
|
92
|
+
|
93
|
+
source_uri=None,
|
94
|
+
|
95
|
+
encoding='FLAC',
|
96
|
+
|
97
|
+
sample_rate=16000)
|
98
|
+
|
99
|
+
|
100
|
+
|
9
|
-
|
101
|
+
operation = speech_client.speech_api.async_recognize(audio_sample, language_code='en-GB', speech_context=['<登録した単語>'], retry_count = 100
|
102
|
+
|
10
|
-
|
103
|
+
while retry_count > 0 and not operation.complete:
|
104
|
+
|
11
|
-
|
105
|
+
retry_count -= 1
|
106
|
+
|
12
|
-
|
107
|
+
time.sleep(2)
|
108
|
+
|
109
|
+
operation.poll()
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
if not operation.complete:
|
114
|
+
|
13
|
-
|
115
|
+
print('Operation not complete and retry limit reached.')
|
116
|
+
|
117
|
+
return
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
alternatives = operation.results
|
122
|
+
|
123
|
+
for alternative in alternatives:
|
124
|
+
|
125
|
+
print('Transcript: {}'.format(alternative.transcript))
|
126
|
+
|
127
|
+
print('Confidence: {}'.format(alternative.confidence))
|
128
|
+
|
129
|
+
# [END send_request]
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def transcribe_gcs(gcs_uri):
|
136
|
+
|
137
|
+
"""Asynchronously transcribes the audio file specified by the gcs_uri."""
|
138
|
+
|
139
|
+
from google.cloud import speech
|
140
|
+
|
141
|
+
speech_client = speech.Client()
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
audio_sample = speech_client.sample(
|
146
|
+
|
147
|
+
content=None,
|
148
|
+
|
149
|
+
source_uri=gcs_uri,
|
150
|
+
|
151
|
+
encoding='FLAC',
|
152
|
+
|
153
|
+
sample_rate=16000)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
operation = speech_client.speech_api.async_recognize(audio_sample)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
retry_count = 100
|
162
|
+
|
163
|
+
while retry_count > 0 and not operation.complete:
|
164
|
+
|
165
|
+
retry_count -= 1
|
166
|
+
|
167
|
+
time.sleep(2)
|
168
|
+
|
169
|
+
operation.poll()
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
if not operation.complete:
|
174
|
+
|
175
|
+
print('Operation not complete and retry limit reached.')
|
176
|
+
|
177
|
+
return
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
alternatives = operation.results
|
182
|
+
|
183
|
+
for alternative in alternatives:
|
184
|
+
|
185
|
+
print('Transcript: {}'.format(alternative.transcript))
|
186
|
+
|
187
|
+
print('Confidence: {}'.format(alternative.confidence))
|
188
|
+
|
189
|
+
# [END send_request_gcs]
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
if __name__ == '__main__':
|
196
|
+
|
197
|
+
parser = argparse.ArgumentParser(
|
198
|
+
|
199
|
+
description=__doc__,
|
200
|
+
|
201
|
+
formatter_class=argparse.RawDescriptionHelpFormatter)
|
202
|
+
|
203
|
+
parser.add_argument(
|
204
|
+
|
205
|
+
'path', help='File or GCS path for audio file to be recognized')
|
206
|
+
|
207
|
+
args = parser.parse_args()
|
208
|
+
|
209
|
+
if args.path.startswith('gs://'):
|
210
|
+
|
211
|
+
transcribe_gcs(args.path)
|
212
|
+
|
213
|
+
else:
|
214
|
+
|
215
|
+
transcribe_file(args.path)
|