מטרות
בדוגמה הזו, תשתמשו ב-Google Vision API כדי לזהות פנים בתמונה. כדי לוודא שהפנים זוהו בצורה נכונה, תשתמשו בנתונים האלה כדי לצייר תיבה סביב כל פנים.
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
- 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 Google Cloud Vision API.
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.-
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 Google Cloud Vision API.
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.- מגדירים את הסביבה לשימוש ב- Application Default Credentials.
-
הגדרת משימות וכלים ספציפיים לשפה:
C#
- התקנה של ספריית הלקוח של Google
- מתקינים את Visual Studio 2012 או Visual Studio 2015.
Java
- מתקינים את Java.
- הפניית API
מורידים ומתקינים את מערכת הבנייה Apache Maven. Maven יוודא שספריית הלקוח של Google API וספריות הלקוח של Vision API יותקנו כשהפרויקט ייבנה, כי הוספנו אותן ל-
pom.xml....
Node.js
- התקנה של ספריית הלקוח של Google
- מתקינים את node.js.
- הפניית API
מתקינים את npm ואת node-canvas. הקוד לדוגמה כולל את הפקודה
package.jsonלהתקנת כל הרכיבים התלויים באמצעות הפקודה:npm install. שימו לב ש-node-canvas כולל יחסי תלות נוספים שאולי תצטרכו להתקין. מידע נוסף זמין במסמך ההתקנה של node-canvas.
PHP
- התקנה של ספריית הלקוח של Google
- מתקינים PHP ו-Composer.
- הפניית API
- מתקינים ומפעילים את GD for PHP.
Python
- התקנה של ספריית הלקוח של Google
- מתקינים את Python.
- הפניית API
- מתקינים את pillow.
Ruby
- התקנה של ספריית הלקוח של Google
- מתקינים את Ruby ואת Bundler.
- הפניית API
- מתקינים את ImageMagick.
יצירת אובייקט השירות
כדי לגשת לממשקי API של Google באמצעות ערכות ה-SDK הרשמיות של הלקוח, יוצרים אובייקט שירות שמבוסס על מסמך הגילוי של ה-API, שמתאר את ה-API ל-SDK. תצטרכו לאחזר אותו משירות הגילוי של Vision API באמצעות פרטי הכניסה שלכם:
Java
Node.js
Python
שליחת בקשה לזיהוי פנים
כדי ליצור בקשה ל-Vision API, קודם צריך לעיין במסמכי ה-API. במקרה כזה, תבקשו מהמשאב images annotate את התמונה. בקשה ל-API הזה היא אובייקט עם רשימה של requests. כל פריט ברשימה הזו מכיל שני חלקי מידע:
- נתוני התמונה בקידוד Base64
- רשימה של תכונות שרוצים להוסיף להן הערות לגבי התמונה.
בדוגמה הזו, פשוט תבקשו הערה FACE_DETECTION על תמונה אחת, ותציגו את החלק הרלוונטי בתגובה:
Java
Node.js
Python
עיבוד התשובה
כל הכבוד – זיהית את הפנים בתמונה! התשובה לבקשת תיוג הפנים שלנו כוללת הרבה מטא-נתונים על הפנים שזוהו, כולל קואורדינטות של מצולע שמקיף את הפנים. אבל בשלב הזה, זו רק רשימה של מספרים. נשתמש בהם כדי לוודא שבאמת מצאת את הפנים בתמונה. ניצור מצולעים על עותק של התמונה, באמצעות הקואורדינטות שמוחזרות על ידי Vision API:
Java
Node.js
אנחנו משתמשים בספרייה node-canvas כדי לצייר על תמונות.
Python
סיכום של כל המידע
Java
...
כדי ליצור ולהריץ את הדוגמה, מריצים את הפקודות הבאות מהספרייה של קוד הדוגמה:
mvn clean compile assembly:single
java -cp target/vision-face-detection-1.0-SNAPSHOT-jar-with-dependencies.jar \
com.google.cloud.vision.samples.facedetect.FaceDetectApp \
data/face.jpg \
output.jpg
Node.js
כדי להריץ את הדוגמה, מריצים את הפקודה הבאה מהספרייה של קוד הדוגמה:
node faceDetection resources/face.png
Python
הסרת המשאבים
כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים, או להשאיר את הפרויקט ולמחוק את המשאבים בנפרד.
- במסוף Google Cloud , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.