Skip to content

Latest commit

 

History

History
147 lines (100 loc) · 4.23 KB

10-visioneventing.md

File metadata and controls

147 lines (100 loc) · 4.23 KB

Integrate with Vision API

Cloud Vision API is another Machine Learning API of Google Cloud. You can use it to derive insight from your images with powerful pre-trained API models or easily train custom vision models with AutoML Vision.

In this lab, we will use a Cloud Storage bucket to store our images. We will also enable Pub/Sub notifications on our bucket. This way, every time we add an image to the bucket, it will trigger a Pub/Sub message. This in turn will trigger our Knative service where we will use Vision API to analyze the image. Pretty cool!

Since we're making calls to Google Cloud services, you need to make sure that the outbound network access is enabled, as described here.

You also want to make sure that the Vision API is enabled:

gcloud services enable vision.googleapis.com

Create a Vision Handler

Follow the instructions for your preferred language to create a service to handle cloud storage notifications:

Build and push Docker image

Build and push the Docker image (replace {username} with your actual DockerHub):

docker build -t {username}/vision:v1 .

docker push {username}/vision:v1

Deploy the service and trigger

Create a trigger.yaml file.

apiVersion: serving.knative.dev/v1beta1
kind: Service
metadata:
  name: vision
  namespace: default
spec:
  template:
    spec:
      containers:
        # Replace {username} with your actual DockerHub
        - image: docker.io/{username}/vision:v1
---
apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
  name: vision
spec:
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1beta1
      kind: Service
      name: vision

This defines the Knative Service that will run our code and Trigger to connect to Pub/Sub messages.

kubectl apply -f trigger.yaml

Check that the service and trigger are created:

kubectl get ksvc,trigger

Create bucket and enable notifications

Before we can test the service, let's first create a Cloud Storage bucket. You can do this in many ways. We'll use gsutil as follows:

# Unique bucket name
export VISION_BUCKET="$(gcloud config get-value core/project)-vision"

gsutil mb gs://$VISION_BUCKET

Creating gs://VISION_BUCKET/...

Once the bucket is created, enable Pub/Sub notifications on it and link to our testing topic we created in earlier labs:

gsutil notification create -t testing -f json gs://$VISION_BUCKET

Created notification config projects/_/buckets/VISION_BUCKET/notificationConfigs/1

Check that the notification is created:

gsutil notification list gs://$VISION_BUCKET

projects/_/buckets/VISION_BUCKET/notificationConfigs/1
        Cloud Pub/Sub topic: projects/PROJECT_ID/topics/testing

Test the service

We can finally test our Knative service by uploading an image to the bucket.

First, let's watch the logs of the service. Wait a little and check that a pod is created:

kubectl get pods --selector serving.knative.dev/service=vision

You can inspect the logs of the subscriber (replace <podid> with actual pod id):

kubectl logs --follow -c user-container <podid>

Drop the image to the bucket in Google Cloud Console or use gsutil to copy the file as follows:

gsutil cp pics/beach.jpg gs://$VISION_BUCKET

This triggers a Pub/Sub message to our Knative service.

You should see something similar to this:

  • C#

    info: vision.Startup[0]
          This picture is labelled: Sky,Body of water,Sea,Nature,Coast,Water,Sunset,Horizon,Cloud,Shore
    info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
          Request finished in 1948.3204ms 200
    
  • Python

    [INFO] Picture labels: Sky, Body of water, Sea, Nature, Coast, Water, Sunset, Horizon, Cloud, Shore
    

What's Next?

Hello World Build