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', '음성 파일 경로 (ex: ./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.