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 Services
at the top. - Search for
Vision
. - Click the first result.
- Click the
Enable
button.
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 API
then 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
Trigger
fromHTTP
toCloud Storage
. - For the
Bucket
field, clickBrowse
and 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
Create
to create a Cloud Storage bucket. - Click
Select
to select this bucket as the trigger for your Cloud Function. - Change the
Runtime
toPython 3.7
. - Copy and paste the
main.py
code from this code sample into themain.py
file in the inline editor. - Copy and paste the
requirements.txt
code from this code sample into therequirements.txt
file in the inline editor. - Change
Function to execute
toprocess_image
. - Click the
Create
button 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 Files
button 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
photos
collection and a document within it named the same as your uploaded image. - Within that document, make sure that there is a
labelsDetected
field 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 days
section at the bottom. You can also click theView Logs
button at the top of your Cloud Function's dashboard.