diff --git a/video/src/main/java/beta/video/StreamingAutoMlActionRecognition.java b/video/src/main/java/beta/video/StreamingAutoMlActionRecognition.java deleted file mode 100644 index c06ff324cc0..00000000000 --- a/video/src/main/java/beta/video/StreamingAutoMlActionRecognition.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package beta.video; - -// [START video_streaming_automl_action_recognition_beta] - -import com.google.api.gax.rpc.BidiStream; -import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation; -import com.google.cloud.videointelligence.v1p3beta1.LabelFrame; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlActionRecognitionConfig; -import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; -import com.google.protobuf.ByteString; -import io.grpc.StatusRuntimeException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.concurrent.TimeoutException; - -class StreamingAutoMlActionRecognition { - - // Perform streaming video action recognition - static void streamingAutoMlActionRecognition(String filePath, String projectId, String modelId) - 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); - - String modelPath = - String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId); - - System.out.println(modelPath); - - StreamingAutomlActionRecognitionConfig streamingAutomlActionRecognitionConfig = - StreamingAutomlActionRecognitionConfig.newBuilder().setModelName(modelPath).build(); - - StreamingVideoConfig streamingVideoConfig = - StreamingVideoConfig.newBuilder() - .setFeature(StreamingFeature.STREAMING_AUTOML_ACTION_RECOGNITION) - .setAutomlActionRecognitionConfig(streamingAutomlActionRecognitionConfig) - .build(); - - BidiStream call = - client.streamingAnnotateVideoCallable().call(); - - // The first request must **only** contain the video configuration: - call.send( - StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build()); - - // Subsequent requests must **only** contain the video data. - // 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."); - } - } -} -// [END video_streaming_automl_action_recognition_beta] diff --git a/video/src/main/java/beta/video/StreamingAutoMlClassification.java b/video/src/main/java/beta/video/StreamingAutoMlClassification.java deleted file mode 100644 index 5a01c581505..00000000000 --- a/video/src/main/java/beta/video/StreamingAutoMlClassification.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package beta.video; - -// [START video_streaming_automl_classification_beta] - -import com.google.api.gax.rpc.BidiStream; -import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation; -import com.google.cloud.videointelligence.v1p3beta1.LabelFrame; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlClassificationConfig; -import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; -import com.google.protobuf.ByteString; -import io.grpc.StatusRuntimeException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.concurrent.TimeoutException; - -class StreamingAutoMlClassification { - - // Perform streaming video classification with an AutoML Model - static void streamingAutoMlClassification(String filePath, String projectId, String modelId) - throws TimeoutException, StatusRuntimeException, IOException { - // String filePath = "path_to_your_video_file"; - // String projectId = "YOUR_GCP_PROJECT_ID"; - // String modelId = "YOUR_AUTO_ML_CLASSIFICATION_MODEL_ID"; - - 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 call = - client.streamingAnnotateVideoCallable().call(); - - // The first request must **only** contain the audio configuration: - call.send( - StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build()); - - // Subsequent requests must **only** contain the audio data. - // 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."); - } - } -} -// [END video_streaming_automl_classification_beta] diff --git a/video/src/main/java/beta/video/StreamingAutoMlObjectTracking.java b/video/src/main/java/beta/video/StreamingAutoMlObjectTracking.java deleted file mode 100644 index 7258c341b76..00000000000 --- a/video/src/main/java/beta/video/StreamingAutoMlObjectTracking.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2021 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package beta.video; - -// [START video_streaming_automl_object_tracking_beta] - -import com.google.api.gax.rpc.BidiStream; -import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation; -import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; -import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlObjectTrackingConfig; -import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; -import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; -import com.google.protobuf.ByteString; -import io.grpc.StatusRuntimeException; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; - -class StreamingAutoMlObjectTracking { - - public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String filePath = "YOUR_VIDEO_FILE"; - String projectId = "YOUR_PROJECT_ID"; - String modelId = "YOUR_AUTOML_OBJECT_TRACKING_MODEL_ID"; - streamingAutoMlObjectTracking(filePath, projectId, modelId); - } - - // 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 call = - client.streamingAnnotateVideoCallable().call(); - - // The first request must **only** contain the audio configuration: - call.send( - StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build()); - - // Subsequent requests must **only** contain the audio data. - // 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."); - } - } -} -// [END video_streaming_automl_object_tracking_beta] diff --git a/video/src/test/java/beta/video/StreamingAutoMlActionRecognitionIT.java b/video/src/test/java/beta/video/StreamingAutoMlActionRecognitionIT.java deleted file mode 100644 index b0fd040a5b3..00000000000 --- a/video/src/test/java/beta/video/StreamingAutoMlActionRecognitionIT.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package beta.video; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Status; -import io.grpc.StatusRuntimeException; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Integration (system) tests for {@link StreamingAutoMlActionRecognition}. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class StreamingAutoMlActionRecognitionIT { - - private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); - private static String MODEL_ID = "2787930479481847808"; - - private ByteArrayOutputStream bout; - private PrintStream out; - private PrintStream originalPrintStream; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @After - public void tearDown() { - // restores print statements in the original method - System.out.flush(); - System.setOut(originalPrintStream); - } - - @Test - public void testStreamingAutoMlActionRecognition() { - // Bad Gateway sporadically occurs - int tryCount = 0; - int maxTries = 3; - while (tryCount < maxTries) { - try { - StreamingAutoMlActionRecognition.streamingAutoMlActionRecognition( - "resources/cat.mp4", PROJECT_ID, MODEL_ID); - assertThat(bout.toString()).contains("Video streamed successfully."); - - break; - } catch (StatusRuntimeException ex) { - if (ex.getStatus().getCode() == Status.Code.UNAVAILABLE) { - assertThat(ex.getMessage()).contains("Bad Gateway"); - tryCount++; - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } -} diff --git a/video/src/test/java/beta/video/StreamingAutoMlClassificationIT.java b/video/src/test/java/beta/video/StreamingAutoMlClassificationIT.java deleted file mode 100644 index e412833ae5d..00000000000 --- a/video/src/test/java/beta/video/StreamingAutoMlClassificationIT.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package beta.video; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Status; -import io.grpc.StatusRuntimeException; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Integration (system) tests for {@link StreamingAutoMlClassification}. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class StreamingAutoMlClassificationIT { - - private static String PROJECT_ID = "779844219229"; // System.getenv().get("GOOGLE_CLOUD_PROJECT"); - private static String MODEL_ID = "VCN6455760532254228480"; - - private ByteArrayOutputStream bout; - private PrintStream out; - private PrintStream originalPrintStream; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @After - public void tearDown() { - // restores print statements in the original method - System.out.flush(); - System.setOut(originalPrintStream); - } - - @Test - public void testStreamingAutoMlClassification() { - // Bad Gateway sporadically occurs - int tryCount = 0; - int maxTries = 3; - while (tryCount < maxTries) { - try { - StreamingAutoMlClassification.streamingAutoMlClassification( - "resources/cat.mp4", PROJECT_ID, MODEL_ID); - assertThat(bout.toString()).contains("Video streamed successfully."); - - break; - } catch (StatusRuntimeException ex) { - if (ex.getStatus().getCode() == Status.Code.UNAVAILABLE) { - assertThat(ex.getMessage()).contains("Bad Gateway"); - tryCount++; - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } -} diff --git a/video/src/test/java/beta/video/StreamingAutoMlObjectTrackingIT.java b/video/src/test/java/beta/video/StreamingAutoMlObjectTrackingIT.java deleted file mode 100644 index 2d7f082fe2c..00000000000 --- a/video/src/test/java/beta/video/StreamingAutoMlObjectTrackingIT.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2021 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package beta.video; - -import static com.google.common.truth.Truth.assertThat; - -import io.grpc.Status; -import io.grpc.StatusRuntimeException; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Integration (system) tests for {@link StreamingAutoMlObjectTracking}. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class StreamingAutoMlObjectTrackingIT { - - private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); - private static String MODEL_ID = System.getenv().get("VIDEO_OBJECT_TRACKING_MODEL_ID"); - - private ByteArrayOutputStream bout; - private PrintStream out; - private PrintStream originalPrintStream; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @After - public void tearDown() { - // restores print statements in the original method - System.out.flush(); - System.setOut(originalPrintStream); - } - - @Test - public void testStreamingAutoMlObjectTracking() { - // Bad Gateway sporadically occurs - int tryCount = 0; - int maxTries = 3; - while (tryCount < maxTries) { - try { - StreamingAutoMlObjectTracking.streamingAutoMlObjectTracking( - "resources/cat.mp4", PROJECT_ID, MODEL_ID); - assertThat(bout.toString()).contains("Video streamed successfully."); - - break; - } catch (StatusRuntimeException ex) { - if (ex.getStatus().getCode() == Status.Code.UNAVAILABLE) { - assertThat(ex.getMessage()).contains("Bad Gateway"); - tryCount++; - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } -}