Tuesday 25 July 2017

Convert Speech to Text- Nodejs



Hi There ,
I am going to tell you how to convert Audio/Speech/Mp3 and other audio file into Text format.

I will use Nodejs, Bluemix (IBM Cloud Plateform).So let's start.

You should follow below steps to the same:

Step 1: Register on Bluemix (IBM Cloud Plateform).
Step 2: Login on Bluemix.
Step 3: Crete a service for speech-to-text. And get the username and password for speech-to-text.  Link : Create service for speech to text

Step 4:  Start coding.
Step 5:  create a .js file and require speech-to-text and fs just like below code.

 
var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');



Step 6: Create an object of SpeechToText just like below code.



var speech_to_text = new SpeechToTextV1({
username: '1234567-8765-4267-9e76-fgff34f',
password: 'ABCdefghiJK'
});

Step 7: Create an array and insert the path of the audio. You can have many audio file in this array.

 
var files = ['./music/hello.flac','./music/somebody2010.flac'];



 Step 8: Create params for every audio, So we will do it in for loop and call speech_to_text.recognize() to convert.Get the response and console it.


for (var file in files) {
var params = {
audio: fs.createReadStream(files[file]),
content_type: 'audio/flac',
timestamps: true,
word_alternatives_threshold: 0.9,
keywords: ['colorado', 'tornado', 'tornadoes'],
keywords_threshold: 0.5
};

speech_to_text.recognize(params, function (error, transcript) {
if (error)
console.log('Error:', error);
else{
console.log(JSON.stringify(transcript, null, 2));
console.log(transcript.results[0].alternatives[0].transcript);
}
});
}

 Congratulation ! You did it.




The complete code is here :



 
 
var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');

var speech_to_text = new SpeechToTextV1({
username: '1234567-8765-4267-9e76-fgff34f',
password: 'ABCdefghiJK'
});

var files = ['./music/hello.flac','./music/somebody2010.flac'];

for (var file in files) {
var params = {
audio: fs.createReadStream(files[file]),
content_type: 'audio/flac',
timestamps: true,
word_alternatives_threshold: 0.9,
keywords: ['colorado', 'tornado', 'tornadoes'],
keywords_threshold: 0.5
};

speech_to_text.recognize(params, function (error, transcript) {
if (error)
console.log('Error:', error);
else
console.log(transcript.results[0].alternatives[0].transcript);
});
}
 
 




HOW TO RUN ABOVE PROGRAM:

/***********************************************************************************
 INSTRUCTIONS:

Step 0: Check the environment setup. (Should installed nodejs,Remove all errors if any).
Step 1: Navigate terminal/command prompt to this directory.
Step 2: Run command:  node FILE_NAME.js;
Step 3: See the message  in console log;
Step 4: if executed Step 3 successfully then you have text message according to audio. Otherwise go to Step 0.

**************************************************************************************/





No comments:

Post a Comment