במדריך הזה נסביר איך להשתמש בפונקציות Cloud Run, ב-Cloud Vision API וב-ImageMagick כדי לזהות ולטשטש תמונות פוגעניות שמועלות לקטגוריה של Cloud Storage.
מטרות
- פריסת פונקציית Cloud Run ברקע שמופעלת על ידי אחסון.
- משתמשים ב-Vision API כדי לזהות תוכן אלים או תוכן למבוגרים בלבד.
- אפשר להשתמש ב-ImageMagick כדי לטשטש תמונות פוגעניות.
- כדי לבדוק את הפונקציה, מעלים תמונה של זומבי אוכל בשר.
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
- Cloud Run functions
- Cloud Storage
- Cloud Vision
כדי ליצור הערכת עלויות בהתאם לשימוש החזוי, אתם יכולים להשתמש במחשבון התמחור.
לפני שמתחילים
- נכנסים לחשבון 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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
הכנת סביבת הפיתוח.
Node.js
Python
המשך
Java
Ruby
אם כבר התקנתם את ה-CLI של gcloud, מריצים את הפקודה הבאה כדי לעדכן אותו:
gcloud components update
המחשת זרימת הנתונים
תהליך העברת הנתונים באפליקציית ההדרכה של ImageMagick כולל כמה שלבים:
- תמונה מועלית לקטגוריה של Cloud Storage.
- הפונקציה מנתחת את התמונה באמצעות Vision API.
- אם מזוהה תוכן אלים או תוכן למבוגרים בלבד, הפונקציה משתמשת ב-ImageMagick כדי לטשטש את התמונה.
- התמונה המטושטשת מועלית לקטגוריה של Cloud Storage אחרת לשימוש.
הכנת הבקשה
יוצרים קטגוריה של Cloud Storage להעלאת תמונות, כאשר
YOUR_INPUT_BUCKET_NAMEהוא שם קטגוריה ייחודי באופן גלובלי:gcloud storage buckets create gs://YOUR_INPUT_BUCKET_NAME
יוצרים קטגוריה של Cloud Storage לקבלת תמונות מטושטשות, כאשר
YOUR_OUTPUT_BUCKET_NAMEהוא שם קטגוריה ייחודי גלובלית:gcloud storage buckets create gs://YOUR_OUTPUT_BUCKET_NAME
משכפלים את מאגר האפליקציות לדוגמה ומעבירים אותו למכונה המקומית:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
אפשרות נוספת היא להוריד את קובץ הדוגמה כקובץ ZIP ולחלץ אותו.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
אפשרות נוספת היא להוריד את קובץ הדוגמה כקובץ ZIP ולחלץ אותו.
המשך
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
אפשרות נוספת היא להוריד את קובץ הדוגמה כקובץ ZIP ולחלץ אותו.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
אפשרות נוספת היא להוריד את קובץ הדוגמה כקובץ ZIP ולחלץ אותו.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
אפשרות נוספת היא להוריד את קובץ הדוגמה כקובץ ZIP ולחלץ אותו.
עוברים לספרייה שמכילה את הקוד לדוגמה של פונקציות Cloud Run:
Node.js
cd nodejs-docs-samples/functions/imagemagick/
Python
cd python-docs-samples/functions/imagemagick/
Go
cd golang-samples/functions/imagemagick/
Java
cd java-docs-samples/functions/imagemagick/
Ruby
cd ruby-docs-samples/functions/imagemagick/
הסבר על הקוד
ייבוא יחסי תלות
כדי שהאפליקציה תוכל ליצור אינטראקציה עם שירותיGoogle Cloud , עם ImageMagick ועם מערכת הקבצים, צריך לייבא אליה כמה יחסי תלות:
Node.js
Python
Go
Java
Ruby
ניתוח תמונות
הפונקציה הבאה מופעלת כשמעלים תמונה לקטגוריית Cloud Storage שיצרתם לאחסון תמונות. הפונקציה משתמשת ב-Vision API כדי לזהות תוכן אלים או תוכן למבוגרים בתמונות שהועלו.
Node.js
Python
Go
Java
Ruby
טשטוש תמונות
הפונקציה הבאה מופעלת כשמזוהה תוכן אלים או תוכן למבוגרים בלבד בתמונה שהועלתה. הפונקציה מורידה את התמונה הפוגעת, משתמשת ב-ImageMagick כדי לטשטש את התמונה, ואז מעלה את התמונה המטושטשת במקום התמונה המקורית.
Node.js
Python
Go
Java
Ruby
פריסת הפונקציה
כדי לפרוס את הפונקציה עם טריגר אחסון, מריצים את הפקודה הבאה בספרייה שמכילה את קוד הדוגמה (או במקרה של Java, את הקובץ pom.xml):
Node.js
gcloud functions deploy blurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Python
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Go
gcloud functions deploy BlurOffensiveImages \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Java
gcloud functions deploy java-blur-function \ --no-gen2 \ --entry-point=functions.ImageMagick \ --runtime=RUNTIME \ --memory 512MB \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
C#
gcloud functions deploy csharp-blur-function \ --no-gen2 \ --entry-point=ImageMagick.Function \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
Ruby
gcloud functions deploy blur_offensive_images \ --no-gen2 \ --runtime=RUNTIME \ --trigger-bucket=YOUR_INPUT_BUCKET_NAME \ --set-env-vars=BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME
מחליפים את מה שכתוב בשדות הבאים:
-
RUNTIME: a runtime that is based on Ubuntu 18.04 (later runtimes don't include support for ImageMagick). -
YOUR_INPUT_BUCKET_NAME: השם של קטגוריית Cloud Storage להעלאת תמונות. -
YOUR_OUTPUT_BUCKET_NAME: השם של הקטגוריה שבה התמונות המטושטשות יישמרו.
בדוגמה הזו, אל תכללו את gs:// כחלק משמות הקטגוריות בפקודה deploy.
העלאת תמונה
העלאת תמונה פוגענית, כמו התמונה הזו של זומבי אוכל בשר:
gcloud storage cp zombie.jpg gs://YOUR_INPUT_BUCKET_NAME
YOUR_INPUT_BUCKET_NAMEהיא קטגוריית Cloud Storage שיצרתם קודם להעלאת תמונות.כדי לוודא שההפעלות הסתיימו, בודקים את היומנים:
gcloud functions logs read --limit 100
אפשר לראות את התמונות המטושטשות בקטגוריה של
YOUR_OUTPUT_BUCKET_NAMECloud Storage שיצרתם קודם.
הסרת המשאבים
כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים או להשאיר את הפרויקט ולמחוק את המשאבים בנפרד.
מחיקת הפרויקט
הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך.
כדי למחוק את הפרויקט:
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.
מחיקת הפונקציה
מחיקה של פונקציות Cloud Run לא מסירה משאבים שמאוחסנים ב-Cloud Storage.
כדי למחוק את הפונקציה שפרסתם במדריך הזה, מריצים את הפקודה הבאה:
Node.js
gcloud functions delete blurOffensiveImages
Python
gcloud functions delete blur_offensive_images
Go
gcloud functions delete BlurOffensiveImages
Java
gcloud functions delete java-blur-function
Ruby
gcloud functions delete blur_offensive_images
אפשר גם למחוק פונקציות Cloud Run מGoogle Cloud המסוף.