Search in sources :

Example 1 with StreamingAutomlObjectTrackingConfig

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

ObjectTrackingAnnotation (com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation)1 ObjectTrackingFrame (com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame)1 StreamingAnnotateVideoRequest (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest)1 StreamingAnnotateVideoResponse (com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse)1 StreamingAutomlObjectTrackingConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlObjectTrackingConfig)1 StreamingVideoAnnotationResults (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults)1 StreamingVideoConfig (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig)1 StreamingVideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient)1 ByteString (com.google.protobuf.ByteString)1 Path (java.nio.file.Path)1