搭配 C# 使用 Video Intelligence API

1. 總覽

Google Cloud Video Intelligence API 可讓開發人員在建立應用程式時使用 Google 影片分析技術。

可用於:

REST API 可讓使用者為儲存在本機或 Google Cloud Storage 的影片加上註解,註解可按照整部影片、每個片段、每個鏡頭及每個影格的層級提供內容資訊。

在本程式碼研究室中,您將著重於使用 C# 搭配 Video Intelligence API。您將瞭解如何分析影片中的標籤、鏡頭轉換和煽情露骨內容偵測。

課程內容

  • 如何使用 Cloud Shell
  • 如何啟用 Video Intelligence API
  • 如何驗證 API 要求
  • 如何安裝 C# 適用的 Google Cloud 用戶端程式庫
  • 如何分析影片中的標籤
  • 如何分析影片中的鏡頭轉換
  • 如何分析影片中的煽情露骨內容

軟硬體需求

  • Google Cloud Platform 專案
  • ChromeFirefox 瀏覽器
  • 熟悉 C# 的使用方式

問卷調查

您會如何使用本教學課程?

僅閱讀 閱讀並完成練習

您對 C# 的體驗評價如何?

新手 中級 熟練

您對使用 Google Cloud Platform 服務的體驗有何評價?

新手 中級 熟練

2. 設定和需求

自修實驗室環境設定

  1. 登入 Google Cloud 控制台,然後建立新專案或重複使用現有專案。如果沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶

295004821bab6a87.png

37d264871000675d.png

96d86d3d5655cdbe.png

  • 專案名稱是這個專案參與者的顯示名稱。這是 Google API 未使用的字元字串。你隨時可以更新。
  • 專案 ID 在所有 Google Cloud 專案中都是不重複的,而且設定後即無法變更。Cloud 控制台會自動產生專屬字串,通常您不需要在意該字串為何。在大多數程式碼研究室中,您需要參照專案 ID (通常標示為 PROJECT_ID)。如果您不喜歡產生的 ID,可以產生另一個隨機 ID。你也可以嘗試使用自己的名稱,看看是否可用。完成這個步驟後就無法變更,且專案期間會維持不變。
  • 請注意,有些 API 會使用第三個值,也就是「專案編號」。如要進一步瞭解這三種值,請參閱說明文件
  1. 接著,您需要在 Cloud 控制台中啟用帳單,才能使用 Cloud 資源/API。完成這個程式碼研究室的費用不高,甚至可能完全免費。如要關閉資源,避免在本教學課程結束後繼續產生費用,請刪除您建立的資源或專案。Google Cloud 新使用者可參加價值$300 美元的免費試用計畫。

啟動 Cloud Shell

雖然可以透過筆電遠端操作 Google Cloud,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是可在雲端執行的指令列環境。

啟用 Cloud Shell

  1. 在 Cloud 控制台,點選「啟用 Cloud Shell」 圖示 d1264ca30785e435.png

cb81e7c8e34bc8d.png

如果您是首次啟動 Cloud Shell,系統會顯示中繼畫面,說明這個指令列環境。如果出現中繼畫面,請按一下「繼續」

d95252b003979716.png

佈建並連至 Cloud Shell 預計只需要幾分鐘。

7833d5e1c5d18f54.png

這部虛擬機器已載入所有必要的開發工具,並提供永久的 5 GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。本程式碼研究室幾乎所有工作都可在瀏覽器上完成。

連至 Cloud Shell 後,您應該會看到驗證已完成,專案也已設為獲派的專案 ID。

  1. 在 Cloud Shell 中執行下列指令,確認您已通過驗證:
gcloud auth list

指令輸出

 Credentialed Accounts
ACTIVE  ACCOUNT
*       <my_account>@<my_domain.com>

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
  1. 在 Cloud Shell 中執行下列指令,確認 gcloud 指令知道您的專案:
gcloud config list project

指令輸出

[core]
project = <PROJECT_ID>

如未設定,請輸入下列指令手動設定專案:

gcloud config set project <PROJECT_ID>

指令輸出

Updated property [core/project].

3. 啟用 Video Intelligence API

您必須先啟用 API,才能使用 Video Intelligence API。您可以在 Cloud Shell 中使用下列指令啟用 API:

