This sample demonstrates how to use the following Google Cloud products together:
- Google Cloud Functions – Core logic
- Google Cloud Storage – Image storage
- Google Cloud Vision API – Image machine learning
- Google Cloud Firestore – Results database
- Create a project with the Google Cloud Platform Console Cloud Resource Manager.
- Make note of your project ID, which may be different than your project name.
- Make sure to Enable Billing for your project.
- Go to the Google Cloud Platform Console
- Click the button in the top left and select
Firestore.- Click
Select Native Mode. - Select a region close to you geographically.
- Click
Create Database. - Wait for your database to be created.
- Click
- Go to the Google Cloud Platform Console
- Click the button in the top left.
- Select
APIs & services. - Click
Enable APIs and Servicesat the top. - Search for
Vision. - Click the first result.
- Click the
Enablebutton.
Run the following command to clone the Github repository:
git clone https://github.com/GoogleCloudPlatform/hackathon-toolkit.git
Change directory to the sample code location:
cd hackathon-toolkit/functions
Deploy the code to Google Cloud Functions:
- Go to the Google Cloud Platform Console
- Click the button in the top left and select
Cloud Functions. - Click
Enable APIthen clickCreate Function.
Create a Google Cloud Function with a Cloud Storage trigger in Python:
- Give your new Cloud Function the name
process_image. - Change the
TriggerfromHTTPtoCloud Storage. - For the
Bucketfield, clickBrowseand then click the basket icon (New bucket) to create a new Cloud Storage bucket. - Name the bucket the same as your Google Cloud project.
- Leave all fields the same and click
Createto create a Cloud Storage bucket. - Click
Selectto select this bucket as the trigger for your Cloud Function. - Change the
RuntimetoPython 3.7. - Copy and paste the
main.pycode from this code sample into themain.pyfile in the inline editor. - Copy and paste the
requirements.txtcode from this code sample into therequirements.txtfile in the inline editor. - Change
Function to executetoprocess_image. - Click the
Createbutton at the bottom to start the deployment process. Your Cloud Function should take <2 minutes to deploy.
Test the Google Cloud Function by first going to Storage to upload a file:
- Go to the Google Cloud Platform Console.
- Click the button in the top left, scroll down and select
Storage - Select the Cloud Storage bucket you created in the previous section.
- Click the
Upload Filesbutton and select an image from your computer to upload.
- Click the button in the top left, scroll down and select
- After the image uploads, click the navigation menu in the top left of your Google Cloud Platform Console and select
Firestore.- Make sure you have a
photoscollection and a document within it named the same as your uploaded image. - Within that document, make sure that there is a
labelsDetectedfield with an array of labels detected using the Vision API.
- Make sure you have a
Note: If you have any issues, visit the dashboard for your Cloud Function in the Cloud Functions section of the Google Cloud Platform Console. See if there are any errors in the
Errors in the last 7 dayssection at the bottom. You can also click theView Logsbutton at the top of your Cloud Function's dashboard.