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

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

אתם יכולים לבצע את השלבים שבדף הזה או לנסות את המדריך למתחילים הזה כ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. 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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  6. Install the Google Cloud CLI.

  7. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  8. To initialize the gcloud CLI, run the following command:

    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, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  13. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    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.

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