我用Flask创建了一个Python项目,并将其作为应用程序上传到Heroku
上。这个应用程序的目标是从一张从前端发送的薯片产品/袋子的照片中识别这个品牌。具体地说:
Heroku
上的应用程序收到了这张照片GCP Vision API
以检索有关此产品的信息(使用OCR等)调用GCP Vision API
的主要python脚本如下:
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
然后从主brand_detect()
函数(在此应用程序中用另一个脚本编写)调用函数flask
,以便将产品的品牌发送到前端。
Credentials.json
文件位于项目的文件夹中,它包含用于调用GCP Vision API
的凭据。看起来是这样的:
{
"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
工作良好,但显然,我必须做更多的事情,以便从我的应用程序调用GCP Vision API
在Heroku和做同样的任务。我的意思是,os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json"
行在Heroku上没有任何意义/实用程序,所以我必须修改上面的脚本,并在Heroku上做一些事情,以便设置谷歌凭据,并从Heroku上的python应用程序调用GCP Vision API
。
step-by-step 能给我解释一下如何修改上面的脚本,以及在Heroku上做些什么,以便像我在本地做的那样,在Heroku上调用 GCP Vision API
吗?
发布于 2018-05-09 01:12:30
我的问题的解决办法(令人惊讶)非常简单。
我只需要替换
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json"
使用
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "Credentials.json"
这既适用于我在Heroku上的应用程序,也适用于Credentials.json
文件在项目/ app文件夹中的本地应用程序。
https://stackoverflow.com/questions/50234541
复制相似问题