Google 愿景 API 未打印 return

Google vision API not printing a return

我正在尝试 python 中的一些脚本,并试图找出这些图像是否 returns 结果。但是,当 运行 python 不打印任何内容时。我没有收到错误,但似乎无法弄清楚。

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

def run(annotations):
# Instantiates a client
    client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
    file_name = os.path.join(
        os.path.dirname(__file__),
        'static/123456.jpg')

# Loads the image into memory
    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()

        image = types.Image(content=content)

    if annotations.pages_with_matching_images:
        print('\n{} Pages with matching images retrieved'.format(
            len(annotations.pages_with_matching_images)))


    matching = annotations.pages_with_matching_images


        print matching

我的工作基于这些示例

https://cloud.google.com/vision/docs/quickstart-client-libraries#client-libraries-install-python

https://cloud.google.com/vision/docs/internet-detection

您遗漏了一些关键部分:

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

def run():   # remove the argument since you aren't using it
# Instantiates a client
    client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
    file_name = os.path.join(
        os.path.dirname(__file__),
        'static/123456.jpg')

# Loads the image into memory
    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)   # dedent this

    web_detection = client.web_detection(image=image).web_detection

    """ annotations doesn't exist in your script as is...
    if annotations.pages_with_matching_images:
        print('\n{} Pages with matching images retrieved'.format(
            len(annotations.pages_with_matching_images)))
    """
    # replace above with this
    if web_detection.pages_with_matching_images:
        print('\n{} Pages with matching images retrieved'.format(
               len(web_detection.pages_with_matching_images)))

if __name__ == '__main__':
    run()

编辑教程脚本时需要注意的一些关键问题是跟随您的对象。如果您不首先创建该对象,则不能在您自己的脚本中使用教程中调用的对象。