STT(Speech-to-Text)のユースケース
    • PDF

    STT(Speech-to-Text)のユースケース

    • PDF

    記事の要約

    Classic/VPC環境で利用できます。

    CLOVA Speech Recognition(CSR) STT(Speech-to-Text) APIを使用するユースケースを紹介します。

    STT (Speech-to-Text) API

    音声認識に使用する言語と音声データの入力を受け、その認識結果をテキストに変換する STT(Speech-to-Text) APIのユースケースを説明します。

    Java

    Javaベースの 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 = "音声ファイルのパス";
                File voiceFile = new File(imgFile);
    
                String language = "Kor";        // 言語コード(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) { // 正常な呼び出し
                    br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                } else {  // エラー発生
                    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

    Pythonベースの STT(Speech-to-Text) APIのサンプルコードは次の通りです。

    import sys
    import requests
    client_id = "YOUR_CLIENT_ID"
    client_secret = "YOUR_CLIENT_SECRET"
    lang = "Kor" # 言語コード(Kor、Jpn、Eng、Chn)
    url = "https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang=" + lang
    data = open('言語ファイルのパス', '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

    PHPベースの STT(Speech-to-Text) APIのサンプルコードは次の通りです。

    <?php
    
    $curl = curl_init();
    $file_path = "音声ファイルのパス";
    
    $lang = "Kor";  // 言語コード(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

    JavaScriptベースの STT(Speech-to-Text) APIのサンプルコードは次の通りです。

    const fs = require('fs');
    const request = require('request');
    
    const clientId = 'YOUR_CLIENT_ID';
    const clientSecret = 'YOUR_CLIENT_SECRET';
    
    // language => 言語コード(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', '音声ファイルのパス(例: ./test.wav)');
    

    C#

    C# ベースの 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";    // 言語コード(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);
            }
        }
    }
    

    この記事は役に立ちましたか?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.