建立入住人數計算應用程式,並使用遠端串流輸入內容

Vertex AI Vision 是一項 AI 輔助平台,可供您擷取、分析及儲存影片 資料。Vertex AI Vision 可讓您建構及部署 AI 應用程式。您可以運用 Vertex AI Vision 與其他產品元件的整合功能,建構端對端 Vertex AI Vision 解決方案。

如要開始使用 Vertex AI Vision 平台導入解決方案,請先瞭解下列 Vertex AI Vision 概念和元件:

  • 串流:代表解決方案中的影片串流層。串流來源可以是即時影像 (例如 IP 攝影機) 或影片檔案 (例如 MP4 檔案)。

  • 應用程式:啟用串流與 AI 處理器之間的連線,對影片執行機器學習作業。舉例來說,您可以將攝影機串流影像連結至 AI 模型,計算攝影機前方經過的人數。

  • 媒體倉儲:將串流擷取的影片儲存至Google Cloud 儲存空間。將資料儲存至這個目的地,即可查詢從擷取串流資料的 AI 處理器輸出和中繼資料。

目標

本教學課程說明如何執行下列操作:

  • 建立入座率計算應用程式。
  • 部署應用程式以供使用。
  • 設定遠端電腦,以便串流播放影片。
  • 將串流影片擷取至應用程式中的串流節點。
  • 在 Vertex AI Vision 的媒體倉儲中搜尋影片。

費用

在本文件中,您會使用下列 Google Cloud的計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine and Vision AI APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the following roles to the service account: Vision AI > Vision AI Editor, Compute Engine > Compute Instance Admin (beta), Storage > Storage Object Viewer.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    7. Click Continue.
    8. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. 安裝 Google Cloud CLI。

  8. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  9. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  10. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  11. Verify that billing is enabled for your Google Cloud project.

  12. Enable the Compute Engine and Vision AI APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  13. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the following roles to the service account: Vision AI > Vision AI Editor, Compute Engine > Compute Instance Admin (beta), Storage > Storage Object Viewer.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    7. Click Continue.
    8. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  14. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  15. 安裝 Google Cloud CLI。

  16. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  17. 執行下列指令,初始化 gcloud CLI:

    gcloud init

如果要從 Cloud Storage bucket 複製影片樣本檔案,才需要這個角色。

建立入座人數計算應用程式

設定環境後,第一步是建立處理資料的應用程式。應用程式可視為自動化管道,連結下列項目:

  • 資料擷取:將影片動態饋給擷取到串流中。
  • 資料分析:擷取資料後,即可加入 AI 模型。您可以對擷取的影片資訊執行任何電腦視覺作業。
  • 資料儲存:影片動態消息的兩個版本 (原始串流和 AI 模型處理的串流) 可以儲存在媒體倉儲中。

在 Google Cloud 控制台中,應用程式會以圖表形式呈現。此外,在 Vertex AI Vision 中,應用程式圖表必須至少有兩個節點:影片來源節點 (串流),以及至少一個以上的節點 (處理模型或輸出目的地)。

建立空白的應用程式

您必須先建立空白應用程式,才能填入應用程式圖表。

控制台

在 Google Cloud 控制台中建立應用程式。

  1. 開啟 Vertex AI Vision 資訊主頁的「應用程式」分頁。

    前往「Applications」(應用程式) 分頁

  2. 按一下「建立」按鈕。

  3. 輸入 occupancy-count-app 做為應用程式名稱,然後選擇您所在的區域。

  4. 點選「建立」

新增應用程式元件節點

建立空白應用程式後,即可將三個節點新增至應用程式圖表:

  1. 擷取節點:串流資源,用於擷取您建立的 Compute Engine VM 執行個體傳送的資料。
  2. 處理節點:根據擷取資料運作的入住率分析模型。
  3. 儲存節點:儲存處理過的影片,同時也是中繼資料儲存空間的媒體倉儲。倉儲可生成有關擷取影片資料的分析資訊,以及 AI 模型推斷的資料資訊。

控制台

在控制台將元件節點新增至應用程式。

  1. 開啟 Vertex AI Vision 資訊主頁的「應用程式」分頁。

    前往「Applications」(應用程式) 分頁

  2. occupancy-count-app 行中,選取「查看圖表」。系統會將您導向處理管道的圖表視覺化畫面。

