Easy introduction of speech recognition with Python

Voice to TXT conversion

import speech_recognition as sr
import soundfile as sf
from io import BytesIO
import os

r = sr.Recognizer()

def analyze_wav(wavfile):
    try:
        adata, samplerate = sf.read(BytesIO(wavfile.read()))

        newpath = "new_file.wav"

        path_tmp = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")

        if not os.path.exists(path_tmp):
            os.mkdir(path_tmp)

        savepath = os.path.join(path_tmp, newpath)
        sf.write(savepath, adata, samplerate)

        with sr.AudioFile(savepath) as src:
            audio = r.record(src)
        txt = r.recognize_google(audio, language='ja-JP')

        return txt
    except:
        import traceback
        traceback.print_exc()

When using on a server (Flask)

from flask import Flask, request, abort

import speech_recognition as sr
import soundfile as sf
from io import BytesIO
import os

r = sr.Recognizer()

@app.route("/webapp", methods=['POST'])
def webapp():
    wavdata = request.files["file"].stream
    txt = analyze_wav(wavdata)
    return txt

def analyze_wav(wavfile):
    try:
        adata, samplerate = sf.read(BytesIO(wavfile.read()))

        newpath = "new_file.wav"

        path_tmp = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tmp")

        if not os.path.exists(path_tmp):
            os.mkdir(path_tmp)

        savepath = os.path.join(path_tmp, newpath)
        sf.write(savepath, adata, samplerate)

        with sr.AudioFile(savepath) as src:
            audio = r.record(src)
        txt = r.recognize_google(audio, language='ja-JP')

        return txt
    except:
        import traceback
        traceback.print_exc()

Recommended Posts

Easy introduction of speech recognition with Python
Introduction of Python
Introduction of Python
English speech recognition with python [speech to text]
Easy introduction of python3 series and OpenCV3
Speech recognition in Python
[Python] Easy introduction to machine learning with python (SVM)
Speech recognition by Python MFCC
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Easy partial download of mp4 with python and youtube-dl!
[Chapter 5] Introduction to Python with 100 knocks of language processing
Easy folder synchronization with Python
Introduction of activities applying Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
Easy Python compilation with NUITKA-Utilities
[Chapter 2] Introduction to Python with 100 knocks of language processing
Easy HTTP server with Python
Try face recognition with Python
[Chapter 4] Introduction to Python with 100 knocks of language processing
Speech recognition of wav files with Google Cloud Speech API Beta
[Raspi4; Introduction to Sound] Stable recording of sound input with python ♪
[Python] Easy reading of serial number image files with OpenCV
Number recognition in images with Python
[Python] Easy parallel processing with Joblib
Easy encryption of file contents (Python)
Getting Started with Python Basics of Python
Hadoop introduction and MapReduce with Python
Easy Python + OpenCV programming with Canopy
Easy email sending with haste python3
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
Try face recognition with python + OpenCV
Bayesian optimization very easy with Python
Face recognition with camera with opencv3 + python2.7
Use Windows 10 speech synthesis with Python
Introduction of python drawing package pygal
Easy data visualization with Python seaborn.
Implementation of Dijkstra's algorithm with python
Easy parallel execution with python subprocess
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Record of Python introduction for newcomers
Basic study of OpenCV with Python
[Python Tutorial] An Easy Introduction to Python
IPynb scoring system made with TA of Introduction to Programming (Python)
Python x Flask x PyTorch Easy construction of number recognition web application
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
[Examples of improving Python] Learning Python with Codecademy
[Python] Super easy test with assert statement
[Python] Easy argument type check with dataclass
Streaming speech recognition with Google Cloud Speech API
General Theory of Relativity in Python: Introduction
Execute Python script with cron of TS-220
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Algorithm learned with Python 8th: Evaluation of algorithm
Python: Application of image recognition using CNN
[Easy] AI automatic recognition with a webcam!
Clogged with python update of GCP console ①
[EDA] Introduction of Sweetviz (comparison with + pandas-profiling)