Search code examples
pythonpython-3.xgoogle-cloud-platformgoogle-cloud-visiongoogle-vision

Google Vision API - Running Explicit Content Detection (Safe Search) Using a URL


I'm having trouble running an image from a URL through the Vision API's Safe Search/Explicit Content Detection. Python Samples can be found here:

https://github.com/googleapis/python-vision/blob/HEAD/samples/snippets/detect/detect.py

If I were to save the below in a python file - what is the best way to run it? I tried !python detect.py safe-search-uri http://www.photos-public-domain.com/wp-content/uploads/2011/01/old-vw-bug-and-van.jpg but it's not working. Maybe I'm missing some of the code or running it the wrong way?

Sample Code from above github:

    from google.cloud import vision
    client = vision.ImageAnnotatorClient()
    image = vision.Image()
    image.source.image_uri = uri

    response = client.safe_search_detection(image=image)
    safe = 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]))

    if response.error.message:
        raise Exception(
            '{}\nFor more info on error messages, check: '
            'https://cloud.google.com/apis/design/errors'.format(
                response.error.message))

Solution

  • If you just executed the code snippet you included in your question, you are not passing the value uri to the code properly. You need to parse the arguments you are passing on your python command. You can do this by adding argparse.

    from google.cloud import vision
    import argparse
    
    # Parse the options in this part #
    parser = argparse.ArgumentParser(description='Safe search')
    parser.add_argument(
        '--safe-search-uri',
        dest='uri'
        )
    args = parser.parse_args()
    uri = args.uri
    
    # [START vision_safe_search_detection_gcs]
    def detect_safe_search_uri(uri):
        """Detects unsafe features in the file located in Google Cloud Storage or
        on the Web."""
        from google.cloud import vision
        client = vision.ImageAnnotatorClient()
        image = vision.Image()
        image.source.image_uri = uri
    
        response = client.safe_search_detection(image=image)
        safe = 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]))
    
        if response.error.message:
            raise Exception(
                '{}\nFor more info on error messages, check: '
                'https://cloud.google.com/apis/design/errors'.format(
                    response.error.message))
    # [END vision_safe_search_detection_gcs]
    
    detect_safe_search_uri(uri)
    

    Testing using command !python detect.py --safe-search-uri http://www.photos-public-domain.com/wp-content/uploads/2011/01/old-vw-bug-and-van.jpg : enter image description here