Google 镜头 API/SDK

Pyt*_*ast 13 google-cloud-platform google-cloud-vision

我正在通过Google Cloud Vision API搜索图像中的相关产品。但是,我发现我需要首先创建一个产品集(并上传图像),然后搜索我创建的产品集。

是否有任何 API 可以让我提供图像(无需创建任何产品集)并在 google(或亚马逊等任何特定网站)中搜索图像中的相关产品(及其电子商务链接)?基本上复制了谷歌镜头对其应用程序的作用。如果没有,是否有适用于相同用例的谷歌镜头移动 SDK,即查找提供图像的相关产品(及其电子商务 URI)?

更新1

我对此进行了更多研究,并找到了Web Inspection API。这确实在一定程度上达到了目的。但是,我无法过滤来自特定网站的结果(例如:亚马逊)。有没有办法过滤特定域的结果?

小智 4

使用 Vision API,您可以从 Web 中检测图像中的属性、人脸和文本。

一个选项是在列出资源、评估或操作时使用过滤来返回更具体的结果。使用?filter="[filter_name=]filter-value返回与过滤器值匹配的所有 JSON 对象。您可以在此链接中查看更多信息。

另一种选择是在显示结果之前进行过滤。您可以看到这个示例,它返回来自 Web和云存储的链接,其中包含图像中的某些颜色。

你可以看这个例子:

def detect_properties_uri(uri):
    """Detects image properties 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.image_properties(image=image)
    props = response.image_properties_annotation
    print('Properties:')
 
    for color in props.dominant_colors.colors:
        print('frac: {}'.format(color.pixel_fraction))
        print('\tr: {}'.format(color.color.red))
        print('\tg: {}'.format(color.color.green))
        print('\tb: {}'.format(color.color.blue))
        print('\ta: {}'.format(color.color.alpha))
 
    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_image_property_detection_gcs]
Run Code Online (Sandbox Code Playgroud)

下一步是迭代结果(如本示例所示)并过滤包含该域的链接。查看网络链接内的域。

for entity in web_detection.web_entities:
    if (str(entity.description).find(domain) >-1)
        print(entity.description)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将迭代每个结果和过滤器,尝试查找描述中的域并将其显示在屏幕上。

另一种选择是以 JSON 格式操作结果。在这场追逐中,你需要:

  • 解析 JSON
  • 过滤 JSON

你可以看这个例子:

import json
 
input_json = """
[
    {
        "type": "1",
        "name": "name 1"
    },
    {
        "type": "2",
        "name": "name 2"
    },
    {
        "type": "1",
        "name": "name 3"
    }
]"""
 
# Transform json input to python objects
input_dict = json.loads(input_json)
 
# Filter python objects with list comprehensions
output_dict = [x for x in input_dict if x['type'] == '1']
 
# Transform python object back into json
output_json = json.dumps(output_dict)
 
# Show json
print output_json
Run Code Online (Sandbox Code Playgroud)