실시간 스트리밍 인식
- 인쇄
- PDF
실시간 스트리밍 인식
- 인쇄
- PDF
기사 요약
이 요약이 도움이 되었나요?
의견을 보내 주셔서 감사합니다.
Classic/VPC 환경에서 이용 가능합니다.
CLOVA Speech 서비스의 실시간 스트리밍 예제를 소개합니다.
Java
Java 기반의 API 예제 코드는 다음과 같습니다.
Project Structure
├───pom.xml │ │ └───src │ ├───main │ │ ├───java │ │ │ └───com │ │ │ └───example │ │ │ └───grpc │ │ │ GRpcClient.java │ │ │ │ │ ├───proto │ │ │ nest.proto
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>clova-speech-grpc</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <netty.version>4.1.52.Final</netty.version> <grpc.version>1.35.0</grpc.version> <protoc.version>3.14.0</protoc.version> </properties> <dependencies> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty</artifactId> <version>${grpc.version}</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> <version>${grpc.version}</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>${grpc.version}</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>${grpc.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> <version>1.18.12</version> </dependency> </dependencies> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.6.1</version> </extension> </extensions> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <showDeprecation>true</showDeprecation> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact> com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} </protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact> io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} </pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
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<NestResponse> responseObserver = new StreamObserver<NestResponse>() { @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<NestRequest> 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()
이 문서가 도움이 되었습니까?