במדריך הזה נסביר איך להשתמש בפונקציות 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.
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.-
התקינו את ה-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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Functions, Cloud Build, Cloud Storage, and Cloud Vision APIs.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
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: סביבת זמן ריצה שמבוססת על Ubuntu 18.04 (סביבות זמן ריצה מאוחרות יותר לא כוללות תמיכה ב-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
אפשר לראות את התמונות המטושטשות בקטגוריה של Cloud Storage שיצרתם קודם.
YOUR_OUTPUT_BUCKET_NAME
הסרת המשאבים
כדי להימנע מחיובים בחשבון 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 .