使用URL运行显式内容检测(安全搜索)的Google Vision API

2024-06-12 09:07:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法通过Vision API的安全搜索/显式内容检测从URL运行图像。Python示例可在此处找到:

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

如果我将下面的内容保存在python文件中,那么运行它的最佳方式是什么?我试过了!python detect.py安全搜索urihttp://www.photos-public-domain.com/wp-content/uploads/2011/01/old-vw-bug-and-van.jpg但它不起作用。也许我遗漏了一些代码或者运行方式不对

来自上面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))

Tags: nameimagecomformatcloud内容searchresponse
1条回答
网友
1楼 · 发布于 2024-06-12 09:07:24

如果您只是执行了问题中包含的代码段,那么您没有正确地将值uri传递给代码。您需要解析在python命令上传递的参数。可以通过添加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)

使用命令!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

相关问题 更多 >