Search in sources :

Example 16 with VideoAnnotationResults

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

the class DetectPersonGcs method detectPersonGcs.

// Detects people in a video stored in Google Cloud Storage using
// the Cloud Video Intelligence API.
public static void detectPersonGcs(String gcsUri) throws Exception {
    try (VideoIntelligenceServiceClient videoIntelligenceServiceClient = VideoIntelligenceServiceClient.create()) {
        // Reads a local video file and converts it to base64.
        PersonDetectionConfig personDetectionConfig = PersonDetectionConfig.newBuilder().setIncludeBoundingBoxes(true).setIncludePoseLandmarks(true).setIncludeAttributes(true).build();
        VideoContext videoContext = VideoContext.newBuilder().setPersonDetectionConfig(personDetectionConfig).build();
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.PERSON_DETECTION).setVideoContext(videoContext).build();
        // Detects people in a video
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future = videoIntelligenceServiceClient.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        AnnotateVideoResponse response = future.get();
        // Get the first response, since we sent only one video.
        VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0);
        // Annotations for list of people detected, tracked and recognized in video.
        for (PersonDetectionAnnotation personDetectionAnnotation : annotationResult.getPersonDetectionAnnotationsList()) {
            System.out.print("Person detected:\n");
            for (Track track : personDetectionAnnotation.getTracksList()) {
                VideoSegment segment = track.getSegment();
                System.out.printf("\tStart: %d.%.0fs\n", segment.getStartTimeOffset().getSeconds(), segment.getStartTimeOffset().getNanos() / 1e6);
                System.out.printf("\tEnd: %d.%.0fs\n", segment.getEndTimeOffset().getSeconds(), segment.getEndTimeOffset().getNanos() / 1e6);
                // Each segment includes timestamped objects that include characteristic--e.g. clothes,
                // posture of the person detected.
                TimestampedObject firstTimestampedObject = track.getTimestampedObjects(0);
                // of the person detected.
                for (DetectedAttribute attribute : firstTimestampedObject.getAttributesList()) {
                    System.out.printf("\tAttribute: %s; Value: %s\n", attribute.getName(), attribute.getValue());
                }
                // Landmarks in person detection include body parts.
                for (DetectedLandmark attribute : firstTimestampedObject.getLandmarksList()) {
                    System.out.printf("\tLandmark: %s; Vertex: %f, %f\n", attribute.getName(), attribute.getPoint().getX(), attribute.getPoint().getY());
                }
            }
        }
    }
}
Also used : AnnotateVideoRequest(com.google.cloud.videointelligence.v1.AnnotateVideoRequest) VideoContext(com.google.cloud.videointelligence.v1.VideoContext) PersonDetectionAnnotation(com.google.cloud.videointelligence.v1.PersonDetectionAnnotation) PersonDetectionConfig(com.google.cloud.videointelligence.v1.PersonDetectionConfig) VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient) AnnotateVideoProgress(com.google.cloud.videointelligence.v1.AnnotateVideoProgress) VideoSegment(com.google.cloud.videointelligence.v1.VideoSegment) DetectedLandmark(com.google.cloud.videointelligence.v1.DetectedLandmark) TimestampedObject(com.google.cloud.videointelligence.v1.TimestampedObject) VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) DetectedAttribute(com.google.cloud.videointelligence.v1.DetectedAttribute) Track(com.google.cloud.videointelligence.v1.Track) AnnotateVideoResponse(com.google.cloud.videointelligence.v1.AnnotateVideoResponse)

Example 17 with VideoAnnotationResults

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

the class Detect method speechTranscription.

// [START video_speech_transcription_gcs_beta]
/**
 * Transcribe speech from a video stored on GCS.
 *
 * @param gcsUri the path to the video file to analyze.
 */
public static void speechTranscription(String gcsUri) throws Exception {
    // Instantiate a com.google.cloud.videointelligence.v1p1beta1.VideoIntelligenceServiceClient
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
        // Set the language code
        SpeechTranscriptionConfig config = SpeechTranscriptionConfig.newBuilder().setLanguageCode("en-US").setEnableAutomaticPunctuation(true).build();
        // Set the video context with the above configuration
        VideoContext context = VideoContext.newBuilder().setSpeechTranscriptionConfig(config).build();
        // Create the request
        AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder().setInputUri(gcsUri).addFeatures(Feature.SPEECH_TRANSCRIPTION).setVideoContext(context).build();
        // asynchronously perform speech transcription on videos
        OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = client.annotateVideoAsync(request);
        System.out.println("Waiting for operation to complete...");
        // Display the results
        for (VideoAnnotationResults results : response.get(300, TimeUnit.SECONDS).getAnnotationResultsList()) {
            for (SpeechTranscription speechTranscription : results.getSpeechTranscriptionsList()) {
                try {
                    // Print the transcription
                    if (speechTranscription.getAlternativesCount() > 0) {
                        SpeechRecognitionAlternative alternative = speechTranscription.getAlternatives(0);
                        System.out.printf("Transcript: %s\n", alternative.getTranscript());
                        System.out.printf("Confidence: %.2f\n", alternative.getConfidence());
                        System.out.println("Word level information:");
                        for (WordInfo wordInfo : alternative.getWordsList()) {
                            double startTime = wordInfo.getStartTime().getSeconds() + wordInfo.getStartTime().getNanos() / 1e9;
                            double endTime = wordInfo.getEndTime().getSeconds() + wordInfo.getEndTime().getNanos() / 1e9;
                            System.out.printf("\t%4.2fs - %4.2fs: %s\n", startTime, endTime, wordInfo.getWord());
                        }
                    } else {
                        System.out.println("No transcription found");
                    }
                } catch (IndexOutOfBoundsException ioe) {
                    System.out.println("Could not retrieve frame: " + ioe.getMessage());
                }
            }
        }
    }
}
Also used : AnnotateVideoRequest(com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoRequest) VideoContext(com.google.cloud.videointelligence.v1p1beta1.VideoContext) VideoIntelligenceServiceClient(com.google.cloud.videointelligence.v1p1beta1.VideoIntelligenceServiceClient) AnnotateVideoProgress(com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoProgress) SpeechRecognitionAlternative(com.google.cloud.videointelligence.v1p1beta1.SpeechRecognitionAlternative) SpeechTranscriptionConfig(com.google.cloud.videointelligence.v1p1beta1.SpeechTranscriptionConfig) VideoAnnotationResults(com.google.cloud.videointelligence.v1p1beta1.VideoAnnotationResults) SpeechTranscription(com.google.cloud.videointelligence.v1p1beta1.SpeechTranscription) WordInfo(com.google.cloud.videointelligence.v1p1beta1.WordInfo) AnnotateVideoResponse(com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoResponse)

