Search in sources :

Example 6 with ObjectTrackingAnnotation

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

Example 7 with ObjectTrackingAnnotation

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

the class StreamingObjectTracking method streamingObjectTracking.

// Perform streaming video object tracking
static void streamingObjectTracking(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_OBJECT_TRACKING).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 (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());
            }
        }
    }
}
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) StreamingAnnotateVideoResponse(com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse) StreamingLabelDetectionConfig(com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig)

Example 8 with ObjectTrackingAnnotation

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

the class TrackObjects method trackObjectsGcs.

// [END video_object_tracking_beta]
// [START video_object_tracking_gcs_beta]
/**
 * Track objects in a video.
 *
 * @param gcsUri the path to the video file to analyze.
 */
public static VideoAnnotationResults trackObjectsGcs(String gcsUri) throws Exception {
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // Create the request
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.OBJECT_TRACKING).setLocationId("us-east1").build();
        // asynchronously perform object tracking on videos
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        // The first result is retrieved because a single video was processed.
        AnnotateVideoResponse response = future.get(450, TimeUnit.SECONDS);
        VideoAnnotationResults results = response.getAnnotationResults(0);
        // Get only the first annotation for demo purposes.
        ObjectTrackingAnnotation annotation = results.getObjectAnnotations(0);
        System.out.println("Confidence: " + annotation.getConfidence());
        if (annotation.hasEntity()) {
            Entity entity = annotation.getEntity();
            System.out.println("Entity description: " + entity.getDescription());
            System.out.println("Entity id:: " + entity.getEntityId());
        }
        if (annotation.hasSegment()) {
            VideoSegment videoSegment = annotation.getSegment();
            Duration startTimeOffset = videoSegment.getStartTimeOffset();
            Duration endTimeOffset = videoSegment.getEndTimeOffset();
            // Display the segment time in seconds, 1e9 converts nanos to seconds
            System.out.println(String.format("Segment: %.2fs to %.2fs", startTimeOffset.getSeconds() + startTimeOffset.getNanos() / 1e9, endTimeOffset.getSeconds() + endTimeOffset.getNanos() / 1e9));
        }
        // Here we print only the bounding box of the first frame in this segment.
        ObjectTrackingFrame frame = annotation.getFrames(0);
        // Display the offset time in seconds, 1e9 converts nanos to seconds
        Duration timeOffset = frame.getTimeOffset();
        System.out.println(String.format("Time offset of the first frame: %.2fs", timeOffset.getSeconds() + timeOffset.getNanos() / 1e9));
        // Display the bounding box of the detected object
        NormalizedBoundingBox normalizedBoundingBox = frame.getNormalizedBoundingBox();
        System.out.println("Bounding box position:");
        System.out.println("\tleft: " + normalizedBoundingBox.getLeft());
        System.out.println("\ttop: " + normalizedBoundingBox.getTop());
        System.out.println("\tright: " + normalizedBoundingBox.getRight());
        System.out.println("\tbottom: " + normalizedBoundingBox.getBottom());
        return results;
    }
}
Also used : VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1p2beta1.VideoIntelligenceServiceClient) ObjectTrackingFrame(com.google.cloud.videointelligence.v1p2beta1.ObjectTrackingFrame) AnnotateVideoProgress(com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoProgress) Entity(com.google.cloud.videointelligence.v1p2beta1.Entity) NormalizedBoundingBox(com.google.cloud.videointelligence.v1p2beta1.NormalizedBoundingBox) ObjectTrackingAnnotation(com.google.cloud.videointelligence.v1p2beta1.ObjectTrackingAnnotation) VideoSegment(com.google.cloud.videointelligence.v1p2beta1.VideoSegment) AnnotateVideoRequest(com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoRequest) VideoAnnotationResults(com.google.cloud.videointelligence.v1p2beta1.VideoAnnotationResults) Duration(com.google.protobuf.Duration) AnnotateVideoResponse(com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoResponse)

Example 9 with ObjectTrackingAnnotation

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

the class DetectIT method testTrackObjectsGcs.

@Test
public void testTrackObjectsGcs() throws Exception {
    VideoAnnotationResults result = TrackObjects.trackObjectsGcs("gs://cloud-samples-data/video/cat.mp4");
    boolean textExists = false;
    for (ObjectTrackingAnnotation objectTrackingAnnotation : result.getObjectAnnotationsList()) {
        if (objectTrackingAnnotation.getEntity().getDescription().toUpperCase().contains("CAT")) {
            textExists = true;
            break;
        }
    }
    assertThat(textExists).isTrue();
}
Also used : ObjectTrackingAnnotation(com.google.cloud.videointelligence.v1p2beta1.ObjectTrackingAnnotation) VideoAnnotationResults(com.google.cloud.videointelligence.v1p2beta1.VideoAnnotationResults) Test(org.junit.Test)

Aggregations

Duration (com.google.protobuf.Duration)6 Path (java.nio.file.Path)5 AnnotateVideoProgress (com.google.cloud.videointelligence.v1.AnnotateVideoProgress)4 AnnotateVideoRequest (com.google.cloud.videointelligence.v1.AnnotateVideoRequest)4 AnnotateVideoResponse (com.google.cloud.videointelligence.v1.AnnotateVideoResponse)4 Entity (com.google.cloud.videointelligence.v1.Entity)4 NormalizedBoundingBox (com.google.cloud.videointelligence.v1.NormalizedBoundingBox)4 ObjectTrackingAnnotation (com.google.cloud.videointelligence.v1.ObjectTrackingAnnotation)4 ObjectTrackingFrame (com.google.cloud.videointelligence.v1.ObjectTrackingFrame)4 VideoAnnotationResults (com.google.cloud.videointelligence.v1.VideoAnnotationResults)4 VideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient)4 VideoSegment (com.google.cloud.videointelligence.v1.VideoSegment)4 ObjectTrackingAnnotation (com.google.cloud.videointelligence.v1p2beta1.ObjectTrackingAnnotation)3 VideoAnnotationResults (com.google.cloud.videointelligence.v1p2beta1.VideoAnnotationResults)3 AnnotateVideoProgress (com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoProgress)2 AnnotateVideoRequest (com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoRequest)2 AnnotateVideoResponse (com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoResponse)2 Entity (com.google.cloud.videointelligence.v1p2beta1.Entity)2 NormalizedBoundingBox (com.google.cloud.videointelligence.v1p2beta1.NormalizedBoundingBox)2 ObjectTrackingFrame (com.google.cloud.videointelligence.v1p2beta1.ObjectTrackingFrame)2