新增資料擷取節點

  1. 如要新增輸入串流節點,請在側邊選單的「Connectors」(連接器) 區段中選取「Streams」(串流) 選項。

  2. 在隨即開啟的「Stream」(串流) 選單中,選取「Source」(來源) 區段的 「Add Stream」(新增串流)

  3. 在「新增串流」選單中,選擇「註冊新串流」,然後新增 occupancy-count-stream 做為串流名稱。

  4. 如要將串流新增至應用程式圖表,請按一下「新增串流」

新增資料處理節點

  1. 如要新增入住人數計數模型節點,請在側邊選單的「Specialized models」(專業模型) 區段中,選取「occupancy analytics」(入住人數分析) 選項。

  2. 保留預設選取項目「人物」和「車輛」

新增資料儲存節點

  1. 如要新增輸出目的地 (儲存空間) 節點,請在側邊選單的「Connectors」(連接器) 區段中選取「Vertex AI Vision's Media Warehouse」(Vertex AI Vision 的媒體倉儲) 選項。

  2. Vertex AI Vision 的媒體倉儲 選單中,按一下 連結倉儲

  3. 在「Connect Warehouse」(連結倉儲) 選單中,選取 「Create new warehouse」(新建倉儲)。將倉儲命名為 occupancy-count-warehouse,並將 TTL 持續時間保留 14 天。

  4. 按一下「Create」(建立) 按鈕來新增倉儲。

部署應用程式以供使用

使用所有必要元件建構端對端應用程式後,最後一個步驟就是部署應用程式。

控制台

  1. 開啟 Vertex AI Vision 資訊主頁的「應用程式」分頁。

    前往「Applications」(應用程式) 分頁

  2. 在清單中,選取 occupancy-count-app 應用程式旁邊的「查看圖表」

  3. 在應用程式圖表建構工具頁面中,按一下「部署」按鈕。

  4. 在隨即顯示的確認對話方塊中,選取「部署」

    部署作業可能需要幾分鐘才能完成。部署作業完成後,節點旁會顯示綠色勾號。

    使用者介面中已部署的應用程式

設定遠端電腦來串流影片

您已部署入住人數計數應用程式,準備接收、處理及儲存串流資料,現在必須將影片資料串流至應用程式。

在本教學課程中,您將建立代管影片的 Compute Engine VM 執行個體,並從該 VM 傳送串流影片資料。

建立 Linux VM

從 Compute Engine VM 執行個體傳送影片的第一步,是建立 VM 執行個體。

控制台

  1. 前往控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 選取您的專案並點選 [繼續]

  3. 點選「建立執行個體」

  4. 指定 VM 的「Name」(名稱)。詳情請參閱資源命名慣例

  5. 選用:變更這個 VM 的「Zone」(區域)。Compute Engine 會隨機排列每個地區內的區域清單,以鼓勵跨多個區域使用。

  6. 接受其餘預設選項。如要進一步瞭解這些選項,請參閱「建立及啟動 VM」。

  7. 如要建立並啟動 VM,請按一下 [Create] (建立)

設定 VM 環境

VM 啟動後,您可以使用控制台在瀏覽器中建立 SSH 連線。建立連線後,您就可以下載 vaictl 指令列工具,將影片擷取至應用程式。

控制台

與 VM 建立 SSH 連線

  1. 前往控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 在您建立的執行個體列的「連線」部分,按一下「SSH」。系統會在新的瀏覽器視窗中開啟 SSH 連線。

    UI 中的 SSH 選項

下載 vaictl 指令列工具

  1. 在「直接透過瀏覽器進行 SSH 連線」視窗中,使用下列指令下載 Vertex AI Vision (vaictl) 指令列工具:

    wget https://github.com/google/visionai/releases/download/v0.0.5/visionai_0.0-5_amd64.deb
    
  2. 執行下列指令來安裝指令列工具:

    sudo apt install ./visionai_0.0-5_amd64.deb
    
  3. 執行下列指令即可測試安裝作業:

    vaictl --help
    

將影片檔案擷取至應用程式

設定 VM 環境後,您可以複製影片樣本,然後使用 vaictl 將影片資料串流至入住人數計數應用程式。

