我已经使用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
本地工作正常,但显然我需要做更多的事情才能从Heroku上的应用调用GCP Vision API
并执行相同的任务。我的意思是,行os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json"
在Heroku上没有任何含义/实用程序,因此我必须修改上面的脚本并在Heroku上做一些事情,以便设置Google凭据并致电GCP Vision API
来自我在Heroku上的python应用程序。
有人可以解释我一步一步如何修改上面的脚本以及在Heroku上做什么,以便在我做的时候调用Heroku上的GCP Vision API
本地吗
答案 0 :(得分:1)
我的问题的解决方案(令人惊讶)非常简单。
我只需要替换
SELECT
TBboth.Id,
Nz(Table1!PartNo, Table2!PartNo) AS PartNo,
Nz(Table1!Nomenclature, Table2!Nomenclature) As Nomenclature,
Sum(Nz(Table1!Quantity,0)) AS Wksp1,
Sum(Nz(Table2!Quantity,0)) AS Wksp2
FROM
(TBboth
LEFT JOIN
TB1 ON TBboth.Id = TB1.Id)
LEFT JOIN
TB2 ON TBboth.Id = TB2.Id
GROUP BY
TBboth.Id,
Nz(Table1!PartNo, Table2!PartNo),
Nz(Table1!Nomenclature, Table2!Nomenclature);
与
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/User/PycharmProjects/Project_brand/Credentials.json"
当os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "Credentials.json"
文件位于project / app文件夹中时,这适用于Heroku上的应用和本地应用。