gcloud services enable videointelligence.googleapis.com

4. 安裝 C# 適用的 Google Cloud Video Intelligence API 用戶端程式庫

首先,請建立簡單的 C# 控制台應用程式,用來執行 Video Intelligence API 範例:

dotnet new console -n VideoIntApiDemo

您應該會看到建立的應用程式和已解決的依附元件:

The template "Console Application" was created successfully.
Processing post-creation actions...
...
Restore succeeded.

接著前往 VideoIntApiDemo 資料夾:

cd VideoIntApiDemo/

並將 Google.Cloud.VideoIntelligence.V1 NuGet 套件新增至專案:

dotnet add package Google.Cloud.VideoIntelligence.V1
info : Adding PackageReference for package 'Google.Cloud.VideoIntelligence.V1' into project '/home/atameldev/VideoIntApiDemo/VideoIntApiDemo.csproj'.
log  : Restoring packages for /home/atameldev/VideoIntApiDemo/VideoIntApiDemo.csproj...
...
info : PackageReference for package 'Google.Cloud.VideoIntelligence.V1' version '1.0.0' added to file '/home/atameldev/VideoIntApiDemo/VideoIntApiDemo.csproj'.

您現在可以使用 Video Intelligence API 了!

5. 標籤偵測

標籤分析會偵測儲存在本機或 Google Cloud Storage 中的影片標籤。在本節中,您將分析儲存在 Google Cloud Storage 中的影片標籤。

首先,從 Cloud Shell 右上方開啟程式碼編輯器:

fd3fc1303e63572.png

前往 VideoIntApiDemo 資料夾中的 Program.cs 檔案,並將程式碼替換成以下程式碼:

using System;
using System.Collections.Generic;
using Google.Cloud.VideoIntelligence.V1;

namespace VideoIntApiDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = VideoIntelligenceServiceClient.Create();
            var request = new AnnotateVideoRequest
            {
                InputUri = "gs://cloud-samples-data/video/gbikes_dinosaur.mp4",
                Features = { Feature.LabelDetection }
            };
            var op = client.AnnotateVideo(request).PollUntilCompleted();
            foreach (var result in op.Result.AnnotationResults)
            {
                PrintLabels("Video", result.SegmentLabelAnnotations);
                PrintLabels("Shot", result.ShotLabelAnnotations);
                PrintLabels("Frame", result.FrameLabelAnnotations);
            }
        }

        static void PrintLabels(string labelName,
            IEnumerable<LabelAnnotation> labelAnnotations)
        {
            foreach (var annotation in labelAnnotations)
            {
                Console.WriteLine($"{labelName} label: {annotation.Entity.Description}");
                foreach (var entity in annotation.CategoryEntities)
                {
                    Console.WriteLine($"{labelName} label category: {entity.Description}");
                }
                foreach (var segment in annotation.Segments)
                {
                    Console.Write("Segment location: ");
                    Console.Write(segment.Segment.StartTimeOffset);
                    Console.Write(":");
                    Console.WriteLine(segment.Segment.EndTimeOffset);
                    Console.WriteLine($"Confidence: {segment.Confidence}");
                }
            }
        }
    }
}

請花一到兩分鐘研究程式碼,瞭解影片的標籤方式。

回到 Cloud Shell,執行應用程式:

dotnet run

Video Intelligence API 擷取標籤需要幾秒鐘的時間,但最終您應該會看到下列輸出內容:

Video label: bicycle
Video label category: vehicle
Segment location: "0s":"42.766666s"
Confidence: 0.475821
Video label: tyrannosaurus
Video label category: dinosaur
Segment location: "0s":"42.766666s"
Confidence: 0.4222222
Video label: tree
Video label category: plant
Segment location: "0s":"42.766666s"
Confidence: 0.4231415
...

摘要

在這個步驟中,您已使用 Video Intelligence API 列出影片中的所有標籤。詳情請參閱「標籤偵測」頁面。

6. 偵測鏡頭轉換

您可以使用 Video Intelligence API 偵測儲存在本機或 Google Cloud Storage 的影片中的鏡頭變化。在本節中,您將對位於 Google Cloud Storage 的檔案執行鏡頭轉換的影片分析。