直接透過瀏覽器進行 SSH 連線

將影片樣本複製到 VM

  1. 在 VM 的「直接透過瀏覽器進行 SSH 連線」視窗中,使用下列 gcloud storage cp 指令複製範例影片。請替換下列變數:
    • SOURCE:要使用的影片檔案位置。你可以使用自己的影片檔案來源 (例如 gs://BUCKET_NAME/FILENAME.mp4),或使用下列其中一個範例影片:
      • gs://cloud-samples-data/vertex-ai-vision/street_vehicles_people.mp4 (含人物和車輛的影片,來源)
      • gs://cloud-samples-data/vertex-ai-vision/highway_vehicles.mp4 (僅含車輛的影片,來源)
    gcloud storage cp SOURCE .

從 VM 串流影片,並將資料擷取至應用程式

  1. 如要將這個本機影片檔案傳送至應用程式輸入串流,請使用下列指令。您必須進行下列變數替換:
    • PROJECT_ID: Google Cloud 專案 ID。
    • LOCATION_ID:您的位置 ID。例如:us-central1。詳情請參閱「Cloud 服務據點」。
    • LOCAL_FILE.EXT:本機影片檔案的檔案名稱。 例如:my-video.mp4
    • --loop 旗標:選用。循環處理檔案資料,模擬串流。

    這項指令會將影片檔案串流至串流。如果您使用 --loop 旗標,影片會循環播放到串流中,直到您停止指令為止:

    vaictl -p PROJECT_ID \
        -l LOCATION_ID \
        -c application-cluster-0 \
        --service-endpoint visionai.googleapis.com \
    send video-file to streams 'occupancy-count-stream' --file-path LOCAL_FILE.EXT --loop

從開始擷取作業到影片顯示在資訊主頁,可能需要約 100 秒。vaictl

串流擷取功能上線後,選取 occupancy-count-stream 串流,即可在 Vertex AI Vision 資訊主頁的「串流」分頁中查看影片動態饋給。

前往「串流」分頁

UI 中串流的即時影像
在控制台中,即時監控串流擷取的影片。 Google Cloud 影片來源: Elizabeth Mavor on Pixabay (pixelation added).

在儲存倉儲中搜尋影片內容

將影片資料擷取至處理應用程式後,即可查看分析後的影片資料,並根據入座率數據分析資訊搜尋資料。

控制台

  1. 開啟 Vertex AI Vision 資訊主頁的「倉庫」分頁。

    前往「倉庫」分頁

  2. 在清單中找出 occupancy-count-warehouse 倉庫,然後按一下「查看資產」

  3. 在「人數」或「車輛數」部分,將「最小值」設為 1,並將「最大值」設為 5

  4. 如要篩選儲存在 Vertex AI Vision Media Warehouse 中的已處理影片資料,請按一下「搜尋」

    使用者介面中儲存資料的切面檢視畫面
    在 Google Cloud 控制台中,檢視符合搜尋條件的已儲存影片資料。影片出處: Elizabeth Mavor (套用搜尋條件) 於 Pixabay 提供。

清除所用資源

為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

刪除專案

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除個別資源

刪除 Compute Engine VM 執行個體

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 選取要刪除的執行個體旁的核取方塊。
  3. 如要刪除執行個體,請依序點選 「More actions」(更多動作) 和「Delete」(刪除),然後按照指示操作。

刪除倉儲

  1. 前往 Google Cloud 控制台的「倉庫」頁面。

    前往「倉庫」分頁

  2. 找到 你的occupancy-count-warehouse倉庫。
  3. 如要刪除倉庫,請依序點選 「動作」和「刪除倉庫」,然後按照指示操作。

刪除串流

  1. 前往 Google Cloud 控制台的「Streams」頁面。

    前往「串流」分頁

  2. 找出 occupancy-count-stream 串流。
  3. 如要刪除串流,請依序點選 「動作」和「刪除串流」,然後按照指示操作。

刪除應用程式

  1. 前往 Google Cloud 控制台的「Applications」頁面。

    前往「應用程式」分頁

  2. 找出 要刪除的 occupancy-count-app 應用程式。
  3. 如要刪除應用程式,請依序點選 「Actions」(動作) 和「Delete application」(刪除應用程式),然後按照指示操作。

後續步驟