8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Python]WatsonのSpeech To Textが想像以上に簡単に使えた話

Posted at

#はじめに
 すでにWatsonのSpeech To Textについての投稿はあるけど、Speech To Textの最近の記事がなかったので投稿

やりたいこと

 いわゆる文字起こし。
 議事録の文章化とかにも使えそう

##うれしかったこと

  • 1000分(約16.5時間)まで無料
  • 追加料金も1分2円もしなかったこと

##環境

  • Windows 10
  • Python 3.5.2
  • Watson(Speech To Text)
  • watson-developer-cloud-1.5.0

##WatsonのSpeech To Textって?
 IBMがリリースしているWatsonのSpeech To Textについてはこちらのリンクで概要を理解してもらえれば。
https://qiita.com/saboten10/items/d4b850e0ea2fe4b592c5

##手順

  1. IBM Cloudに登録(2018/07/15現在は無料)
  2. Pythonのインストール(2系と3系でメソッドなどが大きく異なる場所があるため)
  3. 使用する音声を用意(ag デスクトップレコーダーとかYoutubeの動画をffmpegで音声のみにしたり)

##ソース
 このURL(https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/python.html?python#introduction)に必要なソースがJS, Java, Pythonのサンプルコードを載せてるのでそれをコピーする

sample.py
from watson_developer_cloud import SpeechToTextV1
from os.path import join, dirname
import json

speech_to_text = SpeechToTextV1(
    username='あなたのusername',
    password='あなたのpassword'
)

files = ['test.wav']
for file in files:
    with open(join(dirname(__file__), './.', file),
                   'rb') as audio_file:
        speech_recognition_results = speech_to_text.recognize(
            audio=audio_file,
            content_type='audio/wav',
            model='ja-JP_BroadbandModel',
            timestamps=True,
            word_alternatives_threshold=0.9,
            keywords=['test'],
            keywords_threshold=0.5)
    print(json.dumps(speech_recognition_results, indent=2))
    print()
    print('===')
    print()
    print(speech_recognition_results['results'][0]['alternatives'][0]['transcript'])

実際にサンプルで使用されているのは英語版なので日本語対応したソースを載せてます。
パラメータ:model='言語'

##最後に
 ある一定の時間までは無料だし、複数人で登録しておけば無料で結構いけるので、楽してノートを取りたいとかいうときは重宝してます。

##P.S.
 最大どこまでの音声が対応しているか確認したくて60分(約300M)の音声を投げたんですけど、予期せぬエラーになって落ちました笑

sample___.py
Traceback (most recent call last):
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\contrib\pyopenssl.py", line 304, in _send_until_done
    return self.connection.send(data)
  File "D:\ProgramFile\Python352\lib\site-packages\OpenSSL\SSL.py", line 1630, in send
    self._raise_ssl_error(self._ssl, result)
  File "D:\ProgramFile\Python352\lib\site-packages\OpenSSL\SSL.py", line 1539, in _raise_ssl_error
    raise SysCallError(-1, "Unexpected EOF")
OpenSSL.SSL.SysCallError: (-1, 'Unexpected EOF')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 1151, in _send_request
    self.endheaders(body)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 936, in _send_output
    self.send(message_body)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 905, in send
    self.sock.sendall(datablock)
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\contrib\pyopenssl.py", line 316, in sendall
    sent = self._send_until_done(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE])
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\contrib\pyopenssl.py", line 311, in _send_until_done
    raise SocketError(str(e))
OSError: (-1, 'Unexpected EOF')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\ProgramFile\Python352\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\util\retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\packages\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\connectionpool.py", line 357, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 1151, in _send_request
    self.endheaders(body)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 936, in _send_output
    self.send(message_body)
  File "D:\ProgramFile\Python352\lib\http\client.py", line 905, in send
    self.sock.sendall(datablock)
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\contrib\pyopenssl.py", line 316, in sendall
    sent = self._send_until_done(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE])
  File "D:\ProgramFile\Python352\lib\site-packages\urllib3\contrib\pyopenssl.py", line 311, in _send_until_done
    raise SocketError(str(e))
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(-1, 'Unexpected EOF')",))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "sample___.py", line 21, in <module>
    keywords_threshold=0.5)
  File "D:\ProgramFile\Python352\lib\site-packages\watson_developer_cloud\speech_to_text_v1.py", line 388, in recognize
    accept_json=True)
  File "D:\ProgramFile\Python352\lib\site-packages\watson_developer_cloud\watson_service.py", line 445, in request
    **kwargs)
  File "D:\ProgramFile\Python352\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\ProgramFile\Python352\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\ProgramFile\Python352\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "D:\ProgramFile\Python352\lib\site-packages\requests\adapters.py", line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(-1, 'Unexpected EOF')",))

どうか長い音声を使う場合は分けた方が良いかもしれません。

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?