Search in sources :

Example 1 with StreamingAnnotateVideoRequest

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

the class StreamingExplicitContentDetection method streamingExplicitContentDetection.

// Perform streaming video detection for explicit content
static void streamingExplicitContentDetection(String filePath) 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);
        StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder().setStationaryCamera(false).build();
        StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder().setFeature(StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION).setLabelDetectionConfig(labelConfig).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) {
            StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
            for (ExplicitContentFrame frame : annotationResults.getExplicitAnnotation().getFramesList()) {
                double offset = frame.getTimeOffset().getSeconds() + frame.getTimeOffset().getNanos() / 1e9;
                System.out.format("Offset: %f\n", offset);
                System.out.format("\tPornography: %s", frame.getPornographyLikelihood());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) StreamingVideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient) StreamingVideoAnnotationResults(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults) StreamingAnnotateVideoResponse(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse) ExplicitContentFrame(com.google.cloud.videointelligence.v1p3beta1.ExplicitContentFrame) StreamingLabelDetectionConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig) StreamingAnnotateVideoRequest(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest) StreamingVideoConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig)

Example 2 with StreamingAnnotateVideoRequest

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

the class StreamingShotChangeDetection method streamingShotChangeDetection.

// Perform streaming video detection for shot changes
static void streamingShotChangeDetection(String filePath) 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);
        StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder().setStationaryCamera(false).build();
        StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder().setFeature(StreamingFeature.STREAMING_SHOT_CHANGE_DETECTION).setLabelDetectionConfig(labelConfig).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) {
            StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
            if (response.hasError()) {
                System.out.println(response.getError().getMessage());
                System.out.format("Error was occured with the following status: %s\n", response.getError());
            }
            for (VideoSegment segment : annotationResults.getShotAnnotationsList()) {
                double startTimeOffset = segment.getStartTimeOffset().getSeconds() + segment.getStartTimeOffset().getNanos() / 1e9;
                double endTimeOffset = segment.getEndTimeOffset().getSeconds() + segment.getEndTimeOffset().getNanos() / 1e9;
                System.out.format("Shot: %fs to %fs\n", startTimeOffset, endTimeOffset);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) StreamingVideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient) VideoSegment(com.google.cloud.videointelligence.v1p3beta1.VideoSegment) StreamingVideoAnnotationResults(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults) 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)

Example 3 with StreamingAnnotateVideoRequest

use of com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest 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)

Example 4 with StreamingAnnotateVideoRequest

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

the class StreamingAutoMlClassification method streamingAutoMlClassification.

// Perform streaming video classification with an AutoML Model
static void streamingAutoMlClassification(String filePath, String projectId, String modelId) throws TimeoutException, StatusRuntimeException, IOException {
    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);
        String modelPath = String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId);
        System.out.println(modelPath);
        StreamingAutomlClassificationConfig streamingAutomlClassificationConfig = StreamingAutomlClassificationConfig.newBuilder().setModelName(modelPath).build();
        StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder().setFeature(StreamingFeature.STREAMING_AUTOML_CLASSIFICATION).setAutomlClassificationConfig(streamingAutomlClassificationConfig).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) {
            if (response.hasError()) {
                System.out.println(response.getError().getMessage());
                break;
            }
            StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
            for (LabelAnnotation annotation : annotationResults.getLabelAnnotationsList()) {
                String entity = annotation.getEntity().getDescription();
                // There is only one frame per annotation
                LabelFrame labelFrame = annotation.getFrames(0);
                double offset = labelFrame.getTimeOffset().getSeconds() + labelFrame.getTimeOffset().getNanos() / 1e9;
                float confidence = labelFrame.getConfidence();
                System.out.format("At %fs segment: %s (%f)\n", offset, entity, confidence);
            }
        }
        System.out.println("Video streamed successfully.");
    }
}
Also used : Path(java.nio.file.Path) StreamingVideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient) StreamingVideoAnnotationResults(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults) StreamingAutomlClassificationConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlClassificationConfig) LabelFrame(com.google.cloud.videointelligence.v1p3beta1.LabelFrame) StreamingAnnotateVideoRequest(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest) ByteString(com.google.protobuf.ByteString) LabelAnnotation(com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation) StreamingVideoConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig) StreamingAnnotateVideoResponse(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse)

Example 5 with StreamingAnnotateVideoRequest

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

the class StreamingAutoMlObjectTracking method streamingAutoMlObjectTracking.

// Perform streaming video object tracking with an AutoML Model
static void streamingAutoMlObjectTracking(String filePath, String projectId, String modelId) throws StatusRuntimeException, IOException {
    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);
        String modelPath = String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId);
        StreamingAutomlObjectTrackingConfig streamingAutomlObjectTrackingConfig = StreamingAutomlObjectTrackingConfig.newBuilder().setModelName(modelPath).build();
        StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder().setFeature(StreamingFeature.STREAMING_AUTOML_OBJECT_TRACKING).setAutomlObjectTrackingConfig(streamingAutomlObjectTrackingConfig).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) {
            StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults();
            for (ObjectTrackingAnnotation objectAnnotations : annotationResults.getObjectAnnotationsList()) {
                String entity = objectAnnotations.getEntity().getDescription();
                float confidence = objectAnnotations.getConfidence();
                long trackId = objectAnnotations.getTrackId();
                System.out.format("%s: %f (ID: %d)\n", entity, confidence, trackId);
                // In streaming, there is always one frame.
                ObjectTrackingFrame frame = objectAnnotations.getFrames(0);
                double offset = frame.getTimeOffset().getSeconds() + frame.getTimeOffset().getNanos() / 1e9;
                System.out.format("Offset: %f\n", offset);
                System.out.println("Bounding Box:");
                System.out.format("\tLeft: %f\n", frame.getNormalizedBoundingBox().getLeft());
                System.out.format("\tTop: %f\n", frame.getNormalizedBoundingBox().getTop());
                System.out.format("\tRight: %f\n", frame.getNormalizedBoundingBox().getRight());
                System.out.format("\tBottom: %f\n", frame.getNormalizedBoundingBox().getBottom());
            }
        }
        System.out.println("Video streamed successfully.");
    }
}
Also used : Path(java.nio.file.Path) StreamingVideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient) ObjectTrackingAnnotation(com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation) StreamingVideoAnnotationResults(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults) StreamingAnnotateVideoRequest(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest) ByteString(com.google.protobuf.ByteString) StreamingVideoConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig) ObjectTrackingFrame(com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame) StreamingAutomlObjectTrackingConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlObjectTrackingConfig) StreamingAnnotateVideoResponse(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse)

Aggregations

StreamingAnnotateVideoRequest (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest)8 StreamingAnnotateVideoResponse (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse)8 StreamingVideoConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig)8 StreamingVideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient)8 Path (java.nio.file.Path)8 StreamingVideoAnnotationResults (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults)7 StreamingLabelDetectionConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig)5 ByteString (com.google.protobuf.ByteString)5 LabelAnnotation (com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation)3 LabelFrame (com.google.cloud.videointelligence.v1p3beta1.LabelFrame)3 ObjectTrackingAnnotation (com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation)2 ObjectTrackingFrame (com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame)2 ExplicitContentFrame (com.google.cloud.videointelligence.v1p3beta1.ExplicitContentFrame)1 StreamingAutomlActionRecognitionConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlActionRecognitionConfig)1 StreamingAutomlClassificationConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlClassificationConfig)1 StreamingAutomlObjectTrackingConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlObjectTrackingConfig)1 StreamingStorageConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingStorageConfig)1 VideoSegment (com.google.cloud.videointelligence.v1p3beta1.VideoSegment)1