如何设置Google凭据以从Heroku上的Python应用程序调用Google API [英] How to set Google Credentials to call Google API from Python app on Heroku

查看:79
本文介绍了如何设置Google凭据以从Heroku上的Python应用程序调用Google API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Flask创建了一个Python项目,并将其作为应用程序上传到Heroku.该应用程序的目标是从前端发送的薯片产品/袋的照片中识别品牌.具体来说:

I have created a Python project with Flask and I uploaded it on Heroku as an app. The goal of this app is to identify the brand from a photo of a potato chips product/bag which is sent from the front-end. Specifically:

  1. 前端发送一张薯片产品的照片
  2. Heroku上的应用收到了这张照片
  3. 该应用正在调用GCP Vision API来检索有关此产品的信息(通过使用OCR等)
  4. 该应用正在前端发送产品的品牌
  1. the front-end sends a photo of a potato chips product
  2. the app on Heroku receives this photo
  3. the app is calling GCP Vision API to retrieve information about this product (by using OCR etc)
  4. the app is sending back the brand of the product at the front-end

调用GCP Vision API的主要python脚本如下:

The main python script which calls the GCP Vision API is the following:

from google.cloud import vision
from google.cloud.vision import types
import os

# For local
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json"

brands = ['lays', 'pringles', 'ruffles', 'kettle']

def brand_detect(image):
    web, text = annotate(image)

    response_text = brand_text(text, brands)

    if (response_text is not None):

        return response_text

    else:

        response_web = brand_web(web, brands)

        if (response_web is not None):

            return response_web

        else:
            return 'Not Found'




def annotate(image):
    """Returns web annotations given the path to an image."""
    client = vision.ImageAnnotatorClient()

    image = types.Image(content=image)

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

    text_detection = client.document_text_detection(image=image)

    return web_detection, text_detection


def brand_web(web, brands):
    if web.web_entities:

        for entity in web.web_entities:

            for brand in brands:

                if (brand in entity.description.lower()) and (entity.score > 0.65):

                    return brand


def brand_text(text, brands):
    if text.full_text_annotation.text:

        for brand in brands:

            if (brand in text.full_text_annotation.text.lower()):
                return brand

然后从主要功能flask(在该应用程序的另一个脚本中编写)中调用功能brand_detect(),以便将产品的品牌信息发送到前端.

The function brand_detect() is then called from the main flask function (which is written in another script within this app) so as to send the brand of the product to the front-end.

Credentials.json文件位于项目的文件夹中,并且包含用于调用GCP Vision API的凭据.看起来像这样:

The Credentials.json file is inside the project's folder and it contains the credentials for calling the GCP Vision API. It looks like this:

{
  "type": "service_account",
  "project_id": "**********************",
  "private_key_id": "**********************",
  "private_key": "-----BEGIN PRIVATE KEY-----**********************-----END PRIVATE KEY-----\n",
  "client_email": "**********************",
  "client_id": "**********************",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "**********************"
}

该应用程序在PyCharm上可以在本地正常运行,但是显然我必须做更多的事情才能在Heroku上从我的应用程序中调用GCP Vision API并执行相同的任务.我的意思是,行os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json"在Heroku上没有任何意义/效用,因此我必须修改上面的脚本并在Heroku上做一些事情,以便设置Google凭据并从python应用程序中调用GCP Vision API在Heroku上.

The app works fine locally with PyCharm but obviously I have to do some things more in order to call the GCP Vision API from my app on Heroku and do the same task. By this I mean, that the line os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json" does not have any meaning/utility on Heroku so I have to modify my script above and do some things on Heroku so as to set Google Credentials and to call GCP Vision API from my python app on Heroku.

有人可以逐步解释我 ,如何在我的脚本上修改我的脚本以及如何在Heroku上执行操作,以便像在本地一样在Heroku上调用GCP Vision API?

Can someone explain me step-by-step how to modify my script above and what to do on Heroku in order to call the GCP Vision API on Heroku as I did it locally?

推荐答案

(很奇怪)我的问题的解决方案非常简单.

The solution to my problem was (surprisingly) very simple.

我只需要更换

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json"

使用

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "Credentials.json"

,当Credentials.json文件位于project/app文件夹中时,这既适用于Heroku上的应用程序,也适用于本地应用程序.

and this works both for my app on Heroku and locally when the Credentials.json file is within the project/app folder.

这篇关于如何设置Google凭据以从Heroku上的Python应用程序调用Google API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