Example 18 with VideoAnnotationResults

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

the class DetectIT method testTextDetectionGcs.

@Test
public void testTextDetectionGcs() throws Exception {
    VideoAnnotationResults result = TextDetection.detectTextGcs(SPEECH_GCS_LOCATION);
    boolean textExists = false;
    for (TextAnnotation textAnnotation : result.getTextAnnotationsList()) {
        for (String possibleText : POSSIBLE_TEXTS) {
            if (textAnnotation.getText().toUpperCase().contains(possibleText.toUpperCase())) {
                textExists = true;
                break;
            }
        }
    }
    assertThat(textExists).isTrue();
}
Also used : VideoAnnotationResults(com.google.cloud.videointelligence.v1.VideoAnnotationResults) TextAnnotation(com.google.cloud.videointelligence.v1.TextAnnotation) Test(org.junit.Test)

Example 19 with VideoAnnotationResults

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

the class ITSystemTest method annotateVideoTest.

@Test
public void annotateVideoTest() throws ExecutionException, InterruptedException {
    AnnotateVideoResponse response = client.annotateVideoAsync(BUCKET_URI, FEATURE_LIST).get();
    List<VideoAnnotationResults> videoAnnotationResults = response.getAnnotationResultsList();
    for (VideoAnnotationResults result : videoAnnotationResults) {
        System.out.println(result.getSegmentLabelAnnotationsList().size());
        assertEquals(BUCKET_URI.substring(4), result.getInputUri());
        assertTrue(result.getSegmentLabelAnnotationsList().size() > 0);
    }
}
Also used : VideoAnnotationResults(com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults) AnnotateVideoResponse(com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse) Test(org.junit.Test)

Example 20 with VideoAnnotationResults

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

the class ITSystemTest method annotateVideoTest.

@Test
public void annotateVideoTest() throws ExecutionException, InterruptedException {
    AnnotateVideoResponse response = client.annotateVideoAsync(BUCKET_URI, FEATURE_LIST).get();
    List<VideoAnnotationResults> videoAnnotationResults = response.getAnnotationResultsList();
    for (VideoAnnotationResults result : videoAnnotationResults) {
        assertEquals(BUCKET_URI.substring(4), result.getInputUri());
        assertTrue(result.getSegmentLabelAnnotationsList().size() > 0);
    }
}
Also used : VideoAnnotationResults(com.google.cloud.videointelligence.v1p2beta1.VideoAnnotationResults) AnnotateVideoResponse(com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoResponse) Test(org.junit.Test)

Aggregations

VideoAnnotationResults (com.google.cloud.videointelligence.v1.VideoAnnotationResults)44 AnnotateVideoResponse (com.google.cloud.videointelligence.v1.AnnotateVideoResponse)34 AnnotateVideoProgress (com.google.cloud.videointelligence.v1.AnnotateVideoProgress)33 AnnotateVideoRequest (com.google.cloud.videointelligence.v1.AnnotateVideoRequest)33 VideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient)33 VideoSegment (com.google.cloud.videointelligence.v1.VideoSegment)19 Duration (com.google.protobuf.Duration)18 Entity (com.google.cloud.videointelligence.v1.Entity)17 Path (java.nio.file.Path)14 Test (org.junit.Test)14 VideoContext (com.google.cloud.videointelligence.v1.VideoContext)10 LabelAnnotation (com.google.cloud.videointelligence.v1.LabelAnnotation)9 LabelSegment (com.google.cloud.videointelligence.v1.LabelSegment)9 DetectedAttribute (com.google.cloud.videointelligence.v1.DetectedAttribute)8 NormalizedBoundingBox (com.google.cloud.videointelligence.v1.NormalizedBoundingBox)8 TextAnnotation (com.google.cloud.videointelligence.v1.TextAnnotation)8 TimestampedObject (com.google.cloud.videointelligence.v1.TimestampedObject)8 Track (com.google.cloud.videointelligence.v1.Track)8 VideoAnnotationResults (com.google.cloud.videointelligence.v1p2beta1.VideoAnnotationResults)8 AnnotateVideoResponse (com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoResponse)5