所以我用一个小程序从图像中获取车牌。我这样做是通过发送谷歌视觉图像,并搜索我得到的bex的文本,寻找像正则表达式一样的牌照。
# -*- coding: utf-8 -*-
"""
Created on Sat May 23 19:42:18 2020
@author: Odatas
"""
import io
import os
from google.cloud import vision_v1p3beta1 as vision
import cv2
import re
# Setup google authen client key
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'client_key.json'
# Source path content all images
SOURCE_PATH = "F:/Radsteuereintreiber/Bilder Temp/"
def recognize_license_plate(img_path):
# Read image with opencv
img = cv2.imread(img_path)
# Get image size
height, width = img.shape[:2]
# Scale image
img = cv2.resize(img, (800, int((height * 800) / width)))
# Save the image to temp file
cv2.imwrite(SOURCE_PATH + "output.jpg", img)
# Create new img path for google vision
img_path = SOURCE_PATH + "output.jpg"
# Create google vision client
client = vision.ImageAnnotatorClient()
# Read image file
with io.open(img_path, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
# Recognize text
response = client.text_detection(image=image)
texts = response.text_annotations
return texts
path = SOURCE_PATH + 'IMG_20200513_173356.jpg'
plate = recognize_license_plate(path)
for text in plate:
# read description
license_plate = text.description
# change all symbols to whitespace.
license_plate = re.sub('[^a-zA-Z0-9\n\.]', ' ', license_plate)
# see if some text matches pattern
test = re.findall('[A-Z]{1,3}\s[A-Z]{1,2}\s\d{1,4}', str(license_plate))
# stop if you found someting
if test is not None:
break
try:
print(test[0])
except Exception:
print("No plate found")如您所见,我在开始时将使能变量设置为client_key.json。当我分发我的程序时,我不喜欢把我的密钥发送给每个用户。所以我想把钥匙直接包括在程序中。
我尝试使用google的显式凭证方法,并在程序中创建了一个json,如下所示:
def explicit():
#creat json
credentials={ REMOVED: INSIDE HER WOULD BE ALL THE INFORMATION FROM THE JSON KEY FILE.
}
json_credentials=json.dumps(credentials)
# Explicitly use service account credentials by specifying the private key
# file.
storage_client = storage.Client.from_service_account_json(
json_credentials)
# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)
# [END auth_cloud_explicit]但我明白错误。
Errno 2没有这样的文件或目录: json的内容再次删除
因此,我不确定是否必须切换到基于api的调用,以及如何调用相同的功能?因为我必须很明显地上传一张图片,我甚至认为通过api调用是不可能的。
所以我有点迷失了。谢谢你的帮助。
发布于 2020-05-26 17:38:46
如果您希望用户能够对您的Google项目进行API调用,那么将您的服务帐户键(无论是作为JSON文件还是内联的)包含在您的代码中,基本上是等效的,而且用户可以访问您的密钥。
但是,这通常是不建议的:即使是范围最小的服务帐户也可以提出请求,并可能对您的帐户产生费用。
另一种选择是在Google项目中部署自己的API,该项目封装了对Vision的调用。这将使您能够保护您的服务帐户密钥,如果需要,还可以限制甚至阻止对此API的调用。
然后,您的脚本或库将调用此自定义API,而不是直接调用Vision API。
https://stackoverflow.com/questions/61998663
复制相似问题