Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Add gax support for entity annotations.#2918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -93,6 +93,51 @@ def from_api_repr(cls, response): | ||
| _entity_from_response_type(feature_type, annotation)) | ||
| return cls(**annotations) | ||
| @classmethod | ||
| def from_pb(cls, response): | ||
| """Factory: construct an instance of ``Annotations`` from protobuf. | ||
| :type response: :class:`~google.cloud.grpc.vision.v1.\ | ||
| image_annotator_pb2.AnnotateImageResponse` | ||
| :param response: ``AnnotateImageResponse`` from protobuf call. | ||
| :rtype: :class:`~google.cloud.vision.annotations.Annotations` | ||
| :returns: ``Annotations`` instance populated from gRPC response. | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| annotations = _process_image_annotations(response) | ||
| return cls(**annotations) | ||
| def _process_image_annotations(image): | ||
| """Helper for processing annotation types from protobuf. | ||
| :type image: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.\ | ||
| AnnotateImageResponse` | ||
| :param image: ``AnnotateImageResponse`` from protobuf. | ||
| :rtype: dict | ||
| :returns: Dictionary populated with entities from response. | ||
| """ | ||
| return { | ||
| 'labels': _make_entity_from_pb(image.label_annotations), | ||
| 'landmarks': _make_entity_from_pb(image.landmark_annotations), | ||
| 'logos': _make_entity_from_pb(image.logo_annotations), | ||
| 'texts': _make_entity_from_pb(image.text_annotations), | ||
| } | ||
| def _make_entity_from_pb(annotations): | ||
| """Create an entity from a gRPC response. | ||
| :type annotations: | ||
| :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.EntityAnnotation` | ||
| :param annotations: protobuf instance of ``EntityAnnotation``. | ||
| :rtype: list | ||
| :returns: List of ``EntityAnnotation``. | ||
| """ | ||
| return [EntityAnnotation.from_pb(annotation) for annotation in annotations] | ||
| def _entity_from_response_type(feature_type, results): | ||
| """Convert a JSON result to an entity type based on the feature. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -64,12 +64,32 @@ def from_api_repr(cls, response): | ||
| description = response['description'] | ||
| locale = response.get('locale', None) | ||
| locations = [LocationInformation.from_api_repr(location) | ||
| for location in response.get('locations', [])] | ||
| for location in response.get('locations', ())] | ||
| mid = response.get('mid', None) | ||
| score = response.get('score', None) | ||
| return cls(bounds, description, locale, locations, mid, score) | ||
| @classmethod | ||
| def from_pb(cls, response): | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| """Factory: construct entity from Vision gRPC response. | ||
| :type response: :class:`~google.cloud.grpc.vision.v1.\ | ||
| image_annotator_pb2.AnnotateImageResponse` | ||
| :param response: gRPC response from Vision API with entity data. | ||
| :rtype: :class:`~google.cloud.vision.entity.EntityAnnotation` | ||
| :returns: Instance of ``EntityAnnotation``. | ||
| """ | ||
| bounds = Bounds.from_pb(response.bounding_poly) | ||
| description = response.description | ||
| locale = response.locale | ||
| locations = [LocationInformation.from_pb(location) | ||
| for location in response.locations] | ||
| mid = response.mid | ||
| score = response.score | ||
| return cls(bounds, description, locale, locations, mid, score) | ||
| @property | ||
| def bounds(self): | ||
| """Bounding polygon of detected image feature. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -25,21 +25,33 @@ def __init__(self, vertices): | ||
| self._vertices = vertices | ||
| @classmethod | ||
| def from_api_repr(cls, response_vertices): | ||
| def from_api_repr(cls, vertices): | ||
| """Factory: construct BoundsBase instance from Vision API response. | ||
| :type response_vertices: dict | ||
| :param response_vertices: List of vertices. | ||
| :type vertices: dict | ||
| :param vertices: List of vertices. | ||
| :rtype: :class:`~google.cloud.vision.geometry.BoundsBase` or None | ||
| :returns: Instance of BoundsBase with populated verticies or None. | ||
| """ | ||
| if not response_vertices: | ||
| if vertices is None: | ||
| return None | ||
| return cls([Vertex(vertex.get('x', None), vertex.get('y', None)) | ||
| for vertex in vertices.get('vertices', ())]) | ||
| vertices = [Vertex(vertex.get('x', None), vertex.get('y', None)) for | ||
| vertex in response_vertices.get('vertices', [])] | ||
| return cls(vertices) | ||
| @classmethod | ||
| def from_pb(cls, vertices): | ||
| """Factory: construct BoundsBase instance from Vision gRPC response. | ||
| :type vertices: :class:`~google.cloud.grpc.vision.v1.\ | ||
| geometry_pb2.BoundingPoly` | ||
| :param vertices: List of vertices. | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| :rtype: :class:`~google.cloud.vision.geometry.BoundsBase` or None | ||
| :returns: Instance of ``BoundsBase`` with populated verticies. | ||
| """ | ||
| return cls([Vertex(vertex.x, vertex.y) | ||
| for vertex in vertices.vertices]) | ||
| @property | ||
| def vertices(self): | ||
| @@ -73,20 +85,35 @@ def __init__(self, latitude, longitude): | ||
| self._longitude = longitude | ||
| @classmethod | ||
| def from_api_repr(cls, response): | ||
| def from_api_repr(cls, location_info): | ||
| """Factory: construct location information from Vision API response. | ||
| :type response: dict | ||
| :param response: Dictionary response of locations. | ||
| :type location_info: dict | ||
| :param location_info: Dictionary response of locations. | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| :rtype: :class:`~google.cloud.vision.geometry.LocationInformation` | ||
| :returns: ``LocationInformation`` with populated latitude and | ||
| longitude. | ||
| """ | ||
| latitude = response['latLng']['latitude'] | ||
| longitude = response['latLng']['longitude'] | ||
| lat_long = location_info.get('latLng', {}) | ||
| latitude = lat_long.get('latitude') | ||
| longitude = lat_long.get('longitude') | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| return cls(latitude, longitude) | ||
| @classmethod | ||
| def from_pb(cls, location_info): | ||
| """Factory: construct location information from Vision gRPC response. | ||
| :type location_info: :class:`~google.cloud.vision.v1.LocationInfo` | ||
| :param location_info: gRPC response of ``LocationInfo``. | ||
| :rtype: :class:`~google.cloud.vision.geometry.LocationInformation` | ||
| :returns: ``LocationInformation`` with populated latitude and | ||
| longitude. | ||
| """ | ||
| return cls(location_info.lat_lng.latitude, | ||
| location_info.lat_lng.longitude) | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| @property | ||
| def latitude(self): | ||
| """Latitude coordinate. | ||
| @@ -127,15 +154,18 @@ def __init__(self, x_coordinate, y_coordinate, z_coordinate): | ||
| self._z_coordinate = z_coordinate | ||
| @classmethod | ||
| def from_api_repr(cls, response_position): | ||
| def from_api_repr(cls, position): | ||
| """Factory: construct 3D position from API response. | ||
| :type position: dict | ||
| :param position: Dictionary with 3 axis position data. | ||
| :rtype: :class:`~google.cloud.vision.geometry.Position` | ||
| :returns: `Position` constructed with 3D points from API response. | ||
| """ | ||
| x_coordinate = response_position['x'] | ||
| y_coordinate = response_position['y'] | ||
| z_coordinate = response_position['z'] | ||
| x_coordinate = position['x'] | ||
| y_coordinate = position['y'] | ||
| z_coordinate = position['z'] | ||
| return cls(x_coordinate, y_coordinate, z_coordinate) | ||
| @property | ||
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.