0

I'm trying to run Aws Lambda function which calls Google Vision API when someone uploads picture to s3 bucket.

Here is my sample code

import json
import boto3

def lambda_handler(event, context):

    # read s3 content here

    # Vision API sample code
    def detect_safe_search(path):
        """Detects unsafe features in the file."""
        from google.cloud import vision
        import io
        client = vision.ImageAnnotatorClient()

        with io.open(path, 'rb') as image_file:
            content = image_file.read()

        image = vision.types.Image(content=content)

        google_response = client.safe_search_detection(image=image)
        safe = google_response.safe_search_annotation

        # Names of likelihood from google.cloud.vision.enums
        likelihood_name = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
                        'LIKELY', 'VERY_LIKELY')
        print('Safe search:')

        print('adult: {}'.format(likelihood_name[safe.adult]))
        print('medical: {}'.format(likelihood_name[safe.medical]))
        print('spoofed: {}'.format(likelihood_name[safe.spoof]))
        print('violence: {}'.format(likelihood_name[safe.violence]))
        print('racy: {}'.format(likelihood_name[safe.racy]))

    detect_safe_search(s3contentPath)

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

I know I have to package required libraries in .zip file to execute this lambda function. So I followed these steps:

  1. pip install google-cloud-vision -t . to install package in current directory.
  2. Then create .zip file and upload it to Aws Lambda console.

When I try to run this function I'm getting this error:

{
  "errorMessage": "cannot import name 'cygrpc' from 'grpc._cython' (/var/task/grpc/_cython/__init__.py)",
  "errorType": "ImportError",
  "stackTrace": [
    "  File \"/var/task/lambda.py\", line 46, in lambda_handler\n    detect_safe_search(s3contentPath)\n",
    "  File \"/var/task/lambda.py\", line 23, in detect_safe_search\n    from google.cloud import vision\n",
    "  File \"/var/task/google/cloud/vision.py\", line 17, in <module>\n    from google.cloud.vision_v1 import ImageAnnotatorClient\n",
    "  File \"/var/task/google/cloud/vision_v1/__init__.py\", line 25, in <module>\n    from google.cloud.vision_v1.gapic import image_annotator_client as iac\n",
    "  File \"/var/task/google/cloud/vision_v1/gapic/image_annotator_client.py\", line 24, in <module>\n    import google.api_core.gapic_v1.client_info\n",
    "  File \"/var/task/google/api_core/gapic_v1/__init__.py\", line 16, in <module>\n    from google.api_core.gapic_v1 import config\n",
    "  File \"/var/task/google/api_core/gapic_v1/config.py\", line 23, in <module>\n    import grpc\n",
    "  File \"/var/task/grpc/__init__.py\", line 23, in <module>\n    from grpc._cython import cygrpc as _cygrpc\n"
  ]
}

How can I fix this error?

Shinebayar G
  • 4,624
  • 4
  • 18
  • 29
  • 2
    gRPC may be painful to configure with AWS Lambda. The easier way is to use the Google Vision REST API https://cloud.google.com/vision/docs/reference/rest/ – Horatiu Jeflea Oct 11 '19 at 10:10
  • 1
    Try this: https://stackoverflow.com/questions/52913257/google-cloud-platform-api-for-python-and-aws-lambda-incompatibility-cannot-impo/52913258 – Brendan Oct 12 '19 at 05:26
  • @Brendan thanks for the link, that looks promising. I'll try that. – Shinebayar G Oct 12 '19 at 11:18

0 Answers0