0

我正在使用 python 中的一些脚本并试图找出这些图像是否返回结果。但是,当运行 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

4

1 回答 1

0

您缺少一些关键部分:

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()

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

于 2019-05-08T13:45:23.067 回答