הוספת הערה לסרטון באמצעות שורת הפקודה

בדף הזה מוסבר איך ליצור בקשת API ל-Video Intelligence API באמצעות curl לפרויקטים שבבעלותכם.

אתם יכולים לפעול לפי השלבים שבדף הזה או לנסות את המדריך לתחילת העבודה בתורGoogle Cloud תרגול ב-Lab.

אפשר לנסות את התכונה באמצעות ניסוי

לפני שמתחילים

  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. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Video Intelligence API.

    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 API

  6. התקינו את ה-CLI של Google Cloud.

  7. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  8. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  9. 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

  10. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  12. Enable the Video Intelligence API.

    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 API

  13. התקינו את ה-CLI של Google Cloud.

  14. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  15. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות להוספת הערות לסרטון, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

יצירת קטגוריה

בסביבת הפיתוח, משתמשים בפקודה gcloud storage buckets create כדי ליצור קטגוריה.

לפני שמריצים את הפקודה, מחליפים את הערכים הבאים:

  • BUCKET_LOCATION: המיקום הגיאוגרפי שבו רוצים לאחסן את הנתונים בקטגוריה. לדוגמה, US.
gcloud storage buckets create gs://videointelligence-quickstart-bucket --location=BUCKET_LOCATION

אם הבקשה מבוצעת בהצלחה, הפקודה תחזיר את ההודעה הבאה:

Creating gs://videointelligence-quickstart-bucket/...

העלאת סרטון לקטגוריה

בסביבת הפיתוח, משתמשים בפקודה gcloud storage cp כדי להעלות סרטון לקטגוריה שיצרתם.

לפני שמריצים את הפקודה, מחליפים את הערכים הבאים:

  • OBJECT_LOCATION: הנתיב המקומי לאובייקט. לדוגמה, Desktop/dog.mp4.
gcloud storage cp OBJECT_LOCATION gs://videointelligence-quickstart-bucket

אם הפעולה בוצעה ללא שגיאות, התשובה תיראה כמו בדוגמה הבאה:

Completed files 1/1 | 164.3kiB/164.3kiB

כתיבת הערות לסרטון

gcloud

משתמשים ב-CLI של gcloud כדי להפעיל את הפקודה detect-labels בנתיב של הסרטון שרוצים לנתח.

לפני שמריצים את הפקודה, מחליפים את הערכים הבאים:

  • YOUR_OBJECT: שם הקובץ של הסרטון שהעליתם. לדוגמה, dog.mp4.
gcloud ml video detect-labels gs://videointelligence-quickstart-bucket/YOUR_OBJECT

שורת פקודה

משתמשים ב-curl כדי לשלוח בקשת POST ל-method‏ videos:annotate.

לפני שמריצים את הפקודה, מחליפים את הערכים הבאים:

  • YOUR_OBJECT: שם הקובץ של הסרטון שהעליתם. לדוגמה, dog.mp4.
curl -X POST \
  -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
  -H "Content-Type: application/json; charset=utf-8" \
  --data '{"inputUri":"gs://videointelligence-quickstart-bucket/YOUR_OBJECT","features":["LABEL_DETECTION"]}'\
  "https://videointelligence.googleapis.com/v1/videos:annotate"

ממשק Video Intelligence API יוצר פעולה לעיבוד הבקשה. התשובה כוללת את שם הפעולה:

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

אפשר לבקש מידע על הפעולה על ידי קריאה לנקודת הקצה v1.operations, ולהחליף את OPERATION_NAME בדוגמה שלמטה בשם שהוחזר בשלב הקודם:

  curl -X GET \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    https://videointelligence.googleapis.com/v1/OPERATION_NAME

הפקודה הזו מחזירה מידע שקשור לפעולה שלכם. אם הפעולה הושלמה, השדה done נכלל ומוגדר ל-true:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
  "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT",
        "progressPercent": 100,
        "startTime": "2020-04-01T22:13:17.978847Z",
        "updateTime": "2020-04-01T22:13:29.576004Z"
      }
    ]
  },
  "done": true,
  ...
}

אחרי דקה בערך, הבקשה מחזירה את תוצאות ההערות. התוצאות אמורות להיראות כך:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "videointelligence-quickstart-bucket/YOUR_OBJECT",
        "progressPercent": 100,
        "startTime": "2020-04-01T22:13:17.978847Z",
        "updateTime": "2020-04-01T22:13:29.576004Z"
      }
    ]
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse",
    "annotationResults": [
      {
        "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT",
        "segmentLabelAnnotations": [
          {
            "entity": {
              "entityId": "/m/07bsy",
              "description": "transport",
              "languageCode": "en-US"
            },
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.81231534
              }
            ]
          },
         {
          "entity": {
              "entityId": "/m/01n32",
              "description": "city",
              "languageCode": "en-US"
            },
            "categoryEntities": [
              {
                "entityId": "/m/043rvww",
                "description": "geographical feature",
                "languageCode": "en-US"
              }
            ],
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.3942462
              }
            ]
          },
          ...
          {
            "entity": {
              "entityId": "/m/06gfj",
              "description": "road",
              "languageCode": "en-US"
            },
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.86698604
              }
            ]
          }
        ]
      }
    ]
  }
}
   

כל הכבוד! שלחת את הבקשה הראשונה אל Video Intelligence API.

הסרת המשאבים

כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, אתם יכולים:

  • אם רוצים להמשיך להשתמש בפרויקט שיצרתם, משתמשים בפקודה gcloud storage rm עם הדגל --recursive כדי למחוק את הקטגוריה ואת כל מה שבתוכה:

    gcloud storage rm gs://videointelligence-quickstart-bucket --recursive
  • אם אתם לא צריכים את הפרויקט שיצרתם במדריך למתחילים, אתם יכולים למחוק אותו.

    1. במסוף Google Cloud , נכנסים לדף Manage resources.

      כניסה לדף Manage resources

    2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
    3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

המאמרים הבאים