偵測影片中的煽情露骨內容

「煽情露骨內容偵測」功能會偵測影片中的成人內容。成人內容一般不適合未滿 18 歲者觀賞,包括但不限於裸露、性活動和色情內容。系統也會偵測卡通或動漫中的這類內容。

回覆含有分區塊的可能性值,值的範圍從 VERY_UNLIKELYVERY_LIKELY

煽情露骨內容偵測功能評估影片時,係以單一影格進行偵測且「僅考慮視覺內容」。這項功能不會使用影片的音訊元件來評估煽情露骨內容程度。

下列範例示範如何對位於 Cloud Storage 的檔案使用煽情露骨內容偵測功能來分析影片。

REST

傳送影片註解要求

以下說明如何對 videos:annotate 方法傳送 POST 要求。這個範例使用 Google Cloud CLI 建立存取權杖。如需安裝 gcloud CLI 的操作說明,請參閱 Video Intelligence API 快速入門

使用任何要求資料之前,請先替換以下項目:

  • INPUT_URI:包含要註解檔案的 Cloud Storage bucket,包括檔案名稱。開頭必須為 gs://
    例如: "inputUri": "gs://cloud-videointelligence-demo/assistant.mp4",
  • PROJECT_NUMBER:專案的數值 ID Google Cloud

HTTP 方法和網址:

POST https://videointelligence.googleapis.com/v1/videos:annotate

JSON 要求主體:

{
  "inputUri": "INPUT_URI",
  "features": ["EXPLICIT_CONTENT_DETECTION"]
}

如要傳送要求,請展開以下其中一個選項:

您應該會收到如下的 JSON 回應:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID"
}

如果回應成功,Video Intelligence API 會傳回作業的 name。上例顯示這類回應的範例,其中:

  • PROJECT_NUMBER:專案編號。
  • LOCATION_ID:應進行註解的雲端地區。支援的雲端區域包括:us-east1us-west1europe-west1asia-east1。如果沒有指定任何地區,則會依據影片檔案位置來決定地區。
  • OPERATION_ID:為要求建立的長時間執行作業 ID,並在您開始作業時提供於回應中,例如 12345...

取得註解結果

如要擷取作業結果,請使用呼叫 videos:annotate 時傳回的作業名稱,發出 GET 要求,如下列範例所示。

使用任何要求資料之前,請先替換以下項目:

  • OPERATION_NAME:Video Intelligence API 傳回的作業名稱。作業名稱的格式為 projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID
  • PROJECT_NUMBER:專案的數值 ID Google Cloud

HTTP 方法和網址:

GET https://videointelligence.googleapis.com/v1/OPERATION_NAME

如要傳送要求,請展開以下其中一個選項:

您應該會收到如下的 JSON 回應:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
     {
      "inputUri": "/demomaker/gbikes_dinosaur.mp4",
      "progressPercent": 100,
      "startTime": "2020-03-26T00:16:35.112404Z",
      "updateTime": "2020-03-26T00:16:55.937889Z"
     }
    ]
   },
   "done": true,
   "response": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse",
    "annotationResults": [
     {
      "inputUri": "/demomaker/gbikes_dinosaur.mp4",
      "explicitAnnotation": {
       "frames": [
        {
         "timeOffset": "0.056149s",
         "pornographyLikelihood": "VERY_UNLIKELY"
        },
        {
         "timeOffset": "1.166841s",
         "pornographyLikelihood": "VERY_UNLIKELY"
        },
            ...
        {
         "timeOffset": "41.678209s",
         "pornographyLikelihood": "VERY_UNLIKELY"
        },
        {
         "timeOffset": "42.596413s",
         "pornographyLikelihood": "VERY_UNLIKELY"
        }
       ]
      }
     }
    ]
   }
  }
鏡頭偵測註解會以 shotAnnotations 清單傳回。 注意:只有在 done 欄位的值為 True 時,系統才會傳回這個欄位。 如果作業未完成,則回應不會含有這個欄位。

下載註解結果

將註解從來源複製到目標值區:(請參閱「複製檔案和物件」)

gcloud storage cp gcs_uri gs://my-bucket

注意:如果輸出 GCS URI 是由使用者提供,註解就會儲存在該 GCS URI 中。

Go


