Speech recognition in Python

Introduction

I always want to lower the threshold for programming and CG production, but Maya also wants to move with voice recognition, so I'm doing a lot of research.

This article hasn't gotten to the point of working with Maya.

It's just a test of the Microsoft Speech API that comes standard with Windows.

procedure

Windows10 Python 2.7.12 pywin32==220

  1. python -m virtualenv venv
  2. venv\Scripts\activate.bat
  3. Install win32com
  4. Maybe you have to install Speech SDK
  5. Convert Microsoft Speech Object Library 5.4 to python code with python venv \ Lib \ site-packages \ win32com \ client \ makepy.py

When the following program is executed, it will wait for voice input with the message "Ready", and the character string will be output to the standard output in response to the voices "One", "Two", "Three", "Four", and "Hello".

main.py


#!/usr/bin/env python
# coding=utf-8

from __future__ import absolute_import, division, print_function

from win32com.client import constants
import win32com.client
import pythoncom


class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
    u"""Callback class called when a phrase is recognized

Register words in advance.
    """
    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print(u"You said: {0}".format(newResult.PhraseInfo.GetText()))


class SpeechRecognition(object):
    def __init__(self, wordsToAdd):
        u"""Various initialization"""
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")

        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
        self.context = self.listener.CreateRecoContext()
        self.grammar = self.context.CreateGrammar()
        self.grammar.DictationSetState(0)

        self.wordsRule = self.grammar.Rules.Add(
            "wordsRule",
            constants.SRATopLevel + constants.SRADynamic, 0)
        self.wordsRule.Clear()
        for word in wordsToAdd:
            self.wordsRule.InitialState.AddWordTransition(None, word)
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)
        self.grammar.Rules.Commit()
        self.eventHandler = ContextEvents(self.context)

        self.say(u"Junbikanryo")

    def say(self, phrase):
        u"""Let me talk"""
        self.speaker.Speak(phrase)


if __name__ == '__main__':
    wordsToAdd = ["One", "Two", "Three", "Four", u"Hello"]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

I'm sorry regardless of Maya.

reference

[Play COM with Python + pywin32 to talk. ](Https://mimumimu.net/blog/2011/07/02/python-pywin32-%E3%81%A7-com-%E5%8F%A9%E3%81%84%E3%81%A6% E3% 81% 97% E3% 82% 83% E3% 81% B9% E3% 82% 89% E3% 81% 9B% E3% 82% 8B% E3% 80% 82 /) Python: win32com.client.getevents(“SAPI.SpSharedRecoContext”) returns None

Recommended Posts

Speech recognition in Python
Speech recognition by Python MFCC
Speech to speech in python [text to speech]
Number recognition in images with Python
Handwriting recognition using KNN in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Discord in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
English speech recognition with python [speech to text]
Easy introduction of speech recognition with Python
Traffic Safety-kun: Recognition of traffic signs in Python
Scene recognition by GIST features in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python