我正在尝试了解Google Vision API,并且正在使用一些python代码。当我尝试提取地标的类型时,找不到正确的方法。
我能找到的最好的documentation是Java,其中getType()被列为方法。
这里是带有输出的试用代码:
from google.cloud import vision
import pickle
import inspect
client = vision.ImageAnnotatorClient()
response = client.annotate_image({
'image': {'source': {'image_uri': 'gs://bsa-test-bucket/2020-04-28 19.54.21.jpg'}},
'features': [{'type': vision.enums.Feature.Type.FACE_DETECTION},
{'type' : vision.enums.Feature.Type.LABEL_DETECTION}],})
for face in response.face_annotations:
for i, lm in enumerate(face.landmarks):
print(i, 'landmark_annotations type', type(lm), '\n')
print(inspect.getmembers(lm))
print(lm.getType())
输出:
> 0 landmark_annotations type <class
> 'google.cloud.vision_v1.proto.image_annotator_pb2.Landmark'>
>
> [('_SetListener', <bound method Message._SetListener of type: LEFT_EYE
> position { x: 1198.06494140625 y: 1629.644287109375 z:
> -0.0032958984375 }
> >), ('__getstate__', <bound method Message.__getstate__ of type: LEFT_EYE position { x: 1198.06494140625 y: 1629.644287109375 z:
> -0.0032958984375 }
> >)]
Traceback (most recent call last): File "src/gv-test.py", line 18, in <module>
> print(lm.getType()) AttributeError: getType
什么是正确的方法,在哪里可以找到用Python实现的类的文档?
谢谢!
我找到了。它是一个属性。
response.face_annotations[0].type
返回正确的类型常量。