📜 ⬆️ ⬇️

Speech Recognition on Bash

Hello, Dear $ USERNAME.

Small introduction


In this article, I would like to tell you about voice recognition, written in a programming language such as Bash. As OC, I chose Ubuntu 12.04

Closer to the topic


Voice recognition will be provided by Google, which is used in Google Chrome for voice search. Let's get started

Work algorithm

  1. Record the audio file speech.wav with a length of 3 seconds with a 16 MHz rate
  2. Convert speech.wav to speech.flac (only this format can receive Google)
  3. Send Google speech.flac
  4. We receive the answer of a type:
    {"status":0,"id":"37h03bf4efe17fa76594732d6dokf3-1","hypotheses":[{"utterance":" ","confidence":0.75936891}]}
    one two three - recognized text
    0.75936891 - recognition accuracy (if this value is greater than 0.5, then recognition is considered reliable)
  5. We isolate the values ​​of utterance and confidence into separate variables and display them on the screen.

')
Implementation

  1. Create a speech.sh file with the following content (code with comments):
     #!/bin/bash echo " ..." arecord -d 3 -q -f cd -r 16000 speech.wav #    speech.wav   3    16  echo " " sox speech.wav speech.flac gain -n -5 silence 1 5 2% #  speech.wav  speech.flac rm speech.wav #  speech.wav, ..      echo " ..." wget -q -U "Mozilla/5.0" --post-file speech.flac --header="Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=ru-RU&client=chromium" > all.ret #  Google speech.flac       all.ret rm speech.flac #  speech.flac, ..      cat all.ret | sed 's/.*utterance":"//' | sed 's/","confidence.*//' > text.txt #   utterance   text.txt cat all.ret | sed 's/.*confidence"://' | sed 's/}]}.*//' > confidence.txt #   confidence   confidence.txt rm all.ret #  all.ret, ..      TEXT="$(cat text.txt)" #  TEXT    text.txt CONFIDENCE="$(cat confidence.txt)" #  CONFIDENCE    confidence.txt rm text.txt #  text.txt, ..      rm confidence.txt #  confidence.txt, ..      echo $TEXT #    TEXT echo $CONFIDENCE #    CONFIDENCE 
  2. We download the necessary packages via the console:
    sudo apt-get install lame
    sudo apt-get install sox
  3. Install the rights to run through the console to the speech.sh file:
    sudo chmod 755 speech.sh


Well, now you can run our script and test its performance using the console command:
./speech.sh

How it all looks from me:


Total


And so, to summarize: voice recognition is not such an “unreality”. A skilled person with straight arms can find useful applications. For example, based on it, I create a “Smart Home” with voice control.

See you soon, $ USERNAME.

Source: https://habr.com/ru/post/157333/


All Articles