Search in sources :

Example 6 with LabelAnnotation

use of com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class QuickstartSample method main.

/**
 * Demonstrates using the video intelligence client to detect labels in a video file.
 */
public static void main(String[] args) throws Exception {
    // Instantiate a video intelligence client
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // The Google Cloud Storage path to the video to annotate.
        String gcsUri = "gs://demomaker/cat.mp4";
        // Create an operation that will contain the response when the operation completes.
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.LABEL_DETECTION).build();
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        List<VideoAnnotationResults> results = response.get().getAnnotationResultsList();
        if (results.isEmpty()) {
            System.out.println("No labels detected in " + gcsUri);
            return;
        }
        for (VideoAnnotationResults result : results) {
            System.out.println("Labels:");
            // get video segment label annotations
            for (LabelAnnotation annotation : result.getSegmentLabelAnnotationsList()) {
                System.out.println("Video label description : " + annotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : annotation.getCategoryEntitiesList()) {
                    System.out.println("Label Category description : " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : annotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location : %.3f:%.3f\n", startTime, endTime);
                    System.out.println("Confidence : " + segment.getConfidence());
                }
            }
        }
    }
}
Also used : VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient) AnnotateVideoProgress(com.google.cloud.videointelligence.v1.AnnotateVideoProgress) Entity(com.google.cloud.videointelligence.v1.Entity) LabelSegment(com.google.cloud.videointelligence.v1.LabelSegment) AnnotateVideoRequest(com.google.cloud.videointelligence.v1.AnnotateVideoRequest) VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) LabelAnnotation(com.google.cloud.videointelligence.v1.LabelAnnotation) AnnotateVideoResponse(com.google.cloud.videointelligence.v1.AnnotateVideoResponse)

Example 7 with LabelAnnotation

use of com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method analyzeLabelsFile.

/**
 * Performs label analysis on the video at the provided file path.
 *
 * @param filePath the path to the video file to analyze.
 */
public static void analyzeLabelsFile(String filePath) throws Exception {
    // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // Read file and encode into Base64
        Path path = Paths.get(filePath);
        byte[] data = Files.readAllBytes(path);
        byte[] encodedBytes = Base64.encodeBase64(data);
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputContent(ByteString.copyFrom(encodedBytes)).addFeatures(Feature.LABEL_DETECTION).build();
        // Create an operation that will contain the response when the operation completes.
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        for (VideoAnnotationResults results : response.get().getAnnotationResultsList()) {
            // process video / segment level label annotations
            System.out.println("Locations: ");
            for (LabelAnnotation labelAnnotation : results.getSegmentLabelAnnotationsList()) {
                System.out.println("Video label: " + labelAnnotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : labelAnnotation.getCategoryEntitiesList()) {
                    System.out.println("Video label category: " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : labelAnnotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location: %.3f:%.2f\n", startTime, endTime);
                    System.out.println("Confidence: " + segment.getConfidence());
                }
            }
            // process shot label annotations
            for (LabelAnnotation labelAnnotation : results.getShotLabelAnnotationsList()) {
                System.out.println("Shot label: " + labelAnnotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : labelAnnotation.getCategoryEntitiesList()) {
                    System.out.println("Shot label category: " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : labelAnnotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location: %.3f:%.2f\n", startTime, endTime);
                    System.out.println("Confidence: " + segment.getConfidence());
                }
            }
            // process frame label annotations
            for (LabelAnnotation labelAnnotation : results.getFrameLabelAnnotationsList()) {
                System.out.println("Frame label: " + labelAnnotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : labelAnnotation.getCategoryEntitiesList()) {
                    System.out.println("Frame label category: " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : labelAnnotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location: %.3f:%.2f\n", startTime, endTime);
                    System.out.println("Confidence: " + segment.getConfidence());
                }
            }
        }
    }
// [END detect_labels_file]
}
Also used : VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient) Path(java.nio.file.Path) AnnotateVideoProgress(com.google.cloud.videointelligence.v1.AnnotateVideoProgress) Entity(com.google.cloud.videointelligence.v1.Entity) LabelSegment(com.google.cloud.videointelligence.v1.LabelSegment) AnnotateVideoRequest(com.google.cloud.videointelligence.v1.AnnotateVideoRequest) VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) LabelAnnotation(com.google.cloud.videointelligence.v1.LabelAnnotation) AnnotateVideoResponse(com.google.cloud.videointelligence.v1.AnnotateVideoResponse)

Example 8 with LabelAnnotation

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

the class QuickstartSample method main.

/**
 * Demonstrates using the video intelligence client to detect labels in a video file.
 */
