STT (Speech-to-Text) examples
- Print
- PDF
STT (Speech-to-Text) examples
- Print
- PDF
Article summary
Did you find this summary helpful?
Thank you for your feedback
Available in Classic and VPC
This document introduces examples of using the CLOVA Speech Recognition (CSR) STT (Speech-to-Text) API.
STT (Speech-to-Text) API
This section describes examples of the STT (Speech-to-Text) API that takes in language and speech data for speech recognition and converts the recognition results to text.
Java
The following is a Java-based sample code for the STT (Speech-to-Text) API.
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
String clientId = "YOUR_CLIENT_ID"; // Application Client ID";
String clientSecret = "YOUR_CLIENT_SECRET"; // Application Client Secret";
try {
String imgFile = "Voice file path";
File voiceFile = new File(imgFile);
String language = "Kor"; // Language code ( Kor, Jpn, Eng, Chn )
String apiURL = "https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang=" + language;
URL url = new URL(apiURL);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.setRequestProperty("X-NCP-APIGW-API-KEY-ID", clientId);
conn.setRequestProperty("X-NCP-APIGW-API-KEY", clientSecret);
OutputStream outputStream = conn.getOutputStream();
FileInputStream inputStream = new FileInputStream(voiceFile);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
BufferedReader br = null;
int responseCode = conn.getResponseCode();
if(responseCode==200) { // Successful call
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else { // Error occurred
System.out.println("error!!!!!!! responseCode= " + responseCode);
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
}
String inputLine;
if(br != null) {
StringBuffer response = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
br.close();
System.out.println(response.toString());
} else {
System.out.println("error !!!");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Python
The following is a Python-based sample code for the STT (Speech-to-Text) API.
import sys
import requests
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
lang = "Kor" # Language code ( Kor, Jpn, Eng, Chn )
url = "https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang=" + lang
data = open('Voice file path', 'rb')
headers = {
"X-NCP-APIGW-API-KEY-ID": client_id,
"X-NCP-APIGW-API-KEY": client_secret,
"Content-Type": "application/octet-stream"
}
response = requests.post(url, data=data, headers=headers)
rescode = response.status_code
if(rescode == 200):
print (response.text)
else:
print("Error : " + response.text)
PHP
The following is a PHP-based sample code for the STT (Speech-to-Text) API.
<?php
$curl = curl_init();
$file_path = "Voice file path";
$lang = "Kor"; // Language code ( Kor, Jpn, Eng, Chn )
$client_id = "YOUR_CLIENT_KEY";
$client_secret = "YOUR_CLIENT_SECRET";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang=".$lang,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => file_get_contents($file_path),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/octet-stream",
"X-NCP-APIGW-API-KEY-ID: ".$client_id,
"X-NCP-APIGW-API-KEY: ".$client_secret
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
JavaScript
The following is a JavaScript-based sample code for the STT (Speech-to-Text) API.
const fs = require('fs');
const request = require('request');
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
// language => Language code ( Kor, Jpn, Eng, Chn )
function stt(language, filePath) {
const url = `https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang=${language}`;
const requestConfig = {
url: url,
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'X-NCP-APIGW-API-KEY-ID': clientId,
'X-NCP-APIGW-API-KEY': clientSecret
},
body: fs.createReadStream(filePath)
};
request(requestConfig, (err, response, body) => {
if (err) {
console.log(err);
return;
}
console.log(response.statusCode);
console.log(body);
});
}
stt('Kor', 'Voice file path (ex: ./test.wav)');
C#
The following is a C#-based sample code for the STT (Speech-to-Text) API.
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace NaverAPI_Guide
{
class APIExamSTT
{
static void Main(string[] args)
{
string FilePath = "YOUR_FILE_NAME";
FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
byte[] fileData = new byte[fs.Length];
fs.Read(fileData, 0, fileData.Length);
fs.Close();
string lang = "Kor"; // Language code ( Kor, Jpn, Eng, Chn )
string url = $"https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang={lang}";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("X-NCP-APIGW-API-KEY-ID", "YOUR_CLIENT_ID");
request.Headers.Add("X-NCP-APIGW-API-KEY", "YOUR_CLIENT_SECRET");
request.Method = "POST";
request.ContentType = "application/octet-stream";
request.ContentLength = fileData.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileData, 0, fileData.Length);
requestStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string text = reader.ReadToEnd();
stream.Close();
response.Close();
reader.Close();
Console.WriteLine(text);
}
}
}
Was this article helpful?