Skip to content

Commit bc37f84

Browse files
committed
Feedback from PR googleapis#1803
1 parent e8196bb commit bc37f84

File tree

8 files changed

+48
-56
lines changed

8 files changed

+48
-56
lines changed

gcloud/vision/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717

1818
from gcloud.vision.client import Client
19+
from gcloud.vision.feature import FeatureTypes

gcloud/vision/annotation.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616

1717

1818
class EntityAnnotation(object):
19-
_mid = None
20-
_locale = None
21-
_description = None
22-
_score = None
23-
_confidence = None
24-
_topicality = None
25-
_bounding_poly = None
26-
_locations = []
27-
_properties = []
28-
2919
def __init__(self, mid, locale, description, score, confidence, topicality,
3020
bounding_poly, locations, properties):
3121
self._mid = mid

gcloud/vision/client.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ def __init__(self, image, feature):
3636
self._features = []
3737
self._image = image
3838

39-
if type(feature) == list:
39+
if isinstance(feature, list):
4040
self._features.extend(feature)
41-
elif type(feature) == Feature:
41+
elif isinstance(feature, Feature):
4242
self._features.append(feature)
4343

4444
def as_dict(self):
4545
return {
46-
"image": self.image,
47-
"features": self.features
46+
'image': self.image,
47+
'features': self.features
4848
}
4949

5050
@property
@@ -77,18 +77,22 @@ class Client(JSONClient):
7777
``credentials`` for the current object.
7878
7979
Usage:
80-
>>> from gcloud import vision
81-
>>> vision_client = vision.Client()
82-
>>> f = open('/tmp/car.jpg', 'r')
83-
>>> vision_client.annotate(f.read(), "LABEL_DETECTION", 3)
80+
.. code::
81+
>>> from gcloud import vision
82+
>>> vision_client = vision.Client()
83+
>>> with open('/tmp/car.jpg', 'r') as f:
84+
... vision_client.annotate(f.read(), vision.LABEL_DETECTION, 3)
8485
8586
Multiple images example:
86-
>>> images = (("./image.jpg", ["LABEL_DETECTION", "LANDMARK_DETECTION"]),
87-
>>> ("./image2.jpg", ["FACE_DETECTION", "TEXT_DETECTION"]),)
88-
>>> annotated_images = []
89-
>>> for image, feature_types in images:
90-
>>> annotated_images.append(vision_client.annotate(image,
91-
>>> feature_types))
87+
.. code::
88+
>>> images = (('./image.jpg', [vision.FeatureTypes.LABEL_DETECTION,
89+
... vision.FeatureTypes.LANDMARK_DETECTION]),
90+
... ('./image2.jpg', [vision.FeatureTypes.FACE_DETECTION,
91+
vision.FeatureTypes.TEXT_DETECTION]),)
92+
>>> annotated_images = []
93+
>>> for image, feature_types in images:
94+
... annotated_images.append(vision_client.annotate(image,
95+
... feature_types))
9296
"""
9397

9498
_connection_class = Connection
@@ -113,24 +117,23 @@ def annotate(self, image, feature_type, max_results=1):
113117
:param max_results: The number of results per feature type to be
114118
returned.
115119
"""
116-
vision_requests = []
120+
data = {'requests': []}
117121
features = []
118122

119-
if type(image) == str:
123+
if isinstance(image, str):
120124
img = Image(image)
121125

122-
if type(feature_type) == list:
126+
if isinstance(feature_type, list):
123127
for feature in feature_type:
124128
features.append(Feature(feature, max_results))
125129
else:
126130
features.append(Feature(feature_type, max_results))
127131

128-
vision_requests.append(VisionRequest(img, features))
132+
data['requests'].append(VisionRequest(img, features))
129133

130-
data = {"requests": vision_requests}
131134
data = json.dumps(data, cls=VisionJSONEncoder)
132-
resp = self.connection.api_request(method="POST",
133-
path="/images:annotate",
135+
resp = self.connection.api_request(method='POST',
136+
path='/images:annotate',
134137
data=data)
135138
resp = resp['responses']
136139
return resp

gcloud/vision/feature.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ class FeatureTypes(object):
1919
See:
2020
https://cloud.google.com/vision/reference/rest/v1/images/annotate#Type
2121
"""
22-
FACE_DETECTION = "FACE_DETECTION"
23-
LANDMARK_DETECTION = "LANDMARK_DETECTION"
24-
LOGO_DETECTION = "LOGO_DETECTION"
25-
LABEL_DETECTION = "LABEL_DETECTION"
26-
TEXT_DETECTION = "TEXT_DETECTION"
27-
SAFE_SEARCH_DETECTION = "SAFE_SEARCH_DETECTION"
22+
FACE_DETECTION = 'FACE_DETECTION'
23+
LANDMARK_DETECTION = 'LANDMARK_DETECTION'
24+
LOGO_DETECTION = 'LOGO_DETECTION'
25+
LABEL_DETECTION = 'LABEL_DETECTION'
26+
TEXT_DETECTION = 'TEXT_DETECTION'
27+
SAFE_SEARCH_DETECTION = 'SAFE_SEARCH_DETECTION'
2828

2929

3030
class Feature(object):
31-
"""Feature object specifying the annotation and number of results for the
32-
specified feature type.
31+
"""Feature object specifying the annotation type and maximum results.
3332
3433
:type feature_type: str
3534
:param feature_type: String representation of feature type.
@@ -48,8 +47,8 @@ def __init__(self, feature_type, max_results=1):
4847
def as_dict(self):
4948
"""Generate dictionary for Feature request format."""
5049
return {
51-
"type": self.feature_type,
52-
"maxResults": self.max_results
50+
'type': self.feature_type,
51+
'maxResults': self.max_results
5352
}
5453

5554
@property

gcloud/vision/image.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import httplib2
1516

1617
from base64 import b64encode
1718

@@ -33,27 +34,26 @@ def __init__(self, image):
3334

3435
# TODO: string index out of range for len(string) < 4.
3536
# TODO: Support file path.
36-
if image[:4] == 'http':
37-
import httplib2
37+
if image.startswith('http'):
3838
http = httplib2.Http()
3939
response, downloaded_image = http.request(image, 'GET')
4040
self._content = b64encode(downloaded_image)
41-
elif image[:5] == 'gs://':
41+
elif image.startswith('gs://'):
4242
self._source = image
43-
elif type(image) == str:
43+
elif isinstance(image, str):
4444
self._content = b64encode(image)
4545

4646
def as_dict(self):
4747
"""Generate dictionary for request"""
4848

4949
if self.content:
5050
return {
51-
"content": self.content
51+
'content': self.content
5252
}
5353
else:
5454
return {
55-
"source": {
56-
"gcs_image_uri": self.source
55+
'source': {
56+
'gcs_image_uri': self.source
5757
}
5858
}
5959

gcloud/vision/test_client.py

Lines changed: 3 additions & 4 deletions
Large diffs are not rendered by default.

gcloud/vision/test_feature.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class TestFeature(unittest2.TestCase):
2323
def test_construct_feature(self):
2424
feature = Feature(FeatureTypes.LABEL_DETECTION)
2525
self.assertEqual(1, feature.max_results)
26-
self.assertEqual("LABEL_DETECTION", feature.feature_type)
26+
self.assertEqual('LABEL_DETECTION', feature.feature_type)
2727

2828
feature = Feature(FeatureTypes.FACE_DETECTION, 3)
2929
self.assertEqual(3, feature.max_results)
30-
self.assertEqual("FACE_DETECTION", feature.feature_type)
30+
self.assertEqual('FACE_DETECTION', feature.feature_type)

gcloud/vision/test_image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020

2121
class TestVisionImage(unittest2.TestCase):
22-
IMAGE_SOURCE = "gs://some/image.jpg"
23-
IMAGE_CONTENT = "/9j/4QNURXhpZgAASUkq"
22+
IMAGE_SOURCE = 'gs://some/image.jpg'
23+
IMAGE_CONTENT = '/9j/4QNURXhpZgAASUkq'
2424
B64_IMAGE_CONTENT = base64.b64encode(IMAGE_CONTENT)
2525

2626
def test_image_source_type_content(self):

0 commit comments

Comments
 (0)