הוספת הערה לסרטון באמצעות שורת הפקודה
בדף הזה מוסבר איך ליצור בקשת API ל-Video Intelligence API באמצעות curl לפרויקטים שבבעלותכם.
אתם יכולים לפעול לפי השלבים שבדף הזה או לנסות את המדריך לתחילת העבודה בתורGoogle Cloud תרגול ב-Lab.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
gcloud init
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות להוספת הערות לסרטון, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:
- צרכן שימוש בשירות (
roles/serviceusage.serviceUsageConsumer) - אדמין באחסון (
roles/storage.admin)
להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.
יצירת קטגוריה
בסביבת הפיתוח, משתמשים בפקודה 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
אם אתם לא צריכים את הפרויקט שיצרתם במדריך למתחילים, אתם יכולים למחוק אותו.
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.
המאמרים הבאים
- קוראים את המדריכים.
- כדאי לעיין בדוגמאות ומדריכים.
- למידע מלא על כל הקריאות ל-API, אפשר לעיין בהפניית API.