--- title: "リアルタイムストリーミングの認識" slug: "ai-application-service-clovaspeech-example02" updated: 2026-09-17T09:57:17Z published: 2026-09-17T09:57:17Z canonical: "api.ncloud-docs.com/ai-application-service-clovaspeech-example02" --- > ## Documentation Index > Fetch the complete documentation index at: https://api.ncloud-docs.com/llms.txt > Use this file to discover all available pages before exploring further. # リアルタイムストリーミングの認識 Classic/VPC環境で利用できます。 CLOVA Speechサービスのリアルタイムストリーミングのユースケースを紹介します。 ## Java Javaベースの APIのサンプルコードは次の通りです。 - Project Structure ``` ├───pom.xml │ │ └───src │ ├───main │ │ ├───java │ │ │ └───com │ │ │ └───example │ │ │ └───grpc │ │ │ GRpcClient.java │ │ │ │ │ ├───proto │ │ │ nest.proto ``` - pom.xml ``` 4.0.0 com.example clova-speech-grpc 1.0-SNAPSHOT 1.8 ${java.version} ${java.version} UTF-8 UTF-8 4.1.52.Final 1.35.0 3.14.0 io.grpc grpc-netty ${grpc.version} io.grpc grpc-netty-shaded ${grpc.version} io.grpc grpc-protobuf ${grpc.version} io.grpc grpc-stub ${grpc.version} org.projectlombok lombok true 1.18.12 kr.motd.maven os-maven-plugin 1.6.1 org.apache.maven.plugins maven-compiler-plugin 3.1 compile compile compile testCompile test-compile testCompile true ${project.build.sourceEncoding} org.xolstice.maven.plugins protobuf-maven-plugin 0.6.1 com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} grpc-java io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} compile compile-custom ``` - Java ``` package com.example.grpc; import java.io.FileInputStream; import java.util.concurrent.CountDownLatch; import com.google.protobuf.ByteString; import com.nbp.cdncp.nest.grpc.proto.v1.NestConfig; import com.nbp.cdncp.nest.grpc.proto.v1.NestData; import com.nbp.cdncp.nest.grpc.proto.v1.NestRequest; import com.nbp.cdncp.nest.grpc.proto.v1.NestResponse; import com.nbp.cdncp.nest.grpc.proto.v1.NestServiceGrpc; import com.nbp.cdncp.nest.grpc.proto.v1.RequestType; import io.grpc.ManagedChannel; import io.grpc.Metadata; import io.grpc.StatusRuntimeException; import io.grpc.netty.NettyChannelBuilder; import io.grpc.stub.MetadataUtils; import io.grpc.stub.StreamObserver; public class GRpcClient { public static void main(String[] args) throws Exception { CountDownLatch latch = new CountDownLatch(1); ManagedChannel channel = NettyChannelBuilder .forTarget("clovaspeech-gw.ncloud.com:50051") .useTransportSecurity() .build(); NestServiceGrpc.NestServiceStub client = NestServiceGrpc.newStub(channel); Metadata metadata = new Metadata(); metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), "Bearer ${secretKey}"); client = MetadataUtils.attachHeaders(client, metadata); StreamObserver responseObserver = new StreamObserver() { @Override public void onNext(NestResponse response) { System.out.println("Received response: " + response.getContents()); } @Override public void onError(Throwable t) { if(t instanceof StatusRuntimeException) { StatusRuntimeException error = (StatusRuntimeException)t; System.out.println(error.getStatus().getDescription()); } latch.countDown(); } @Override public void onCompleted() { System.out.println("completed"); latch.countDown(); } }; StreamObserver requestObserver = client.recognize(responseObserver); requestObserver.onNext(NestRequest.newBuilder() .setType(RequestType.CONFIG) .setConfig(NestConfig.newBuilder() .setConfig("{\"transcription\":{\"language\":\"ko\"}}") .build()) .build()); java.io.File file = new java.io.File("~/media/42s.wav"); byte[] buffer = new byte[32000]; int bytesRead; FileInputStream inputStream = new FileInputStream(file); while ((bytesRead = inputStream.read(buffer)) != -1) { requestObserver.onNext(NestRequest.newBuilder() .setType(RequestType.DATA) .setData(NestData.newBuilder() .setChunk(ByteString.copyFrom(buffer, 0, bytesRead)) .setExtraContents("{ \"seqId\": 0, \"epFlag\": false}") .build()) .build()); } requestObserver.onCompleted(); latch.await(); channel.shutdown(); } } ``` ## Python Pythonベースの APIのサンプルコードは次の通りです。 ``` import grpc import json import nest_pb2 import nest_pb2_grpc AUDIO_PATH = "path/to/audio/file" #認識対象のオーディオファイルがあるパスを入力してください。(16kHz、1channel、16 bits per sampleの PCM(ヘッダがない raw wave)形式) CLIENT_SECRET = "長文認識 secretKey" def generate_requests(audio_path): # 初期設定リクエスト: 音声認識設定 yield nest_pb2.NestRequest( type=nest_pb2.RequestType.CONFIG, config=nest_pb2.NestConfig( config=json.dumps({"transcription": {"language": "ko"}}) ) ) # オーディオファイルを開いて32,000バイトずつ読み取る with open(audio_path, "rb") as audio_file: while True: chunk = audio_file.read(32000) # オーディオファイルのチャンクを読み取る if not chunk: break # データがなくなったらループ終了 yield nest_pb2.NestRequest( type=nest_pb2.RequestType.DATA, data=nest_pb2.NestData( chunk=chunk, extra_contents=json.dumps({"seqId": 0, "epFlag": False}) ) ) def main(): # Clova Speechサーバに対するセキュリティ gRPCチャンネルを設定 channel = grpc.secure_channel( "clovaspeech-gw.ncloud.com:50051", grpc.ssl_channel_credentials() ) stub = nest_pb2_grpc.NestServiceStub(channel) # NestServiceの stubを作成 metadata = (("authorization", f"Bearer {CLIENT_SECRET}"),) # 認証トークンと一緒にメタデータを設定 responses = stub.recognize(generate_requests(AUDIO_PATH), metadata=metadata) # 作成されたリクエストで認識(recognize)メソッドを呼び出す try: # サーバからのレスポンスを繰り返し処理 for response in responses: print("Received response: " + response.contents) except grpc.RpcError as e: # gRPCエラー処理 print(f"Error: {e.details()}") finally: channel.close() # 処理が終わったらチャンネルを閉じる if __name__ == "__main__": main() ```