func explicitContentURI(w io.Writer, file string) error {
	ctx := context.Background()
	client, err := video.NewClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	op, err := client.AnnotateVideo(ctx, &videopb.AnnotateVideoRequest{
		Features: []videopb.Feature{
			videopb.Feature_EXPLICIT_CONTENT_DETECTION,
		},
		InputUri: file,
	})
	if err != nil {
		return err
	}
	resp, err := op.Wait(ctx)
	if err != nil {
		return err
	}

	// A single video was processed. Get the first result.
	result := resp.AnnotationResults[0].ExplicitAnnotation

	for _, frame := range result.Frames {
		offset, _ := ptypes.Duration(frame.TimeOffset)
		fmt.Fprintf(w, "%s - %s\n", offset, frame.PornographyLikelihood.String())
	}

	return nil
}

Java

如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

// Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient
try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
  // Create an operation that will contain the response when the operation completes.
  AnnotateVideoRequest request =
      AnnotateVideoRequest.newBuilder()
          .setInputUri(gcsUri)
          .addFeatures(Feature.EXPLICIT_CONTENT_DETECTION)
          .build();

  OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response =
      client.annotateVideoAsync(request);

  System.out.println("Waiting for operation to complete...");
  // Print detected annotations and their positions in the analyzed video.
  for (VideoAnnotationResults result : response.get().getAnnotationResultsList()) {
    for (ExplicitContentFrame frame : result.getExplicitAnnotation().getFramesList()) {
      double frameTime =
          frame.getTimeOffset().getSeconds() + frame.getTimeOffset().getNanos() / 1e9;
      System.out.printf("Location: %.3fs\n", frameTime);
      System.out.println("Adult: " + frame.getPornographyLikelihood());
    }
  }

Node.js

如要向 Video Intelligence 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

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

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

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const gcsUri = 'GCS URI of video to analyze, e.g. gs://my-bucket/my-video.mp4';

const request = {
  inputUri: gcsUri,
  features: ['EXPLICIT_CONTENT_DETECTION'],
};

// Human-readable likelihoods
const likelihoods = [
  'UNKNOWN',
  'VERY_UNLIKELY',
  'UNLIKELY',
  'POSSIBLE',
  'LIKELY',
  'VERY_LIKELY',
];

// Detects unsafe content
const [operation] = await client.annotateVideo(request);
console.log('Waiting for operation to complete...');
const [operationResult] = await operation.promise();
// Gets unsafe content
const explicitContentResults =
  operationResult.annotationResults[0].explicitAnnotation;
console.log('Explicit annotation results:');
explicitContentResults.frames.forEach(result => {
  if (result.timeOffset === undefined) {
    result.timeOffset = {};
  }
  if (result.timeOffset.seconds === undefined) {
    result.timeOffset.seconds = 0;
  }
  if (result.timeOffset.nanos === undefined) {
    result.timeOffset.nanos = 0;
  }
  console.log(
    `\tTime: ${result.timeOffset.seconds}` +
      `.${(result.timeOffset.nanos / 1e6).toFixed(0)}s`
  );
  console.log(
    `\t\tPornography likelihood: ${likelihoods[result.pornographyLikelihood]}`
  );
});

Python

如要進一步瞭解如何安裝及使用 Python 專用的 Cloud Video Intelligence API 用戶端程式庫,請參閱「Cloud Video Intelligence API 用戶端程式庫」。
"""Detects explicit content from the GCS path to a video."""
video_client = videointelligence.VideoIntelligenceServiceClient()
features = [videointelligence.Feature.EXPLICIT_CONTENT_DETECTION]

operation = video_client.annotate_video(
    request={"features": features, "input_uri": path}
)
print("\nProcessing video for explicit content annotations:")

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

# Retrieve first result because a single video was processed
for frame in result.annotation_results[0].explicit_annotation.frames:
    likelihood = videointelligence.Likelihood(frame.pornography_likelihood)
    frame_time = frame.time_offset.seconds + frame.time_offset.microseconds / 1e6
    print("Time: {}s".format(frame_time))
    print("\tpornography: {}".format(likelihood.name))

其他語言

C#: 請按照用戶端程式庫頁面上的 C# 設定說明操作, 然後參閱 .NET 適用的 Video Intelligence 參考說明文件

PHP: 請按照用戶端程式庫頁面的 PHP 設定說明 操作,然後前往 PHP 適用的 Video Intelligence 參考文件

Ruby: 請按照用戶端程式庫頁面的 Ruby 設定說明 操作,然後前往 Ruby 適用的 Video Intelligence 參考說明文件