如要偵測鏡頭變化,請前往 VideoIntApiDemo 資料夾中的 Program.cs 檔案,然後將程式碼替換為下列程式碼:

using System;
using Google.Cloud.VideoIntelligence.V1;

namespace VideoIntApiDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = VideoIntelligenceServiceClient.Create();
            var request = new AnnotateVideoRequest
            {
                InputUri = "gs://cloud-samples-data/video/gbikes_dinosaur.mp4",
                Features = { Feature.ShotChangeDetection }
            };
            var op = client.AnnotateVideo(request).PollUntilCompleted();
            foreach (var result in op.Result.AnnotationResults)
            {
                foreach (var annotation in result.ShotAnnotations)
                {
                    Console.Out.WriteLine("Start Time Offset: {0}\tEnd Time Offset: {1}",
                        annotation.StartTimeOffset, annotation.EndTimeOffset);
                }
            }
        }
    }
}

請花一到兩分鐘研究程式碼,瞭解鏡頭偵測的執行方式。

返回 Cloud Shell 執行應用程式,您應該會看到下列輸出內容:

dotnet run

您應該會看到以下的輸出內容:

Start Time Offset: "0s" End Time Offset: "5.166666s"
Start Time Offset: "5.233333s"  End Time Offset: "10.066666s"
Start Time Offset: "10.100s"    End Time Offset: "28.133333s"
Start Time Offset: "28.166666s" End Time Offset: "42.766666s"

摘要

在這個步驟中,您已能使用 Video Intelligence API 偵測儲存在 Google Cloud Storage 中的檔案是否發生鏡頭轉換。進一步瞭解鏡頭轉換

7. 煽情露骨內容偵測

「煽情露骨內容偵測」功能會偵測影片中的成人內容。成人內容是一般適合 18 歲以上成人的內容,包括但不限於裸露、性活動和色情內容 (包括卡通或動漫)。回覆含有分區塊的可能性值,值的範圍從 VERY_UNLIKELY 到 VERY_LIKELY。

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

如要偵測露骨內容,請前往 VideoIntApiDemo 資料夾中的 Program.cs 檔案,然後將程式碼替換為下列程式碼:

using System;
using Google.Cloud.VideoIntelligence.V1;

namespace VideoIntApiDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = VideoIntelligenceServiceClient.Create();
            var request = new AnnotateVideoRequest
            {
                InputUri = "gs://cloud-samples-data/video/gbikes_dinosaur.mp4",
                Features = { Feature.ExplicitContentDetection }
            };
            var op = client.AnnotateVideo(request).PollUntilCompleted();
            foreach (var result in op.Result.AnnotationResults)
            {
                foreach (var frame in result.ExplicitAnnotation.Frames)
                {
                    Console.WriteLine("Time Offset: {0}", frame.TimeOffset);
                    Console.WriteLine("Pornography Likelihood: {0}", frame.PornographyLikelihood);
                    Console.WriteLine();
                }
            }
        }
    }
}

請花一到兩分鐘研究程式碼,瞭解系統如何偵測露骨內容*.*

回到 Cloud Shell,執行應用程式:

dotnet run

這可能需要幾秒鐘,但最終您應該會看到下列輸出內容:

dotnet run

Time Offset: "0.056149s"
Pornography Likelihood: VeryUnlikely

Time Offset: "1.166841s"
Pornography Likelihood: VeryUnlikely
...
Time Offset: "41.678209s"
Pornography Likelihood: VeryUnlikely

Time Offset: "42.596413s"
Pornography Likelihood: VeryUnlikely

摘要

在這個步驟中,您已使用 Video Intelligence API 對影片執行煽情露骨內容偵測。進一步瞭解情色露骨內容偵測

8. 恭喜!

您已瞭解如何使用 C# 存取 Video Intelligence API!

清除所用資源

如何避免系統向您的 Google Cloud Platform 帳戶收取您在本快速入門導覽課程中所用資源的相關費用:

  • 前往 Cloud Platform Console
  • 選取要關閉的專案,然後按一下頂端的「刪除」,系統就會排定刪除專案的時間。

瞭解詳情

授權

這項內容採用的授權為 Creative Commons 姓名標示 2.0 通用授權。