public static void main(String[] args) throws Exception {
    // Instantiate a video intelligence client
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // The Google Cloud Storage path to the video to annotate.
        String gcsUri = "gs://cloud-samples-data/video/cat.mp4";
        // Create an operation that will contain the response when the operation completes.
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.LABEL_DETECTION).build();
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        List<VideoAnnotationResults> results = response.get().getAnnotationResultsList();
        if (results.isEmpty()) {
            System.out.println("No labels detected in " + gcsUri);
            return;
        }
        for (VideoAnnotationResults result : results) {
            System.out.println("Labels:");
            // get video segment label annotations
            for (LabelAnnotation annotation : result.getSegmentLabelAnnotationsList()) {
                System.out.println("Video label description : " + annotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : annotation.getCategoryEntitiesList()) {
                    System.out.println("Label Category description : " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : annotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location : %.3f:%.3f\n", startTime, endTime);
                    System.out.println("Confidence : " + segment.getConfidence());
                }
            }
        }
    }
}
Also used : VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient) AnnotateVideoProgress(com.google.cloud.videointelligence.v1.AnnotateVideoProgress) Entity(com.google.cloud.videointelligence.v1.Entity) LabelSegment(com.google.cloud.videointelligence.v1.LabelSegment) AnnotateVideoRequest(com.google.cloud.videointelligence.v1.AnnotateVideoRequest) VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) LabelAnnotation(com.google.cloud.videointelligence.v1.LabelAnnotation) AnnotateVideoResponse(com.google.cloud.videointelligence.v1.AnnotateVideoResponse)

Example 9 with LabelAnnotation

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

the class Detect method analyzeLabelsFile.

/**
 * Performs label analysis on the video at the provided file path.
 *
 * @param filePath the path to the video file to analyze.
 */
public static void analyzeLabelsFile(String filePath) throws Exception {
    // Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // Read file and encode into Base64
        Path path = Paths.get(filePath);
        byte[] data = Files.readAllBytes(path);
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputContent(ByteString.copyFrom(data)).addFeatures(Feature.LABEL_DETECTION).build();
        // Create an operation that will contain the response when the operation completes.
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        for (VideoAnnotationResults results : response.get().getAnnotationResultsList()) {
            // process video / segment level label annotations
            System.out.println("Locations: ");
            for (LabelAnnotation labelAnnotation : results.getSegmentLabelAnnotationsList()) {
                System.out.println("Video label: " + labelAnnotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : labelAnnotation.getCategoryEntitiesList()) {
                    System.out.println("Video label category: " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : labelAnnotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location: %.3f:%.2f\n", startTime, endTime);
                    System.out.println("Confidence: " + segment.getConfidence());
                }
            }
            // process shot label annotations
            for (LabelAnnotation labelAnnotation : results.getShotLabelAnnotationsList()) {
                System.out.println("Shot label: " + labelAnnotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : labelAnnotation.getCategoryEntitiesList()) {
                    System.out.println("Shot label category: " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : labelAnnotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location: %.3f:%.2f\n", startTime, endTime);
                    System.out.println("Confidence: " + segment.getConfidence());
                }
            }
            // process frame label annotations
            for (LabelAnnotation labelAnnotation : results.getFrameLabelAnnotationsList()) {
                System.out.println("Frame label: " + labelAnnotation.getEntity().getDescription());
                // categories
                for (Entity categoryEntity : labelAnnotation.getCategoryEntitiesList()) {
                    System.out.println("Frame label category: " + categoryEntity.getDescription());
                }
                // segments
                for (LabelSegment segment : labelAnnotation.getSegmentsList()) {
                    double startTime = segment.getSegment().getStartTimeOffset().getSeconds() + segment.getSegment().getStartTimeOffset().getNanos() / 1e9;
                    double endTime = segment.getSegment().getEndTimeOffset().getSeconds() + segment.getSegment().getEndTimeOffset().getNanos() / 1e9;
                    System.out.printf("Segment location: %.3f:%.2f\n", startTime, endTime);
                    System.out.println("Confidence: " + segment.getConfidence());
                }
            }
        }
    }
// [END video_analyze_labels]
}
Also used : VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient) Path(java.nio.file.Path) AnnotateVideoProgress(com.google.cloud.videointelligence.v1.AnnotateVideoProgress) Entity(com.google.cloud.videointelligence.v1.Entity) LabelSegment(com.google.cloud.videointelligence.v1.LabelSegment) AnnotateVideoRequest(com.google.cloud.videointelligence.v1.AnnotateVideoRequest) VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) LabelAnnotation(com.google.cloud.videointelligence.v1.LabelAnnotation) AnnotateVideoResponse(com.google.cloud.videointelligence.v1.AnnotateVideoResponse)

Example 10 with LabelAnnotation

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

Aggregations

AnnotateVideoProgress (com.google.cloud.videointelligence.v1.AnnotateVideoProgress)9 AnnotateVideoRequest (com.google.cloud.videointelligence.v1.AnnotateVideoRequest)9 AnnotateVideoResponse (com.google.cloud.videointelligence.v1.AnnotateVideoResponse)9 Entity (com.google.cloud.videointelligence.v1.Entity)9 LabelAnnotation (com.google.cloud.videointelligence.v1.LabelAnnotation)9 LabelSegment (com.google.cloud.videointelligence.v1.LabelSegment)9 VideoAnnotationResults (com.google.cloud.videointelligence.v1.VideoAnnotationResults)9 VideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient)9 Path (java.nio.file.Path)6 LabelAnnotation (com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation)3 LabelFrame (com.google.cloud.videointelligence.v1p3beta1.LabelFrame)3 StreamingAnnotateVideoRequest (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest)3 StreamingAnnotateVideoResponse (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse)3 StreamingVideoAnnotationResults (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults)3 StreamingVideoConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig)3 StreamingVideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient)3 ByteString (com.google.protobuf.ByteString)3 StreamingAutomlActionRecognitionConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlActionRecognitionConfig)1 StreamingAutomlClassificationConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlClassificationConfig)1 StreamingLabelDetectionConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig)1