Google 视觉 API - 运行 使用 URL 的显式内容检测(安全搜索)
Google Vision API - Running Explicit Content Detection (Safe Search) Using a URL
我在通过 Vision API 的安全 Search/Explicit 内容检测从 URL 中获取图像时遇到问题。 Python 示例可在此处找到:
https://github.com/googleapis/python-vision/blob/HEAD/samples/snippets/detect/detect.py
如果我要将以下内容保存在 python 文件中 - 运行 的最佳方式是什么?我尝试了 !python detect.py safe-search-uri http://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))
如果您只是执行了问题中包含的代码片段,那么您没有正确地将值 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
进行测试:
我在通过 Vision API 的安全 Search/Explicit 内容检测从 URL 中获取图像时遇到问题。 Python 示例可在此处找到:
https://github.com/googleapis/python-vision/blob/HEAD/samples/snippets/detect/detect.py
如果我要将以下内容保存在 python 文件中 - 运行 的最佳方式是什么?我尝试了 !python detect.py safe-search-uri http://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))
如果您只是执行了问题中包含的代码片段,那么您没有正确地将值 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
进行测试: