GeezerVison is showing github.com — Add vision label detection by daspecster · Pull Request #2237 · googleapis/google-cloud-python
Add vision label detection
#2237
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.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.py
(5)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -55,7 +55,7 @@ def from_api_repr(cls, response):
:rtype: :class:`~google.cloud.vision.entiy.EntityAnnotation`
:returns: Instance of ``EntityAnnotation``.
"""
bounds = Bounds.from_api_repr(response[ 'boundingPoly'] )
bounds = Bounds.from_api_repr(response.get( 'boundingPoly') )
description = response['description']
locations = [LocationInformation.from_api_repr(location)
for location in response.get('locations', [])]
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -31,13 +31,14 @@ def from_api_repr(cls, response_vertices):
:type response_vertices: dict
:param response_vertices: List of vertices.
:rtype: :class:`~google.cloud.vision.geometry.BoundsBase`
:returns: Instance of BoundsBase with populated verticies.
:rtype: :class:`~google.cloud.vision.geometry.BoundsBase` or None
:returns: Instance of BoundsBase with populated verticies or None .
"""
vertices = []
for vertex in response_vertices['vertices']:
vertices.append(Vertex(vertex.get('x', None),
vertex.get('y', None)))
if not response_vertices:
return None
vertices = [Vertex(vertex.get('x', None), vertex.get('y', None)) for
vertex in response_vertices.get('vertices', [])]
Comment thread
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
return cls(vertices)
@property
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -94,6 +94,7 @@ def _detect_annotation(self, feature):
:class:`~google.cloud.vision.entity.EntityAnnotation`.
"""
reverse_types = {
'LABEL_DETECTION': 'labelAnnotations',
'LANDMARK_DETECTION': 'landmarkAnnotations',
'LOGO_DETECTION': 'logoAnnotations',
}
Expand Down
Expand Up
@@ -122,6 +123,18 @@ def detect_faces(self, limit=10):
return faces
def detect_labels(self, limit=10):
"""Detect labels that describe objects in an image.
:type limit: int
:param limit: The maximum number of labels to try and detect.
:rtype: list
:returns: List of :class:`~google.cloud.vision.entity.EntityAnnotation`
"""
feature = Feature(FeatureTypes.LABEL_DETECTION, limit)
return self._detect_annotation(feature)
def detect_landmarks(self, limit=10):
"""Detect landmarks in an image.
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,28 @@
LABEL_DETECTION_RESPONSE = {
'responses': [
{
'labelAnnotations': [
{
'mid': '/m/0k4j',
'description': 'automobile',
'score': 0.9776855
},
{
'mid': '/m/07yv9',
'description': 'vehicle',
'score': 0.947987
},
{
'mid': '/m/07r04',
'description': 'truck',
'score': 0.88429511
}
]
}
]
}
LANDMARK_DETECTION_RESPONSE = {
'responses': [
{
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -114,6 +114,27 @@ def test_face_detection_from_content(self):
image_request['image']['content'])
self.assertEqual(5, image_request['features'][0]['maxResults'])
def test_label_detection_from_source(self):
from google.cloud.vision.entity import EntityAnnotation
from unit_tests.vision._fixtures import (LABEL_DETECTION_RESPONSE as
RETURNED)
credentials = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=credentials)
client.connection = _Connection(RETURNED)
image = client.image(source_uri=_IMAGE_SOURCE)
labels = image.detect_labels(limit=3)
self.assertEqual(3, len(labels))
self.assertTrue(isinstance(labels[0], EntityAnnotation))
image_request = client.connection._requested[0]['data']['requests'][0]
self.assertEqual(_IMAGE_SOURCE,
image_request['image']['source']['gcs_image_uri'])
self.assertEqual(3, image_request['features'][0]['maxResults'])
self.assertEqual('automobile', labels[0].description)
self.assertEqual('vehicle', labels[1].description)
self.assertEqual('/m/0k4j', labels[0].mid)
self.assertEqual('/m/07yv9', labels[1].mid)
def test_landmark_detection_from_source(self):
from google.cloud.vision.entity import EntityAnnotation
from unit_tests.vision._fixtures import (LANDMARK_DETECTION_RESPONSE as
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.