簡體   English   中英

解析來自 Google Cloud Vision API Python 客戶端的響應

[英]Parse response from Google Cloud Vision API Python Client

我正在為 Google Cloud Vision API 使用 Python 客戶端,與文檔http 中的代碼基本相同://google-cloud-python.readthedocs.io/en/latest/vision/

>>> from google.cloud import vision
>>> client = vision.ImageAnnotatorClient()
>>> response = client.annotate_image({
...   'image': {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}},
...   'features': [{'type': vision.enums.Feature.Type.FACE_DETECTOIN}],
... })

問題是響應沒有字段“注釋”(因為它是文檔)但是基於文檔有每個“類型”的字段。 所以當我嘗試獲取 response.face_annotations 時,我基本上不知道如何從響應(AnnotateImageResponse)中提取 Vision API 的結果以獲取類似 json/dictionary 之類的數據。 google-cloud-vision 的版本是 0.25.1,它被安裝為完整的 google-cloud 庫(pip install google-cloud)。 我想今天不是我的日子我感謝任何澄清/幫助

一個很好的選擇是使用Python API Google客戶端,示例在這里https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/vision/api/label/label.py

嗯。 這有點棘手,但是API總體上來說很棒。 實際上,您可以直接調用人臉檢測界面,它將完全吐出您想要的內容-包含所有信息的字典。

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

img = 'YOUR_IMAGE_URL'
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = img
faces = client.face_detection(image=image).face_annotations
print faces 

以上答案無濟於事,因為即興創作的三角洲正在發生,你可以說現實與理論。

視覺響應不是json類型,它只是定制的class類型,非常適合視覺呼叫。

所以經過大量研究,我想到了這個解決方案並且它有效

這是解決方案

把這個output轉成ProtoBuff再轉成json,就很簡單提取了。

 def json_to_hash_dump(vision_response): """ a function defined to take a convert the response from vision api to json object transformation via protoBuff Args: vision_response Returns: json_object """ from google.protobuf.json_format import MessageToJson json_obj = MessageToJson((vision_response._pb)) # to dict items r = json.loads(json_obj) return r

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM