Search in sources :

Example 1 with StreamingStorageConfig

use of com.google.cloud.videointelligence.v1p3beta1.StreamingStorageConfig in project java-video-intelligence by googleapis.

the class StreamingAnnotationToStorage method streamingAnnotationToStorage.

// Perform streaming video detection for explicit content
static void streamingAnnotationToStorage(String filePath, String gcsUri) throws IOException, TimeoutException, StatusRuntimeException {
    try (StreamingVideoIntelligenceServiceClient client = StreamingVideoIntelligenceServiceClient.create()) {
        Path path = Paths.get(filePath);
        byte[] data = Files.readAllBytes(path);
        // Set the chunk size to 5MB (recommended less than 10MB).
        int chunkSize = 5 * 1024 * 1024;
        int numChunks = (int) Math.ceil((double) data.length / chunkSize);
        StreamingStorageConfig streamingStorageConfig = StreamingStorageConfig.newBuilder().setEnableStorageAnnotationResult(true).setAnnotationResultStorageDirectory(gcsUri).build();
        StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder().setStationaryCamera(false).build();
        StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder().setFeature(StreamingFeature.STREAMING_LABEL_DETECTION).setLabelDetectionConfig(labelConfig).setStorageConfig(streamingStorageConfig).build();
        BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call = client.streamingAnnotateVideoCallable().call();
        // The first request must **only** contain the audio configuration:
        call.send(StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build());
        // Send the requests in chunks
        for (int i = 0; i < numChunks; i++) {
            call.send(StreamingAnnotateVideoRequest.newBuilder().setInputContent(ByteString.copyFrom(Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))).build());
        }
        // Tell the service you are done sending data
        call.closeSend();
        for (StreamingAnnotateVideoResponse response : call) {
            System.out.format("Storage Uri: %s\n", response.getAnnotationResultsUri());
        }
    }
}
Also used : Path(java.nio.file.Path) StreamingVideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient) StreamingStorageConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingStorageConfig) StreamingAnnotateVideoResponse(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse) StreamingLabelDetectionConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig) StreamingAnnotateVideoRequest(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest) StreamingVideoConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig)

Aggregations

StreamingAnnotateVideoRequest (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest)1 StreamingAnnotateVideoResponse (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse)1 StreamingLabelDetectionConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig)1 StreamingStorageConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingStorageConfig)1 StreamingVideoConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig)1 StreamingVideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient)1 Path (java.nio.file.Path)1