Membuat anotasi video menggunakan library klien

Panduan memulai ini memperkenalkan Video Intelligence API kepada Anda. Dalam panduan memulai ini, Anda akan menyiapkan project dan otorisasi Google Cloud, lalu membuat permintaan agar Video Intelligence menganotasi video.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. Di konsol Google Cloud, pada halaman pemilih project, pilih atau buat project Google Cloud.

    Buka pemilih project

  3. Pastikan penagihan telah diaktifkan untuk project Google Cloud Anda.

  4. Enable the Cloud Video Intelligence API.

    Enable the API

  5. Buat akun layanan:

    1. Di konsol Google Cloud, buka halaman Buat akun layanan.

      Buka Create service account
    2. Pilih project Anda.
    3. Di kolom Nama akun layanan, masukkan nama. Konsol Google Cloud akan mengisi kolom ID akun layanan berdasarkan nama ini.

      Di kolom Deskripsi akun layanan, masukkan sebuah deskripsi. Sebagai contoh, Service account for quickstart.

    4. Klik Buat dan lanjutkan.
    5. Klik Selesai untuk menyelesaikan pembuatan akun layanan.

      Jangan tutup jendela browser Anda. Anda akan menggunakannya pada langkah berikutnya.

  6. Membuat kunci akun layanan:

    1. Di konsol Google Cloud, klik alamat email untuk akun layanan yang telah dibuat.
    2. Klik Kunci.
    3. Klik Tambahkan kunci, lalu klik Buat kunci baru.
    4. Klik Create. File kunci JSON akan didownload ke komputer Anda.
    5. Klik Close.
  7. Tetapkan variabel lingkungan GOOGLE_APPLICATION_CREDENTIALS ke jalur file JSON yang berisi kredensial Anda. Variabel ini hanya berlaku untuk sesi shell Anda saat ini. Jadi, jika Anda membuka sesi baru, tetapkan variabel kembali.

  8. Menginstal Google Cloud CLI.
  9. Untuk initialize gcloud CLI, jalankan perintah berikut:

    gcloud init
  10. Di konsol Google Cloud, pada halaman pemilih project, pilih atau buat project Google Cloud.

    Buka pemilih project

  11. Pastikan penagihan telah diaktifkan untuk project Google Cloud Anda.

  12. Enable the Cloud Video Intelligence API.

    Enable the API

  13. Buat akun layanan:

    1. Di konsol Google Cloud, buka halaman Buat akun layanan.

      Buka Create service account
    2. Pilih project Anda.
    3. Di kolom Nama akun layanan, masukkan nama. Konsol Google Cloud akan mengisi kolom ID akun layanan berdasarkan nama ini.

      Di kolom Deskripsi akun layanan, masukkan sebuah deskripsi. Sebagai contoh, Service account for quickstart.

    4. Klik Buat dan lanjutkan.
    5. Klik Selesai untuk menyelesaikan pembuatan akun layanan.

      Jangan tutup jendela browser Anda. Anda akan menggunakannya pada langkah berikutnya.

  14. Membuat kunci akun layanan:

    1. Di konsol Google Cloud, klik alamat email untuk akun layanan yang telah dibuat.
    2. Klik Kunci.
    3. Klik Tambahkan kunci, lalu klik Buat kunci baru.
    4. Klik Create. File kunci JSON akan didownload ke komputer Anda.
    5. Klik Close.
  15. Tetapkan variabel lingkungan GOOGLE_APPLICATION_CREDENTIALS ke jalur file JSON yang berisi kredensial Anda. Variabel ini hanya berlaku untuk sesi shell Anda saat ini. Jadi, jika Anda membuka sesi baru, tetapkan variabel kembali.

  16. Menginstal Google Cloud CLI.
  17. Untuk initialize gcloud CLI, jalankan perintah berikut:

    gcloud init

Menginstal library klien

Go

go get cloud.google.com/go/videointelligence/apiv1

Java

Node.js

Sebelum menginstal library, pastikan Anda telah menyiapkan lingkungan untuk pengembangan Node.js.

npm install --save @google-cloud/video-intelligence

Python

Sebelum menginstal library, pastikan Anda telah menyiapkan lingkungan untuk pengembangan Python.

pip install --upgrade google-cloud-videointelligence

Bahasa tambahan

C#: Ikuti Petunjuk penyiapan C# di halaman library klien lalu buka Dokumentasi referensi Video Intelligence untuk .NET.

PHP: Ikuti petunjuk penyiapan PHP di halaman library klien lalu kunjungi Dokumentasi referensi Video Intelligence untuk PHP.

Ruby: Ikuti petunjuk penyiapan Ruby di halaman library klien, lalu buka Dokumentasi referensi Video Intelligence untuk Ruby.

Deteksi label

Sekarang Anda dapat menggunakan Video Intelligence API untuk meminta informasi dari segmen video atau video, seperti deteksi label. Jalankan kode berikut untuk melakukan permintaan deteksi label video pertama Anda:

Go


// Sample video_quickstart uses the Google Cloud Video Intelligence API to label a video.
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/golang/protobuf/ptypes"

	video "cloud.google.com/go/videointelligence/apiv1"
	videopb "cloud.google.com/go/videointelligence/apiv1/videointelligencepb"
)

func main() {
	ctx := context.Background()

	// Creates a client.
	client, err := video.NewClient(ctx)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}
	defer client.Close()

	op, err := client.AnnotateVideo(ctx, &videopb.AnnotateVideoRequest{
		InputUri: "gs://cloud-samples-data/video/cat.mp4",
		Features: []videopb.Feature{
			videopb.Feature_LABEL_DETECTION,
		},
	})
	if err != nil {
		log.Fatalf("Failed to start annotation job: %v", err)
	}

	resp, err := op.Wait(ctx)
	if err != nil {
		log.Fatalf("Failed to annotate: %v", err)
	}

	// Only one video was processed, so get the first result.
	result := resp.GetAnnotationResults()[0]

	for _, annotation := range result.SegmentLabelAnnotations {
		fmt.Printf("Description: %s\n", annotation.Entity.Description)

		for _, category := range annotation.CategoryEntities {
			fmt.Printf("\tCategory: %s\n", category.Description)
		}

		for _, segment := range annotation.Segments {
			start, _ := ptypes.Duration(segment.Segment.StartTimeOffset)
			end, _ := ptypes.Duration(segment.Segment.EndTimeOffset)
			fmt.Printf("\tSegment: %s to %s\n", start, end)
			fmt.Printf("\tConfidence: %v\n", segment.Confidence)
		}
	}
}

Java


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.videointelligence.v1.AnnotateVideoProgress;
import com.google.cloud.videointelligence.v1.AnnotateVideoRequest;
import com.google.cloud.videointelligence.v1.AnnotateVideoResponse;
import com.google.cloud.videointelligence.v1.Entity;
import com.google.cloud.videointelligence.v1.Feature;
import com.google.cloud.videointelligence.v1.LabelAnnotation;
import com.google.cloud.videointelligence.v1.LabelSegment;
import com.google.cloud.videointelligence.v1.VideoAnnotationResults;
import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient;
import java.util.List;

public class QuickstartSample {

  /** 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());
          }
        }
      }
    }
  }
}

Node.js

Sebelum menjalankan contoh, pastikan Anda telah menyiapkan lingkungan untuk pengembangan Node.js.

// Imports the Google Cloud Video Intelligence library
const videoIntelligence = require('@google-cloud/video-intelligence');

// Creates a client
const client = new videoIntelligence.VideoIntelligenceServiceClient();

// The GCS uri of the video to analyze
const gcsUri = 'gs://cloud-samples-data/video/cat.mp4';

// Construct request
const request = {
  inputUri: gcsUri,
  features: ['LABEL_DETECTION'],
};

// Execute request
const [operation] = await client.annotateVideo(request);

console.log(
  'Waiting for operation to complete... (this may take a few minutes)'
);

const [operationResult] = await operation.promise();

// Gets annotations for video
const annotations = operationResult.annotationResults[0];

// Gets labels for video from its annotations
const labels = annotations.segmentLabelAnnotations;
labels.forEach(label => {
  console.log(`Label ${label.entity.description} occurs at:`);
  label.segments.forEach(segment => {
    segment = segment.segment;
    console.log(
      `\tStart: ${segment.startTimeOffset.seconds}` +
        `.${(segment.startTimeOffset.nanos / 1e6).toFixed(0)}s`
    );
    console.log(
      `\tEnd: ${segment.endTimeOffset.seconds}.` +
        `${(segment.endTimeOffset.nanos / 1e6).toFixed(0)}s`
    );
  });
});

Python

Sebelum menjalankan contoh, pastikan Anda telah menyiapkan lingkungan untuk pengembangan Python.

from google.cloud import videointelligence

video_client = videointelligence.VideoIntelligenceServiceClient()
features = [videointelligence.Feature.LABEL_DETECTION]
operation = video_client.annotate_video(
    request={
        "features": features,
        "input_uri": "gs://cloud-samples-data/video/cat.mp4",
    }
)
print("\nProcessing video for label annotations:")

result = operation.result(timeout=180)
print("\nFinished processing.")

# first result is retrieved because a single video was processed
segment_labels = result.annotation_results[0].segment_label_annotations
for i, segment_label in enumerate(segment_labels):
    print("Video label description: {}".format(segment_label.entity.description))
    for category_entity in segment_label.category_entities:
        print(
            "\tLabel category description: {}".format(category_entity.description)
        )

    for i, segment in enumerate(segment_label.segments):
        start_time = (
            segment.segment.start_time_offset.seconds
            + segment.segment.start_time_offset.microseconds / 1e6
        )
        end_time = (
            segment.segment.end_time_offset.seconds
            + segment.segment.end_time_offset.microseconds / 1e6
        )
        positions = "{}s to {}s".format(start_time, end_time)
        confidence = segment.confidence
        print("\tSegment {}: {}".format(i, positions))
        print("\tConfidence: {}".format(confidence))
    print("\n")

Bahasa tambahan

C#: Ikuti Petunjuk penyiapan C# di halaman library klien lalu buka Dokumentasi referensi Video Intelligence untuk .NET.

PHP: Ikuti petunjuk penyiapan PHP di halaman library klien lalu kunjungi Dokumentasi referensi Video Intelligence untuk PHP.

Ruby: Ikuti petunjuk penyiapan Ruby di halaman library klien, lalu buka Dokumentasi referensi Video Intelligence untuk Ruby.

Selamat! Anda berhasil mengirimkan permintaan pertama ke Video Intelligence API.

Bagaimana hasilnya?

Pembersihan

Agar tidak menimbulkan biaya pada akun Google Cloud Anda untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut.

Langkah selanjutnya